Skip to content

Commit d7d1feb

Browse files
authored
Get more PR info and do better Slack messaging (#5)
* Add get-pr-info command * testing * iterating * iterating * iterating * iterate * iterate * iterate * iterate * iterate * iterate * iterate * foo * bar * baz * baz * baz * baz * baz * baz * baz * i should go to sleep * ... * ... * ... * ... * ... * ... * ... * ... * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * gkwb * update description and stuff * foo * foo * foo * foo * foo * Fixing bugs * clean up and documentation * prettify
1 parent 521a8b7 commit d7d1feb

File tree

8 files changed

+279
-89
lines changed

8 files changed

+279
-89
lines changed

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jobs:
2323
- ghpr/build-prospective-branch
2424
- ghpr/slack-pr-author:
2525
message: ":tada: Tests passed!"
26+
get_slack_user_by: meseeks
27+
color: "#f2c744"
2628

2729
publish:
2830
machine: true

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
[![CircleCI Orb Version](https://img.shields.io/badge/endpoint.svg?url=https://badges.circleci.io/orb/narrativescience/ghpr)](https://circleci.com/orbs/registry/orb/narrativescience/ghpr)
44
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
55

6-
Set of git utilities to manage GitHub Pull Requests in CI.
7-
This orb was created to address the need to simulate the result of merging the head branch
8-
into a PR's target base branch.
6+
Set of git utilities to manage GitHub Pull Requests in CI. This orb was created to address the need to simulate the result of merging the head branch into a PR's target base branch.
97

108
Additional features:
119

@@ -16,6 +14,14 @@ The commands in the orb will expose the following environment variables:
1614

1715
* `GITHUB_PR_BASE_BRANCH` - The base branch for the PR.
1816
* `GITHUB_PR_NUMBER` - The number of the PR.
17+
* `GITHUB_PR_TITLE` - The title of the PR.
18+
* `GITHUB_PR_COMMIT_MESSAGE` - The current commit's message.
19+
* `GITHUB_PR_AUTHOR_USERNAME` - The PR author's username.
20+
* `GITHUB_PR_AUTHOR_NAME` - The PR author's name.
21+
* `GITHUB_PR_AUTHOR_EMAIL` - The PR author's email address.
22+
23+
All these commands will work out-of-the-box in jobs using the
24+
[`machine` executor](https://circleci.com/docs/2.0/executor-types/#using-machine).
1925

2026
## Getting Started
2127

@@ -41,9 +47,15 @@ and set the following environment variables:
4147
* `SLACK_OAUTH_TOKEN` - [OAuth token](https://api.slack.com/docs/token-types#bot) for the Slack
4248
bot that will be used to send Slack messages.
4349

44-
This orb uses the [email associated with commits](https://help.github.com/en/github/setting-up-and-managing-your-github-user-account/setting-your-commit-email-address)
45-
to look up the user in a Slack workspace. Therefore, to receive notifications, committers
46-
should set their `user.email` in their `git config` to be the same email associated with their Slack account.
50+
The bot user will need at least the following bot token scopes:
51+
52+
* [`chat:write`](https://api.slack.com/scopes/chat:write)
53+
* [`chat:write.public`](https://api.slack.com/scopes/chat:write.public)
54+
* [`users:read`](https://api.slack.com/scopes/users:read)
55+
* [`users.profile:read`](https://api.slack.com/scopes/users.profile:read)
56+
* [`users:read.email`](https://api.slack.com/scopes/users:read.email)
57+
* [`im:write`](https://api.slack.com/scopes/im:write)
58+
4759
Messages show as being sent by the user associated with the `SLACK_OAUTH_TOKEN`.
4860

4961
#### Example notification

src/@orb.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
version: 2.1
2-
description: Bundle of utils for GitHub pull requests
2+
description: |
3+
Set of git utilities to manage GitHub Pull Requests in CI. This orb was created to address the need to simulate the result of merging the head branch into a PR's target base branch
34
45
examples:
56
test-pull-request:
@@ -20,3 +21,5 @@ examples:
2021
when: on_fail
2122
- ghpr/slack-pr-author:
2223
message: ":tada: Tests passed!"
24+
get_slack_user_by: meseeks
25+
color: "#1CBF43"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
description: |
22
Builds the prospective merge branch by merging the head branch into the pull request's base branch.
3-
Requires `GITHUB_EMAIL` and `GITHUB_USERNAME` to be set as environment variables.
3+
Requires `GITHUB_EMAIL`, `GITHUB_USERNAME`, and `GITHUB_PASSWORD` to be set as environment variables.
44
parameters:
55
force:
66
description: |
@@ -10,15 +10,15 @@ parameters:
1010
default: false
1111
steps:
1212
- checkout
13-
- get-base-branch
13+
- get-pr-info
1414
- run:
1515
name: Build prospective merge branch
1616
command: |
1717
set +e
1818
git config --global user.email "$GITHUB_EMAIL"
1919
git config --global user.name "$GITHUB_USERNAME"
2020
git fetch && git merge "origin/$GITHUB_PR_BASE_BRANCH" --no-edit
21-
if [[ $? -ne 0 && "<< parameters.force >>" == "false" ]]; then
21+
if [[ $? -ne 0 && << parameters.force >> == false ]]; then
2222
echo "Failed to merge $GITHUB_PR_BASE_BRANCH into $CIRCLE_BRANCH"
2323
exit 1
2424
fi

src/commands/get-base-branch.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/commands/get-pr-info.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 >>

src/commands/get-pr-number.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)