Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement Github CLI "pr merge" command #36

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/examples/pr-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
description: >
Use the GitHub CLI merge pull request automatically. Usually it can be a pull request from a bot like Dependabot.
In this example, on every Dependabot pull request, we run tests and merge the PR if tests pass
Add a Context containing your GITHUB_TOKEN and GITHUB_HOSTNAME (optional) or set a project-level environment variable.
usage:
version: 2.1
orbs:
gh: circleci/github-cli@2.2.1
KyleTryon marked this conversation as resolved.
Show resolved Hide resolved
workflows:
build_and_test:
jobs:
- build:
- test:
requires:
- build
- gh/pr-merge:
context:
- GITHUB_CREDS
additional-args: --rebase --delete-branch
filters:
branches:
only: /^dependabot.*/
requires:
- test
47 changes: 47 additions & 0 deletions src/jobs/pr-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
description: >
Merge pull request to target branch.
See more at https://cli.github.com/manual/gh_pr_merge.

docker:
- image: cimg/base:stable

resource_class: small

parameters:
branch:
type: string
default: ""
description: "Enter PR number, branch or URL to merge. Without an argument, the pull request that belongs to the current branch is selected"
additional_args:
type: string
default: ""
description: "Enter any additional arguments to pass to the `gh pr merge` command (e.g. --squash)"
hostname:
type: string
default: "github.com"
description: >
Specify the hostname of the GitHub instance to authenticate with.
Set this to connect to your GitHub Enterprise instance.
token:
type: env_var_name
default: "GITHUB_TOKEN"
description: >
Enter the name of the environment variable containing the GitHub Personal Access token to be used for authentication.
It is recommended for CI processes that you create a "machine" user on GitHub.com with the needed permissions, rather than using your own.
version:
type: string
default: "2.20.2"
description: Specify the full semver versioned tag to use for the GitHub CLI installation.

steps:
- install:
version: <<parameters.version>>
- clone
- run:
name: "Merging PR to target branch"
environment:
ORB_EVAL_ADDITIONAL_ARGS: <<parameters.additional_args>>
ORB_EVAL_BRANCH: <<parameters.branch>>
ORB_EVAL_HOSTNAME: <<parameters.hostname>>
ORB_ENV_TOKEN: <<parameters.token>>
command: <<include(scripts/pr-merge.sh)>>
17 changes: 17 additions & 0 deletions src/scripts/pr-merge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
branch="$(eval printf '%s' "$ORB_EVAL_BRANCH")"
additional_args="$(eval printf '%s\\n' "$ORB_EVAL_ADDITIONAL_ARGS")"
hostname="$(eval printf '%s' "$ORB_EVAL_HOSTNAME")"
token="${!ORB_ENV_TOKEN}"

[ -z "$token" ] && { >&2 printf '%s\n' "A GitHub token must be supplied" "Check the \"token\" parameter."; exit 1; }
printf '%s\n' "export GITHUB_TOKEN=$token" >> "$BASH_ENV"
[ -n "$hostname" ] && printf '%s\n' "export GITHUB_HOSTNAME=$hostname" >> "$BASH_ENV"

set -x
# shellcheck disable=SC2086
gh pr merge \
$branch
--repo "$(git config --get remote.origin.url)" \
$additional_args
set +x