diff --git a/.github/workflows/check-base-branch.yml b/.github/workflows/check-base-branch.yml index 1a497f2..6d056f2 100644 --- a/.github/workflows/check-base-branch.yml +++ b/.github/workflows/check-base-branch.yml @@ -8,6 +8,11 @@ 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' @@ -15,17 +20,44 @@ on: 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. " echo "You can add the label '${{ inputs.skip-check-label }}'. " exit 1 fi