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