Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Written in Go, using Mage: https://magefile.org/
Code is loosely based on this example:
https://www.sethvargo.com/writing-github-actions-in-go/
Checks for exactly one of the specified labels, and fails if there are
none or more than one.
Future enhanced functionality could be to allow the client application
to also specify a set of labels none of which should be present, and
a set all of which should be present.
Two noteworthy things to improve in future commits:
There is a bug whereby a PR can show that the check has passed, but
still not allow the pull request to be merged. This is because GitHub
Actions create a separate new instance of the check for some different
event types, e.g. the
labeled
event creates a new check instanceseparate from the
push
event checks. So if we, for instance:push a commit when there is no required label
add the required label
then the
lableled
instance of the check will pass, but the push willstill show the initial failure.
The proposed fix for this (in a future commit) is to retrieve the GitHub
labels for the pull request via the GitHub API, instead of using the
labels in the GitHub Action's event JSON (which is a frozen snapshot of
the labels at the time of committing and therefore does not stay up to
date with subsequent label changes). Then if we rerun the failed
push
job after amending the labels, the check will pass.
The
github.CheckLabels()
method should return a boolean (along witha message) indicating whether the labels are valid or not for the pull
request, rather than returning an error. As per:
https://martinfowler.com/articles/replaceThrowWithNotification.html
If this method returns false, we'll then return an error in the Mage
command (which will ensure a non-zero exit code), so that the GitHub
Action fails the check.