forked from AliceO2Group/O2Physics
-
Notifications
You must be signed in to change notification settings - Fork 0
122 lines (121 loc) · 5.02 KB
/
labeler.yml
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
name: "Pull Request Labeler"
'on':
pull_request_target:
types: [opened, synchronize, reopened, edited]
permissions: read-all
jobs:
labeler:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
outputs:
labels: ${{ steps.labeler.outputs.all-labels }}
steps:
- name: Label the PR
id: labeler
uses: actions/labeler@v5
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
sync-labels: true
title-prefix-checker:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
needs: labeler
steps:
- name: Check the PR title prefix
id: check-prefix
env:
title: ${{ github.event.pull_request.title }}
labels: ${{ needs.labeler.outputs.labels }}
shell: python
run: |
import os
import re
import sys
title = os.environ['title']
labels = os.environ['labels']
tags = {
"infrastructure": "Infrastructure",
"common": "Common",
"alice3": "ALICE3",
"pwgcf": "PWGCF",
"pwgdq": "PWGDQ",
"pwgem": "PWGEM",
"pwghf": "PWGHF",
"pwgje": "PWGJE",
"pwglf": "PWGLF",
"pwgud": "PWGUD",
"dpg": "DPG",
"trigger": "Trigger",
"tutorial": "Tutorial",
}
print(f'PR title: "{title}"')
print(f'PR labels: "{labels}"')
tags_relevant = [tags[label] for label in tags if label in labels.split(",")]
print("Relevant title tags:", ",".join(tags_relevant))
passed = True
prefix_good = ",".join(tags_relevant)
prefix_good = f"[{prefix_good}] "
print(f"Generated prefix: {prefix_good}")
replace_title = 0
title_new = title
# If there is a prefix which contains a known tag, check it for correct tags, and reformat it if needed.
# If there is a prefix which does not contain any known tag, add the tag prefix.
# If there is no prefix, add the tag prefix.
if match := re.match(r"\[?(\w[\w, /\+-]+)[\]:]+ ", title):
prefix_title = match.group(1)
words_prefix_title = prefix_title.replace(",", " ").replace("/", " ").split()
title_stripped = title[len(match.group()) :]
print(f'PR title prefix: "{prefix_title}" -> tags: {words_prefix_title}')
print(f'Stripped PR title: "{title_stripped}"')
if any(tag in words_prefix_title for tag in tags.values()):
for tag in tags.values():
if tag in tags_relevant and tag not in words_prefix_title:
print(f'::error::Relevant tag "{tag}" not found in the prefix of the PR title.')
passed = False
if tag not in tags_relevant and tag in words_prefix_title:
print(f'::error::Irrelevant tag "{tag}" found in the prefix of the PR title.')
passed = False
# Format a valid prefix.
if passed:
prefix_good = ",".join(w for w in prefix_title.replace(",", " ").split() if w)
prefix_good = f"[{prefix_good}] "
print(f"::notice::Reformatted prefix: {prefix_good}")
if match.group() != prefix_good:
replace_title = 1
title_new = prefix_good + title_stripped
else:
print("::warning::No known tags found in the prefix.")
if tags_relevant:
replace_title = 1
title_new = prefix_good + title
else:
print("::warning::No valid prefix found in the PR title.")
if tags_relevant:
replace_title = 1
title_new = prefix_good + title
if not passed:
print("::error::Problems were found in the PR title prefix.")
print('::notice::Use the form "tags: title" or "[tags] title".')
sys.exit(1)
if replace_title:
print("::warning::The PR title prefix with tags needs to be added or adjusted.")
print(f'::warning::New title: "{title_new}".')
else:
print("::notice::The PR title prefix is fine.")
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as fh:
print(f"replace={replace_title}", file=fh)
print(f"title={title_new}", file=fh)
- name: Fix the PR title prefix
if: ${{ steps.check-prefix.outputs.replace == 1 }}
uses: the-wright-jamie/[email protected]
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
base-branch-regex: master
error-on-fail: false
title-template: "${{ steps.check-prefix.outputs.title }}"
title-update-action: replace