From 97c8701a5a1c9e1b2439e8b7c513fd2372aa6956 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 1 Feb 2025 14:10:25 -0600 Subject: [PATCH] Add Use Labels To Block PR Merge as a GitHub Actions PR --- README.md | 3 +- .../use-labels-to-block-pr-merge.md | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 github-actions/use-labels-to-block-pr-merge.md diff --git a/README.md b/README.md index 17583b0..3950d65 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1581 TILs and counting..._ +_1582 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -407,6 +407,7 @@ See some of the other learning resources I work on: - [Disable A Workflow With The gh CLI](github-actions/disable-a-workflow-with-the-gh-cli.md) - [Reference An Encrypted Secret In An Action](github-actions/reference-an-encrypted-secret-in-an-action.md) - [Trigger A Workflow Via An API Call](github-actions/trigger-a-workflow-via-an-api-call.md) +- [Use Labels To Block PR Merge](github-actions/use-labels-to-block-pr-merge.md) ### Go diff --git a/github-actions/use-labels-to-block-pr-merge.md b/github-actions/use-labels-to-block-pr-merge.md new file mode 100644 index 0000000..ae411b9 --- /dev/null +++ b/github-actions/use-labels-to-block-pr-merge.md @@ -0,0 +1,41 @@ +# Use Labels To Block PR Merge + +Let's say our GitHub project has custom tags for both `no merge` and `wip` +(_work in progress_). Whenever either of those labels has been applied to a PR, +we want there to be a failed check so as to block the merge. This is useful to +ensure automated tools (as well as someone not looking closely enough) don't +merge a PR that isn't _ready to go_. + +This can be achieved with a basic GitHub Actions workflow that requires no +3rd-party actions. We can add the following as +`.github/workflows/block-labeled-prs.yml` in our project. + +```yaml +name: Block Labeled PR Merges + +on: + pull_request: + types: [labeled, unlabeled, opened, edited, synchronize] + +jobs: + prevent-merge: + if: ${{ contains(github.event.*.labels.*.name, 'no merge') || contains(github.event.*.labels.*.name, 'wip') }} + name: Prevent Merging + runs-on: ubuntu-latest + steps: + - name: Check for label + run: | + echo "Pull request label prevents merging." + echo "Labels: ${{ join(github.event.*.labels.*.name, ', ') }}" + echo "Remove the blocking label(s) to skip this check." + exit 1 +``` + +This workflow is run when a pull request is opened, when it is edited or +synchronized, and when a label change is made. The job `prevent-merge` sees if +any of the label names match `no merge` or `wip`. If so, we echo out some +details in the ubuntu container and then `exit 1` to fail the check. + +Shoutout to [Jesse Squire's +implementation](https://www.jessesquires.com/blog/2021/08/24/useful-label-based-github-actions-workflows/#updated-21-march-2022) +which I've heavily borrowed from here.