octaviuslabs / walter

AI-powered software development assistant built right into GitHub so it can act as your junior developer.
MIT License
8 stars 1 forks source link

Get the repo primary branch #114

Closed jsfour closed 1 year ago

jsfour commented 1 year ago

Right now we have the base branch hard coded to 'main' here: https://github.com/octaviuslabs/walter/blob/main/src/code-committer.ts#L43 when in reality we should be pulling the primary branch for the repo from the giithhub api.

jsfour commented 1 year ago

@imwalterbot what do you think the best approach is here?

imwalterbot commented 1 year ago

@imwalterbot what do you think the best approach is here?

Queued for processing...

imwalterbot commented 1 year ago

@imwalterbot what do you think the best approach is here?

Processing this now

imwalterbot commented 1 year ago

To update the hardcoded 'main' branch to the primary branch fetched from the GitHub API, you can follow these steps:

  1. Create a function to fetch the primary branch from the GitHub API.
async function getPrimaryBranch(repository: Repository): Promise<string> {
  const response = await octokit.rest.repos.get({
    owner: repository.owner.login,
    repo: repository.name,
  });

  return response.data.default_branch;
}
  1. Update the createNewPullRequest function to fetch the primary branch before creating a pull request.
export const createNewPullRequest = async (
  result: psg.CodeEdit[],
  repo: Repository,
  issueNumber?: number
) => {
  console.log("creating branch");
  const primaryBranch = await getPrimaryBranch(repo);
  const newBranch = await createBranch(repo, primaryBranch);
  console.log("committing");
  await commitCode(repo, newBranch, result);

  console.log("creating pull request");
  await createPullRequest(repo, newBranch, issueNumber, primaryBranch);
};
  1. Update the createBranch function to accept the primary branch as a parameter.
async function createBranch(
  repository: Repository,
  primaryBranch: string
): Promise<string> {
  const baseBranch = await octokit.rest.git.getRef({
    owner: repository.owner.login,
    repo: repository.name,
    ref: `heads/${primaryBranch}`,
  });

  const newBranchName = `bot-generated-code-${Date.now()}`;

  await octokit.rest.git.createRef({
    owner: repository.owner.login,
    repo: repository.name,
    ref: `refs/heads/${newBranchName}`,
    sha: baseBranch.data.commit.sha,
  });

  return newBranchName;
}

With these changes, the primary branch will be fetched from the GitHub API and used instead of the hardcoded 'main' branch.

jsfour commented 1 year ago

@imwalterbot APPROVED edits on https://github.com/octaviuslabs/walter/blob/main/src/code-committer.ts#L43

imwalterbot commented 1 year ago

@imwalterbot APPROVED edits on https://github.com/octaviuslabs/walter/blob/main/src/code-committer.ts#L43

Queued for processing...

imwalterbot commented 1 year ago

@imwalterbot APPROVED edits on https://github.com/octaviuslabs/walter/blob/main/src/code-committer.ts#L43

Processing this now