Create and merge GitHub pull requests API C#

Viewed 441

I am creating a scheduled tool for merging changes to my GitHub repository on a daily basis. For this I need the tool to do the following function

  • Checkout a branch (working branch) and pull it with the latest changes from the target branch
  • Make the changes in the working branch which needs to be merged in the target branch.
  • Commit the modified files into the remote working branch.
  • Create the pull request from the working branch to the target branch.
  • Merge the pull request that is created.

I was able to achieve the first 3 points above using the PowerShell command and the GitHub access token. I am stuck in the part of creating the pull request and merging them. I have followed the below documentation for the same but the API call is returning the Not found.

https://docs.github.com/en/rest/reference/pulls#create-a-pull-request

Sample code used based on the above documentation

RestRequest restRequest = new RestRequest("repos/" + GitHubUserId + "/" + "working-repository", Method.POST);
restRequest.Parameters.Clear();
restRequest.AddHeader("accept", "application/vnd.github.v3+json");
restRequest.AddParameter("head", sourceBranch);
restRequest.AddParameter("base", targetBranch);
restRequest.AddParameter("title", title);
restRequest.AddParameter("body", description);

https://github.com/octokit/octokit.net/blob/main/docs/demos/exploring-pull-requests.md

Sample code used based on the above documentation using Octokit package.

GitHubClient gitHubClient = new GitHubClient(new ProductHeaderValue(ConfigurationManager.AppSettings["GitHubUserId"].ToString(), "v1.0.0"));
gitHubClient.Credentials = new Credentials(ConfigurationManager.AppSettings["GitHubAccessToken"].ToString());
NewPullRequest newPullRequest = new NewPullRequest(title, sourceBranch, targetBranch);
var octokitRepo = await gitHubClient.Repository.Get("Organization", "working-reposiory");
var pullRequest = await gitHubClient.PullRequest.Create("Organization", "working-reposiory", newPullRequest);

What can I try next?

0 Answers
Related