Update GitHub Pull Request Body using GitHub Actions

Recently I have been automating GitHub Pull Request Body with various updates/outputs from GitHub Actions, I wanted to create a quick blog post on a couple of ways to do this that will hopefully assist you.

If you’re like me, you’ve probably found yourself in situations where you need to update the body of a pull request after it’s been opened. It could be a simple typo fix, additional information from outputs within your workflow, or any other changes you want to make. Thankfully with GitHub Actions makes it incredibly easy to automate tasks like this, saving you time. In this example, we’ll be using the actions/github-script action to update the body of a pull request.

What will I cover in this blog post?

I will show you two scripts that can:

  • Updating the Pull Request Body with Fresh Output from GitHub Actions
  • Appending to the Initial Pull Request Body with Action Output

What is the GitHub Pull request body that I am referring to?

The area to where you add your initial comments for the pull request, it could be text, reference to a bug request, URL links etc:

GitHub Actions time

1. Updating the Pull Request Body with Fresh Output from GitHub Actions:

Lets look at the initial action that will just replace the content from above with what I have in my body: reference below:

      - uses: actions/github-script@v7
        with:
          script: |
            github.rest.pulls.update({
              pull_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'thomasthornton.cloud example'
            })

Running this as part of my work flow, lets check the body now:

It has now updated to the body reference: thomasthornton.cloud example with the above highlighting showing that has been edited by github-actions. Checking the change we can see the replacement:

Awesome, very simple to implement – but very useful, heres the full GitHub workflow below used:

name: Update PR Body Example
on:
  pull_request:
    branches:
      - main
  workflow_dispatch:

jobs: 
  review:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/github-script@v7
        with:
          script: |
            github.rest.pulls.update({
              pull_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'thomasthornton.cloud example'
            })

GitHub Repository containing the above

2. Appending to the Initial Pull Request Body with Action Output:

Lets now look at similar, but this time appending to the initial pull request body, when the pull request has been created, the initial body may contain information you don’t want removed as its part of your organisations/teams processes.

As below, we can see some relevant content:

Now look at the action that will just append to the content from above with what I have in my body: reference below:

      - uses: actions/github-script@v7
        with:
          script: |
            github.rest.pulls.update({
                pull_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: context.payload.pull_request.body + '\nThis has been appended by GitHub Action :)'
            })

Now with a successful run, I have now appended to the body: This has been appended by GitHub Action :)

Reviewing the action edit, we can see it updated successfully:

The GitHub workflow that has been used as part of this:

name: Append to PR Body Example
on:
  pull_request:
    branches:
      - main
  workflow_dispatch:

jobs: 
  review:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/github-script@v7
        with:
          script: |
            github.rest.pulls.update({
                pull_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: context.payload.pull_request.body + '\nThis has been appended by GitHub Action :)'
            })

GitHub Repository containing the above

And there you have it! Two simple yet powerful ways to automate pull request updates using GitHub Actions. Feel free to tweak and customise the workflows to fit your specific needs.

Automation like this not only saves time but also ensures consistency and efficiency in your development workflow. Give it a try, and let me know how it goes!

Leave a Reply