Giter Club home page Giter Club logo

circleci-orb-ghpr's Introduction

CircleCI Orb: GitHub PR

CircleCI Orb Version License

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:

  • Posting PR comments on success/failure/always
  • Sending Slack notifications to PR authors

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.

Getting Started

To use this orb, there are a few environment variables to set. These can be set in a Context or your Project's Environment Variables.

Setting up a GitHub service user

It's recommended to create a service GitHub user with at least Read access to your repository and set the following environment variables:

  • GITHUB_USERNAME - Username of the service user that has read/write permissions to the repo.
  • GITHUB_EMAIL - Email of the service user.
  • GITHUB_TOKEN - API token for the GitHub user

Enabling Slack Notifications

To use the Slack related orb commands, create or use an existing workspace bot and set the following environment variables:

  • SLACK_OAUTH_TOKEN - OAuth token for the Slack bot that will be used to send Slack messages.

The bot user will need at least the following bot token scopes:

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

Example notification

Screen Shot 2020-06-03 at 21 55 38

circleci-orb-ghpr's People

Contributors

celestinekao avatar ns-bdesimone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

circleci-orb-ghpr's Issues

get-pr-info exits with 1 status for unknown reason

I installed this orb recently and so far I'm unable to get the ghpr/get-pr-info to complete correctly. The output is very similar to what is seen in #12 but I am running the build off a branch with a PR in this case. It's hard to say what the exact problem is, but it would be nice if there could at least be some additional console logging in the event that there is an error. As far as I can tell I have everything configured correctly on my end.

Screen Shot 2021-06-11 at 12 44 26 PM

Multiple single quotes still break the runner

Given a PR title of Update 'foo', the runner fails with the following error:

/tmp/.bash_env-613efb15de489f033baf34e7-0-build: line 4: unexpected EOF while looking for matching `''
/tmp/.bash_env-613efb15de489f033baf34e7-0-build: line 5: syntax error: unexpected end of file

While this is the same problem as #8, #8 is no longer reproducible as the merged fix in #10 works for PRs containing 1 single quote. For any PR containing 2 or more, we have the same problem.

I've brought up a fix at #15.

Single quote in PR title breaks the get-pr-info variables

Scenario

Given PR title:

ASDF-111 don't use single quotes in PR title

When using step ghpr/get-pr-info and accessing $GITHUB_PR_BASE_BRANCH variable
Then variable is an empty line and error message is printed:

/tmp/.bash_env-5ff85b683890bc490b71ca94-0-build: line 4: unexpected EOF while looking for matching `''
/tmp/.bash_env-5ff85b683890bc490b71ca94-0-build: line 5: syntax error: unexpected end of file

Details

I've noticed that exporting variables in get-pr-info command is not escaping the contents:

PR_TITLE=$(echo $PR_RESPONSE | jq -e '.title' | tr -d '"')
echo "PR_TITLE: $PR_TITLE"
echo "export GITHUB_PR_TITLE='$PR_TITLE'" >> $BASH_ENV

In my case this resulted in a corrupted env file:

echo $BASH_ENV
/tmp/.bash_env-5ff86be9b2b77a7bed582e27-0-build
cat $BASH_ENV
export GITHUB_PR_NUMBER=1568
export GITHUB_PR_TITLE='ASDF-111 don't use single quotes in PR title'
export GITHUB_PR_BASE_BRANCH='develop'
export GITHUB_PR_AUTHOR_USERNAME='jakub'

I think single quotes should be escaped to avoid such issues.

Command get-pr-info exits w/ status 1 if NOT in PR

If you attempt to run ghpr/get-pr-info on a branch w/o a PR, the Job fails exit status 1. This results in Workflow failures and means that ANY script using GHPR can NOT be run on a branch w/o a PR.

I am curious if there is a use case that desires this behavior or if it should instead return 0. For our use case, we are trying to determine the merge into branch and it is ok if that isn't present yet as long as we can set the ENV accordingly.

I suppose the easiest thing to do would be to add the ** pass_on_fail** option that is available to slack-pr-author.

Screen Shot 2021-06-02 at 4 46 19 PM

Does this rerun CI when the base branch is changed?

This orb looks really helpful for avoiding PRs that can't be merged.

However, just to confirm: I assume that there's no easy way to proactively re-run CI when the base branch is updated, right? So if I push my feature branch and CI succeeds, then somebody pushes the base branch with a conflicting change, my PR will still look green, right?

Conditional isn't behaving as expected

https://github.com/NarrativeScience/circleci-orb-ghpr/blob/67bcfe9e201cd79470d7d775f0dc7c4669a30a44/src/commands/get-pr-info.yml#L58

isn't doing the right thing.

If I just use:

- ghpr/get-pr-info

This resolves as:

#!/bin/bash -eo pipefail
# 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 [[ false == true || false ]]; 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

The last section always runs - even though it looks like it shouldn't.

e.g.

circleci@63cb6556884b:~$ if [[ false == true || false ]]; then echo hi; fi
hi

It looks like the second section is being parsed as "is this string, which happens to be false, non-empty"

Possibly change that line to:

if << parameters.get_pr_author_email >> || << parameters.get_pr_author_name >>;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.