From 83c0feaed8870b867550f9c3bae698b86f2db8c5 Mon Sep 17 00:00:00 2001 From: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com> Date: Wed, 14 Jun 2023 15:03:40 +0100 Subject: [PATCH 1/5] Extend base branch workflow with a set job --- .github/workflows/check-base-branch.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/.github/workflows/check-base-branch.yml b/.github/workflows/check-base-branch.yml index 1a497f2..0caccad 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,8 +20,26 @@ on: required: false jobs: + maybe-set: + 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 + needs: [maybe-set] + if: ${{ always() }} runs-on: ubuntu-latest steps: - name: Check Base Ref is Development Branch From 1397b43beaedc99bc15adb4bcdeca1be793894ab Mon Sep 17 00:00:00 2001 From: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com> Date: Wed, 14 Jun 2023 15:14:17 +0100 Subject: [PATCH 2/5] get base ref from API --- .github/workflows/check-base-branch.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check-base-branch.yml b/.github/workflows/check-base-branch.yml index 0caccad..331289f 100644 --- a/.github/workflows/check-base-branch.yml +++ b/.github/workflows/check-base-branch.yml @@ -45,7 +45,13 @@ jobs: - name: Check Base Ref is Development Branch if: ${{ !contains(github.event.*.labels.*.name, inputs.skip-check-label) }} 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 '.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. " From c68faea65e0842eb42b18d8e39ba5796f2d4fc3e Mon Sep 17 00:00:00 2001 From: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com> Date: Wed, 14 Jun 2023 15:15:40 +0100 Subject: [PATCH 3/5] Add GH_TOKEN env var --- .github/workflows/check-base-branch.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/check-base-branch.yml b/.github/workflows/check-base-branch.yml index 331289f..9a59797 100644 --- a/.github/workflows/check-base-branch.yml +++ b/.github/workflows/check-base-branch.yml @@ -44,6 +44,8 @@ jobs: steps: - name: Check Base Ref is Development Branch if: ${{ !contains(github.event.*.labels.*.name, inputs.skip-check-label) }} + env: + GH_TOKEN: ${{ github.token }} run: | base_ref=$(gh api \ -H "Accept: application/vnd.github+json" \ From 6894c2284a4e2975be70730b52a4f70906b963a5 Mon Sep 17 00:00:00 2001 From: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com> Date: Wed, 14 Jun 2023 15:19:30 +0100 Subject: [PATCH 4/5] use raw output --- .github/workflows/check-base-branch.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check-base-branch.yml b/.github/workflows/check-base-branch.yml index 9a59797..b89a48f 100644 --- a/.github/workflows/check-base-branch.yml +++ b/.github/workflows/check-base-branch.yml @@ -51,12 +51,12 @@ jobs: -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ /repos/${{ github.repository }}/pulls/${{ github.event.number }} \ - | jq '.base.ref' + | 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 From 4e3a415d959678be6b44007c57a09205545f96fc Mon Sep 17 00:00:00 2001 From: Oliver Holworthy <1216955+oliverholworthy@users.noreply.github.com> Date: Wed, 14 Jun 2023 15:21:49 +0100 Subject: [PATCH 5/5] Update job names --- .github/workflows/check-base-branch.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check-base-branch.yml b/.github/workflows/check-base-branch.yml index b89a48f..6d056f2 100644 --- a/.github/workflows/check-base-branch.yml +++ b/.github/workflows/check-base-branch.yml @@ -21,6 +21,7 @@ on: jobs: maybe-set: + name: Maybe Set Base Branch if: github.base_ref == inputs.stable-branch-name runs-on: ubuntu-latest permissions: @@ -37,7 +38,7 @@ jobs: /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