Skip to content

Commit

Permalink
Get more PR info and do better Slack messaging (#5)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
CelestineKao authored Jun 4, 2020
1 parent 521a8b7 commit d7d1feb
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 89 deletions.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
- ghpr/build-prospective-branch
- ghpr/slack-pr-author:
message: ":tada: Tests passed!"
get_slack_user_by: meseeks
color: "#f2c744"

publish:
machine: true
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
[![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)
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)

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.
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.

Additional features:

Expand All @@ -16,6 +14,14 @@ The commands in the orb will expose 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.
* `GITHUB_PR_AUTHOR_USERNAME` - The PR author's username.
* `GITHUB_PR_AUTHOR_NAME` - The PR author's name.
* `GITHUB_PR_AUTHOR_EMAIL` - The PR author's email address.

All these commands will work out-of-the-box in jobs using the
[`machine` executor](https://circleci.com/docs/2.0/executor-types/#using-machine).

## Getting Started

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

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)
to look up the user in a Slack workspace. Therefore, to receive notifications, committers
should set their `user.email` in their `git config` to be the same email associated with their Slack account.
The bot user will need at least the following bot token scopes:

* [`chat:write`](https://api.slack.com/scopes/chat:write)
* [`chat:write.public`](https://api.slack.com/scopes/chat:write.public)
* [`users:read`](https://api.slack.com/scopes/users:read)
* [`users.profile:read`](https://api.slack.com/scopes/users.profile:read)
* [`users:read.email`](https://api.slack.com/scopes/users:read.email)
* [`im:write`](https://api.slack.com/scopes/im:write)

Messages show as being sent by the user associated with the `SLACK_OAUTH_TOKEN`.

#### Example notification
Expand Down
5 changes: 4 additions & 1 deletion src/@orb.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
version: 2.1
description: Bundle of utils for GitHub pull requests
description: |
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
examples:
test-pull-request:
Expand All @@ -20,3 +21,5 @@ examples:
when: on_fail
- ghpr/slack-pr-author:
message: ":tada: Tests passed!"
get_slack_user_by: meseeks
color: "#1CBF43"
6 changes: 3 additions & 3 deletions src/commands/build-prospective-branch.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
description: |
Builds the prospective merge branch by merging the head branch into the pull request's base branch.
Requires `GITHUB_EMAIL` and `GITHUB_USERNAME` to be set as environment variables.
Requires `GITHUB_EMAIL`, `GITHUB_USERNAME`, and `GITHUB_PASSWORD` to be set as environment variables.
parameters:
force:
description: |
Expand All @@ -10,15 +10,15 @@ parameters:
default: false
steps:
- checkout
- get-base-branch
- get-pr-info
- run:
name: Build prospective merge branch
command: |
set +e
git config --global user.email "$GITHUB_EMAIL"
git config --global user.name "$GITHUB_USERNAME"
git fetch && git merge "origin/$GITHUB_PR_BASE_BRANCH" --no-edit
if [[ $? -ne 0 && "<< parameters.force >>" == "false" ]]; then
if [[ $? -ne 0 && << parameters.force >> == false ]]; then
echo "Failed to merge $GITHUB_PR_BASE_BRANCH into $CIRCLE_BRANCH"
exit 1
fi
18 changes: 0 additions & 18 deletions src/commands/get-base-branch.yml

This file was deleted.

87 changes: 87 additions & 0 deletions src/commands/get-pr-info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,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_EMAIL` and `GITHUB_PASSWORD` to be set as environment variables.
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 --user "${GITHUB_USERNAME}:${GITHUB_PASSWORD}" "$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 --user "${GITHUB_USERNAME}:${GITHUB_PASSWORD}" "$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 --user "${GITHUB_USERNAME}:${GITHUB_PASSWORD}" "$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 >>
8 changes: 0 additions & 8 deletions src/commands/get-pr-number.yml

This file was deleted.

Loading

0 comments on commit d7d1feb

Please sign in to comment.