-
Notifications
You must be signed in to change notification settings - Fork 0
60 lines (55 loc) · 2.73 KB
/
release-label-validation.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
name: Validate release labels on PR
on:
workflow_call:
inputs:
on_base_ref:
required: false
type: string
default: main
jobs:
validate_release_labels:
name: Run validation
runs-on: ubuntu-latest
# This workflow does not work with the Renovate Bot, so it's disabled for those branches
# It prevents the automerge because the workflow is run for the branch (not PR) and fails
# This should not be an issue because the Renovate Bot automatically adds the labels
if: |
github.event_name == 'pull_request' &&
github.base_ref == inputs.on_base_ref &&
contains(fromJSON('["opened", "edited", "synchronize", "reopened", "labeled", "unlabeled"]'), github.event.action) &&
!startsWith(github.head_ref, 'renovate/')
steps:
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: determine labels
id: determine_release_labels
shell: python
run: |
import os
import json
allowed_release_labels = ['major', 'minor', 'patch']
pr_labels = json.loads("""${{ toJSON(github.event.pull_request.labels.*.name) }}""")
is_chore = 'chore' in pr_labels
release_labels_on_pr = list(filter(lambda label: label in allowed_release_labels, pr_labels))
release_labels_count = len(release_labels_on_pr)
labels_are_valid = release_labels_count == 1 or (release_labels_count == 0 and is_chore)
GITHUB_OUTPUT = os.getenv('GITHUB_OUTPUT')
if GITHUB_OUTPUT:
with open(GITHUB_OUTPUT, 'a') as github_output:
github_output.write('labels_are_valid={labels_are_valid}\n'.format(labels_are_valid = labels_are_valid))
github_output.write('release_labels_count={release_labels_count}\n'.format(release_labels_count = release_labels_count))
- name: fail on empty labels
if: ${{ steps.determine_release_labels.outputs.labels_are_valid != 'True' && steps.determine_release_labels.outputs.release_labels_count == 0 }}
run: |
echo "### :x: a release label (major, minor, patch or chore) must be set" >> $GITHUB_STEP_SUMMARY
exit 1
- name: fail on multiple labels
if: ${{ steps.determine_release_labels.outputs.labels_are_valid != 'True' && steps.determine_release_labels.outputs.release_labels_count > 1 }}
run: |
echo "### :x: pr must either be marked as chore or contain exactly one release label (major, minor, patch) " >> $GITHUB_STEP_SUMMARY
exit 1
- name: report correct label
if: steps.determine_release_labels.outputs.labels_are_valid
run: |
echo "### :white_check_mark: pr is marked with valid label" >> $GITHUB_STEP_SUMMARY