0

I'm attempting to create an Action that automatically adds a merged PR to a project so the PR can be reviewed for documentation needs. The key here is that the PR should be added to the project after it's been merged into main. From what I can tell, Github Actions don't work directly with merged PRs for the pull_request webhook.

Has anybody managed to do this or have any tips? My initial thought is to do something like:

name: Add-Merged-PRs

# Run this workflow every time a PR is merged into the main branch
on: push
  branches:
    - main

jobs:
  # Take the PR that was merged and add it to a specified project.
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116

1 Answers1

0

You can use the pull_request event and narrow it down to "has been closed" and "was merged" using the types and branches properties of pull_request, then check in the github.event.pull_request context that the merged property is true.

All together, for a project named "Test" and a column "Merged PRs", using the GitHub CLI to make API calls, a workflow might look like this:

name: Add merged PRs to project

# When a PR into main is closed or merged
on:
  pull_request:
    types:
      - closed
    branches:
      - main

jobs:
  addpr:
    name: Add PR to project

    # PR must must be merged, not closed
    if: github.event.pull_request.merged

    runs-on: ubuntu-20.04

    steps:
      - name: Add PR to project board
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          projectid=$(gh api "repos/${{ github.repository }}/projects" \
              -H "Accept: application/vnd.github.inertia-preview+json" \
              | jq '.[] | select(.name == "Test").id')

          columnid=$(gh api "projects/$projectid/columns" \
              -H "Accept: application/vnd.github.inertia-preview+json" \
              | jq '.[] | select(.name == "Merged PRs").id')

          gh api "projects/columns/$columnid/cards" \
              -H "Accept: application/vnd.github.inertia-preview+json" \
              -F "content_type=PullRequest" \
              -F "content_id=${{ github.event.pull_request.id }}"

For reference and to see how "closed with unmerged commits" and "properly merged" are told apart, see the docs for the pull_request payload object.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • Thanks! If I'm following correctly, I would then be able to manipulate the PR object to update the 'project' field? – myshortfriend Jan 11 '21 at 20:27
  • @myshortfriend No, you'd have to make an API call to update the project to add that PR, but at a glance, I don't see an endpoint that lets you do that in the [projects API docs](https://git.io/Jteh4) or the [pull request API docs](https://docs.github.com/en/free-pro-team@latest/rest/reference/pulls). There is some [built-in automation](https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/configuring-automation-for-project-boards) for project boards, but it only works for PRs already in the project; I'm not sure if what you're trying to do is currently possible. – Benjamin W. Jan 11 '21 at 20:53
  • Thanks for checking. I think I might be able to use the [create a project card](https://stackoverflow.com/questions/57024087/github-api-how-to-move-an-issue-to-a-project) API to get this to work. – myshortfriend Jan 11 '21 at 20:56
  • @myshortfriend I think you're right, I was just looking at that. I've been using the GitHub CLI to make API calls out of GitHub Actions, it's pretty convenient, see for example the `gh api` calls in [this action](https://github.com/bewuethr/release-tracker-action/blob/master/tagupdater). – Benjamin W. Jan 11 '21 at 21:05
  • @myshortfriend Thanks for that pointer to the answer about adding a PR in a card, the API docs aren't very clear about this. I've extended my answer to a full workflow that adds closed PRs to a project board now. – Benjamin W. Jan 12 '21 at 22:50