4

I have a GitHub Actions workflow which is triggered only on push to the master (aka main) branch.

on:
  push:
    branches:
      - master

I only push to master via merging GitHub pull requests, not via directly committing changes to master and pushing them.

Within the steps of the job in this workflow, I would like to reference the pull request which was merged to lead to the merge commit which was pushed to master. What is the simplest way to achieve this?

I've tried to find such a reference in the GitHub Actions github context object, and the most promising property I found was github.event. In my case, this would be the push event, but within this event object I can find no property that will lead me to the pull request that led to the pushed (merge) commit.

Is there some context or webhook object property I have not discovered which would hold this information? If not, how else can I uncover it? If possible, I would prefer to avoid using 3rd party GitHub actions, from a security perspective.

Anders Rabo Thorbeck
  • 1,126
  • 3
  • 18
  • 28
  • You've got the `commit id` (`github.event.commits[0].id`) or `sha` from the push github context, if you add `/commits/` to the repo url, this should redirect you to the PR (as explained [here](https://stackoverflow.com/questions/17818167/find-a-pull-request-on-github-where-a-commit-was-originally-created)). – GuiFalourd Mar 07 '22 at 15:00
  • 2
    Thank you @GuiFalourd, but I'm trying to link directly to the pull request, to avoid people always having to perform the manual action of navigating from the commit to the PR. A PR link in a GitHub comment is also automatically enriched with the PR name, so you don't even have to follow the link to see what the PR is about; a link to the commit won't give that. – Anders Rabo Thorbeck Mar 07 '22 at 17:37
  • Would it work to trigger on pull request merges rather than on master? e.g. the opening post here summarizes how to do that: https://github.community/t/trigger-workflow-only-on-pull-request-merge/17359 – TWiStErRob Jun 11 '22 at 14:56

2 Answers2

0

yes, I found a way

not very pretty, but it works by using Github's GraphQL API

this is my get_pr_labels.sh script

#!/bin/bash
# query github's GraphQL endpoint for pull request labels using one of its commits' SHA

usage () {
  echo "script usage: $(basename $0) -t <token> -u <endpoint URL> -c <commit sha (long or short)> [-h]" >&2
}

while getopts 't:u:c:h' OPTION; do
  case "$OPTION" in
    t) TOKEN="$OPTARG" ;;
    u) URL="$OPTARG" ;;
    c) COMMIT="$OPTARG" ;;
    h) usage; exit 0 ;;
    ?) usage; exit 1 ;;
  esac
done

make_query() {
  jq -n --arg query "$(echo """
query {
  repository(owner: \"${GITHUB_REPOSITORY_OWNER}\", name: \"${GITHUB_REPOSITORY/*\//}\") {
    object(expression: \"${COMMIT}\") {
      ... on Commit {
        url
        message
        associatedPullRequests(first: 1) {
          nodes {
            title
            number
            url
            labels(first: 10) {
              edges {
                node {
                  name
                }
              }
            }
          }
        }
      }
    }
  }
}
""")" '{query: $query}'
}

# query for all the PRs that added the commit $COMMIT, this should only be triggered when a PR is merged, so we expect only one PR
RESPONSE=$(curl -H "Authorization: bearer $TOKEN" -X POST --data "$(make_query)" $URL)
PR=$(echo $RESPONSE | jq '.data.repository.object.associatedPullRequests.nodes[0]') # access the relevant part of the response
LABELS=$(echo $PR | jq '.labels.edges[].node.name') # grab only the names
echo $LABELS # print the labels for the caller

And I call it like this from my action (all the environment variables used are availble)

PR_LABELS=$(./get_pr_labels.sh -t ${{ secrets.GITHUB_TOKEN }} -u ${GITHUB_GRAPHQL_URL} -c ${GITHUB_SHA})

I use it to get the labels of the merged PR, but you can use jq to get many other parameters. see response docs

BTW github.event.pull_request is only available when the trigger is a pull_request and not a push

Nimrod Morag
  • 938
  • 9
  • 20
-2

This one would give you the pull request link

${{ github.event.pull_request._links.html.href }}
pavan
  • 1,825
  • 1
  • 11
  • 10