-
Notifications
You must be signed in to change notification settings - Fork 474
282 lines (249 loc) · 11.9 KB
/
label-issues-in-release.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
name: Label issues in release
on:
workflow_dispatch:
inputs:
tag:
description: 'Release tag'
required: true
type: string
label_name:
description: 'Name of the label'
required: true
type: string
label_description:
description: 'Description of the label'
default: ''
required: false
type: string
label_color:
description: 'A color of the added label'
default: 'FFFFFF'
required: false
type: string
workflow_call:
inputs:
tag:
description: 'Release tag'
required: true
type: string
label_name:
description: 'Name of the label'
required: true
type: string
label_description:
description: 'Description of the label'
default: ''
required: false
type: string
label_color:
description: 'A color of the added label'
default: 'FFFFFF'
required: false
type: string
outputs:
issues:
description: "JSON encoded list of issues linked to commits in the release"
value: ${{ jobs.run.outputs.issues }}
# concurrency:
# group: ${{ github.workflow }}__${{ github.job }}__${{ github.ref }}
# cancel-in-progress: true
jobs:
run:
name: Run
runs-on: ubuntu-latest
outputs:
issues: ${{ steps.linked_issues.outputs.result }}
steps:
- name: Getting tags of the two latestest releases
id: tags
uses: actions/github-script@v7
env:
TAG: ${{ inputs.tag }}
with:
script: |
const { repository: { releases: { nodes: releases } } } = await github.graphql(`
query ($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
releases(first: 10, orderBy: { field: CREATED_AT, direction: DESC }) {
nodes {
name
tagName
tagCommit {
oid
}
isPrerelease
isDraft
publishedAt
}
}
}
}
`,
{
owner: context.repo.owner,
repo: context.repo.repo,
}
);
if (releases[0].tagName !== process.env.TAG) {
core.info(`Current latest tag: ${ releases[0].tagName }`);
core.setFailed(`Release with tag ${ process.env.TAG } is not latest one.`);
return;
}
const latestTag = process.env.TAG;
const [ { tagName: previousTag } ] = releases
.slice(1)
.filter(({ isDraft }) => !isDraft);
core.info('Found following tags:');
core.info(` latest: ${ latestTag }`);
core.info(` second latest: ${ previousTag }`);
core.setOutput('latest', latestTag);
core.setOutput('previous', previousTag);
- name: Looking for commits between two releases
id: commits
uses: actions/github-script@v7
env:
PREVIOUS_TAG: ${{ steps.tags.outputs.previous }}
LATEST_TAG: ${{ steps.tags.outputs.latest }}
with:
script: |
const { data: { commits: commitsInRelease } } = await github.request('GET /repos/{owner}/{repo}/compare/{basehead}', {
owner: context.repo.owner,
repo: context.repo.repo,
basehead: `${ process.env.PREVIOUS_TAG }...${ process.env.LATEST_TAG }`,
});
if (commitsInRelease.length === 0) {
core.notice(`No commits found between ${ process.env.PREVIOUS_TAG } and ${ process.env.LATEST_TAG }`);
return [];
}
const commits = commitsInRelease.map(({ sha }) => sha);
core.startGroup(`Found ${ commits.length } commits`);
commits.forEach((sha) => {
core.info(sha);
})
core.endGroup();
return commits;
- name: Looking for issues linked to commits
id: linked_issues
uses: actions/github-script@v7
env:
COMMITS: ${{ steps.commits.outputs.result }}
with:
script: |
const commits = JSON.parse(process.env.COMMITS);
if (commits.length === 0) {
return [];
}
const map = {};
core.startGroup(`Looking for linked issues`);
for (const sha of commits) {
const result = await getLinkedIssuesForCommitPR(sha);
result.forEach((issue) => {
map[issue] = issue;
});
}
core.endGroup();
const issues = Object.values(map);
if (issues.length > 0) {
core.startGroup(`Found ${ issues.length } unique issues`);
issues.sort().forEach((issue) => {
core.info(`#${ issue } - https://github.com/${ context.repo.owner }/${ context.repo.repo }/issues/${ issue }`);
})
core.endGroup();
} else {
core.notice('No linked issues found.');
}
return issues;
async function getLinkedIssuesForCommitPR(sha) {
core.info(`Fetching issues for commit: ${ sha }`);
const response = await github.graphql(`
query ($owner: String!, $repo: String!, $sha: GitObjectID!) {
repository(owner: $owner, name: $repo) {
object(oid: $sha) {
... on Commit {
associatedPullRequests(first: 10) {
nodes {
number
title
state
merged
closingIssuesReferences(first: 10) {
nodes {
number
title
closed
}
}
}
}
}
}
}
}
`, {
owner: context.repo.owner,
repo: context.repo.repo,
sha,
});
if (!response) {
core.info('Nothing has found.');
return [];
}
const { repository: { object: { associatedPullRequests } } } = response;
const issues = associatedPullRequests
.nodes
.map(({ closingIssuesReferences: { nodes: issues } }) => issues.map(({ number }) => number))
.flat();
core.info(`Found following issues: ${ issues.join(', ') || '-' }\n`);
return issues;
}
- name: Creating label
id: label_creating
uses: actions/github-script@v7
env:
LABEL_NAME: ${{ inputs.label_name }}
LABEL_COLOR: ${{ inputs.label_color }}
LABEL_DESCRIPTION: ${{ inputs.label_description }}
with:
script: |
try {
const result = await github.request('POST /repos/{owner}/{repo}/labels', {
owner: context.repo.owner,
repo: context.repo.repo,
name: process.env.LABEL_NAME,
color: process.env.LABEL_COLOR,
description: process.env.LABEL_DESCRIPTION,
});
core.info('Label was created.');
} catch (error) {
if (error.status === 422) {
core.info('Label already exist.');
} else {
core.setFailed(error.message);
}
}
- name: Adding label to issues
id: labeling_issues
uses: actions/github-script@v7
env:
LABEL_NAME: ${{ inputs.label_name }}
ISSUES: ${{ steps.linked_issues.outputs.result }}
with:
script: |
const issues = JSON.parse(process.env.ISSUES);
if (issues.length === 0) {
core.notice('No issues has found. Nothing to label.');
return;
}
for (const issue of issues) {
core.info(`Adding label to the issue #${ issue }...`);
await addLabelToIssue(issue, process.env.LABEL_NAME);
core.info('Done.\n');
}
async function addLabelToIssue(issue, label) {
return await github.request('POST /repos/{owner}/{repo}/issues/{issue_number}/labels', {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue,
labels: [ label ],
});
}