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

Maybe set base branch #20

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
40 changes: 36 additions & 4 deletions .github/workflows/check-base-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,56 @@ on:
required: false
type: string
default: 'main'
stable-branch-name:
description: 'The name of the stable branch'
required: false
type: string
default: 'stable'
skip-check-label:
description: 'The label used to skip this check'
default: 'skip-base-branch-check'
type: string
required: false

jobs:
maybe-set:
name: Maybe Set Base Branch
if: github.base_ref == inputs.stable-branch-name
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Set the pull request base ref
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api \
--method PATCH \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/pulls/${{ github.event.number }} \
-f base='${{ inputs.development-branch-name }}'
check:
name: Require Development Branch
name: Require Development Base Branch
needs: [maybe-set]
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- name: Check Base Ref is Development Branch
if: ${{ !contains(github.event.*.labels.*.name, inputs.skip-check-label) }}
env:
GH_TOKEN: ${{ github.token }}
run: |
if [[ ${{ github.base_ref }} != ${{ inputs.development-branch-name }} ]]
base_ref=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/pulls/${{ github.event.number }} \
| jq --raw-output '.base.ref'
)
if [[ "${base_ref}" != ${{ inputs.development-branch-name }} ]]
then
echo "::error::Pull Request base ref must be the development branch: '${{ inputs.development-branch-name }}'. Found base ref: '${{ github.base_ref }}' ."
echo "If you're sure you need to merge this change into '${{ github.base_ref }}' instead. "
echo "::error::Pull Request base ref must be the development branch: '${{ inputs.development-branch-name }}'. Found base ref: '${base_ref}' ."
echo "If you're sure you need to merge this change into '${base_ref}' instead. "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this include instructions for what to do in that case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the instruction is on the next line 'you can add the label 'skip-base-branch-check' (or do you have something else in mind). Can move that up to the same line if that's clearer?

echo "You can add the label '${{ inputs.skip-check-label }}'. "
exit 1
fi