|
| 1 | +description: | |
| 2 | + Gets and sets the following environment variables: |
| 3 | + * `GITHUB_PR_BASE_BRANCH` - The base branch for the PR. |
| 4 | + * `GITHUB_PR_NUMBER` - The number of the PR. |
| 5 | + * `GITHUB_PR_TITLE` - The title of the PR. |
| 6 | + * `GITHUB_PR_COMMIT_MESSAGE` - The current commit's message. (Optional) |
| 7 | + * `GITHUB_PR_AUTHOR_USERNAME` - The PR author's username. |
| 8 | + * `GITHUB_PR_AUTHOR_NAME` - The PR author's name. (Optional) |
| 9 | + * `GITHUB_PR_AUTHOR_EMAIL` - The PR author's email address. (Optional) |
| 10 | + Requires `GITHUB_EMAIL` and `GITHUB_PASSWORD` to be set as environment variables. |
| 11 | +parameters: |
| 12 | + get_pr_author_email: |
| 13 | + description: | |
| 14 | + If true, also sets GITHUB_PR_AUTHOR_EMAIL. This requires an additional API call. |
| 15 | + type: boolean |
| 16 | + default: false |
| 17 | + get_pr_author_name: |
| 18 | + description: | |
| 19 | + If true, also sets GITHUB_PR_AUTHOR_NAME. This requires an additional API call. |
| 20 | + type: boolean |
| 21 | + default: false |
| 22 | + get_commit_message: |
| 23 | + description: | |
| 24 | + If true, also sets GITHUB_PR_COMMIT_MESSAGE. This requires an additional API call. |
| 25 | + type: boolean |
| 26 | + default: false |
| 27 | +steps: |
| 28 | + - run: |
| 29 | + when: always |
| 30 | + name: Get PR information |
| 31 | + command: | |
| 32 | + # Check `jq` dependency |
| 33 | + if ! (command -v jq >/dev/null 2>&1); then |
| 34 | + echo "This command requires jq to be installed" |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | +
|
| 38 | + PR_NUMBER=$(echo "$CIRCLE_PULL_REQUEST" | sed "s/.*\/pull\///") |
| 39 | + echo "PR_NUMBER: $PR_NUMBER" |
| 40 | + echo "export GITHUB_PR_NUMBER=$PR_NUMBER" >> $BASH_ENV |
| 41 | +
|
| 42 | + API_GITHUB="https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME" |
| 43 | + PR_REQUEST_URL="$API_GITHUB/pulls/$PR_NUMBER" |
| 44 | + PR_RESPONSE=$(curl --user "${GITHUB_USERNAME}:${GITHUB_PASSWORD}" "$PR_REQUEST_URL") |
| 45 | +
|
| 46 | + PR_TITLE=$(echo $PR_RESPONSE | jq -e '.title' | tr -d '"') |
| 47 | + echo "PR_TITLE: $PR_TITLE" |
| 48 | + echo "export GITHUB_PR_TITLE='$PR_TITLE'" >> $BASH_ENV |
| 49 | +
|
| 50 | + PR_BASE_BRANCH=$(echo $PR_RESPONSE | jq -e '.base.ref' | tr -d '"') |
| 51 | + echo "PR_BASE_BRANCH: $PR_BASE_BRANCH" |
| 52 | + echo "export GITHUB_PR_BASE_BRANCH='$PR_BASE_BRANCH'" >> $BASH_ENV |
| 53 | +
|
| 54 | + PR_AUTHOR_USERNAME=$(echo $PR_RESPONSE | jq -e '.user.login' | tr -d '"') |
| 55 | + echo "PR_AUTHOR_USERNAME: $PR_AUTHOR_USERNAME" |
| 56 | + echo "export GITHUB_PR_AUTHOR_USERNAME='$PR_AUTHOR_USERNAME'" >> $BASH_ENV |
| 57 | +
|
| 58 | + if [[ << parameters.get_pr_author_email >> == true || << parameters.get_pr_author_name >> ]]; then |
| 59 | + # We need to use the email address associated with the merge_commit_sha since |
| 60 | + # CIRCLE_SHA1 may have been authored by someone who is not the PR author. |
| 61 | + # Sadly, PR_RESPONSE doesn't include the email associated with the merge_commit_sha. |
| 62 | + # So we have to get that from the commit information. |
| 63 | +
|
| 64 | + PR_MERGE_COMMIT_SHA=$(echo $PR_RESPONSE | jq -e '.merge_commit_sha' | tr -d '"') |
| 65 | + COMMIT_REQUEST_URL="$API_GITHUB/commits/$PR_MERGE_COMMIT_SHA" |
| 66 | + COMMIT_RESPONSE=$(curl --user "${GITHUB_USERNAME}:${GITHUB_PASSWORD}" "$COMMIT_REQUEST_URL") |
| 67 | + fi |
| 68 | +
|
| 69 | + <<# parameters.get_pr_author_email >> |
| 70 | + PR_AUTHOR_EMAIL=$(echo $COMMIT_RESPONSE | jq -e '.commit.author.email' | tr -d '"') |
| 71 | + echo "PR_AUTHOR_EMAIL: $PR_AUTHOR_EMAIL" |
| 72 | + echo "export GITHUB_PR_AUTHOR_EMAIL='$PR_AUTHOR_EMAIL'" >> $BASH_ENV |
| 73 | + <</ parameters.get_pr_author_email >> |
| 74 | +
|
| 75 | + <<# parameters.get_pr_author_name >> |
| 76 | + PR_AUTHOR_NAME=$(echo $COMMIT_RESPONSE | jq -e '.commit.author.name' | tr -d '"') |
| 77 | + echo "PR_AUTHOR_NAME: $PR_AUTHOR_NAME" |
| 78 | + echo "export GITHUB_PR_AUTHOR_NAME='$PR_AUTHOR_NAME'" >> $BASH_ENV |
| 79 | + <</ parameters.get_pr_author_name >> |
| 80 | +
|
| 81 | + <<# parameters.get_commit_message >> |
| 82 | + COMMIT_REQUEST_URL="$API_GITHUB/commits/$CIRCLE_SHA1" |
| 83 | + COMMIT_RESPONSE=$(curl --user "${GITHUB_USERNAME}:${GITHUB_PASSWORD}" "$COMMIT_REQUEST_URL") |
| 84 | + PR_COMMIT_MESSAGE=$(echo $COMMIT_RESPONSE | jq -e '.commit.message' | tr -d '"') |
| 85 | + echo "PR_COMMIT_MESSAGE: $PR_COMMIT_MESSAGE" |
| 86 | + echo "export GITHUB_PR_COMMIT_MESSAGE='$PR_COMMIT_MESSAGE'" >> $BASH_ENV |
| 87 | + <</ parameters.get_commit_message >> |
0 commit comments