-
Notifications
You must be signed in to change notification settings - Fork 362
137 lines (116 loc) · 5.23 KB
/
Copy pathPR_label_checker.yml
File metadata and controls
137 lines (116 loc) · 5.23 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
name: PR check - Labels
on:
pull_request:
types:
- opened
- synchronize
- reopened
- labeled
- unlabeled
jobs:
check_labels:
name: Check labels
runs-on: ubuntu-latest
if: ${{ github.repository_owner == 'sofa-framework' }}
steps:
- name: Check status labels
id: check_status_labels
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Get PR info: id and labels
const prNumber = context.payload.pull_request.number;
const labels = context.payload.pull_request.labels.map(label => label.name);
// Array of possible labels defining the status of the PR
const statusLabels = [
'pr: status wip',
'pr: status to review',
'pr: status ready'
];
// Filter the labels array to get only the status labels
const matchingStatusLabels = labels.filter(label => statusLabels.includes(label));
// Count the number of entries in 'matchingLabels'
const matchingLabelsCount = matchingStatusLabels.length;
// If no descriptive label is set, add a comment in the PR and make the action check fail
if (matchingLabelsCount === 0) {
const comment = ':information_source: @'+context.payload.pull_request.user.login+' your PR does not include any **status** label :label: The label \"pr: status to review\" is added automatically.';
github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
// Set flag to TRUE
core.setOutput('no_status_label', 'true');
}
else if (matchingLabelsCount > 1) {
const comment = ':warning: :warning: :warning:<br>@'+context.payload.pull_request.user.login+' your PR includes **too many status labels** :label:<br> Make sure to **select only one** (wip, to review or ready).<br>:warning: :warning: :warning:';
github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
// Make the action step fail
core.setFailed('Too many status PR labels');
}
// Print all PR labels in log
console.log('Labels:', labels.join(', '));
- name: Add status label if none given
if: ${{ steps.check_status_labels.outputs.no_status_label == 'true' }}
run: |
gh pr edit "$NUMBER" --add-label "$LABELS"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.pull_request.number }}
LABELS: "pr: status to review"
- name: Check descriptive labels
if: ${{ always() }}
id: check_descriptive_labels
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Get PR info: id and labels
const prNumber = context.payload.pull_request.number;
const labels = context.payload.pull_request.labels.map(label => label.name);
// Define an array of descriptive labels
const descriptiveLabels = [
'deprecated',
'refactoring',
'pr: enhancement',
'pr: breaking',
'pr: clean',
'pr: fix',
'pr: new feature',
'pr: test',
'pr: revert(ed)',
'pr: project-ci-infrastructure'
];
// Check if any descriptive label is included in the 'labels' array
const hasDescriptiveLabel = labels.some(label => descriptiveLabels.includes(label));
// If no descriptive label is set, add a comment in the PR and make the action check fail
if (!hasDescriptiveLabel) {
const comment = ':warning: :warning: :warning:<br>@' + context.payload.pull_request.user.login + ' your PR does not include any **descriptive** label :label:<br> Make sure to add an appropriate [PR label](https://github.com/sofa-framework/sofa/labels) before merge.<br>:warning: :warning: :warning:';
github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
// Make the action step fail
core.setFailed('Invalid PR label');
}
- name: Check label pr based on previous PR
if: ${{ always() }}
uses: actions/github-script@v7
with:
script: |
const labels = context.payload.pull_request.labels.map(label => label.name);
const found = labels.includes("pr: based on previous PR");
// Make the action step fail
if (found) {
core.setFailed("Merge blocked: PR has label 'pr: based on previous PR'");
}