Create GitHub Pull Requests Automatically With n8n

Even though the native GitHub node in n8n doesn’t allow you to create pull requests, it’s still actually pretty easy to do if you use HTTP request nodes in its place.

Looks something like this:

gh n8n

Before you do anything, you’ll need to create a personal access token in your GitHub. You’ll need it to integrate your n8n setup with it.

Then:

Node #1: HTTP Request – Get main SHA

This gets the hash for this operation.

  • Method: GET
  • URL: https://api.github.com/repos/{user}/{repo}/git/ref/heads/main
  • Headers:
Authorization: Bearer YOUR_TOKEN
Accept: application/vnd.github+json
X-GitHub-Api-Version: 2022-11-28

Notice that you need to replace YOUR_TOKEN with your actual GitHub token.

After this node executes, you’ll access the result from response via: object.sha

Node #2: HTTP Request – Create branch

  • Method: POST
  • URL: https://api.github.com/repos/{user}/{repo}/git/refs
  • Headers same as above
  • Body:
{
  "ref": "refs/heads/branch-name",
  "sha": "{{ $json.object.sha }}"
}

Replace branch-name with the new branch name that you want to set.

Node #3: GitHub node – Create File

When adding the GitHub node, select “Create File” as the main action.

  • Operation: Create
  • Owner: {user}
  • Repository: {repo}
  • File path: path-to-new-file
  • File content: {{ $('Some other node with content').item.json.content }}
  • Commit message: Just some message that makes sense to you
  • Branch: branch-name

This whole part: {{ $('Some other node with content').item.json.content }} is just an example. This is where the content of the new file comes from in my case. You can put whatever other source of content you want there.

Node #4: HTTP Request – Create PR

This is the node that’s actually creating the PR.

  • Method: POST
  • URL: https://api.github.com/repos/{user}/{repo}/pulls
  • Headers same as before
  • Body:
{
  "title": "Title of the PR",
  "head": "branch-name",
  "base": "main",
  "body": "Additional message you want to add"
}

Done! After executing all four nodes, you will have a new PR in the repo.