-
Notifications
You must be signed in to change notification settings - Fork 0
208 lines (187 loc) · 7.9 KB
/
Copy pathnotify_user.yml
File metadata and controls
208 lines (187 loc) · 7.9 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
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
name: Comment when commit references issue
on:
workflow_call:
workflow_run:
workflows:
- Build APKs
types:
- completed
branches:
- main
jobs:
notify-issue:
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
contents: read
steps:
- name: Notify referenced issues
uses: actions/github-script@v9
env:
ISSUE_LABEL: CONFIRMED
ISSUE_LABEL_COLOR: 8DF148
ISSUE_LABEL_DESCRIPTION: Issue was referenced and confirmed by a commit and should be closed.
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const nightlyLinks = [
{
label: "GMS",
url: "https://nightly.link/SolsticeIO/Solstice/workflows/build/dev/app-gms-mobile-universal-release"
},
{
label: "FOSS",
url: "https://nightly.link/SolsticeIO/Solstice/workflows/build/dev/app-foss-mobile-universal-release"
}
];
const nightlyUrls = nightlyLinks.map((link) => link.url);
const labelName = process.env.ISSUE_LABEL;
const labelColor = process.env.ISSUE_LABEL_COLOR;
const labelDescription = process.env.ISSUE_LABEL_DESCRIPTION;
const commentMarker = "<!-- solstice-notify-user:nightly-reference -->";
const referencedIssuePattern = /#([1-9]\d*)\b/g;
const issueClosedReason = "completed";
const workflowRun = context.payload.workflow_run;
const commits = Array.isArray(context.payload.commits)
? context.payload.commits
: [];
const headCommit = workflowRun?.head_commit ?? context.payload.head_commit;
const commitMessages = commits.length > 0
? commits.map((commit) => commit.message)
: headCommit?.message
? [headCommit.message]
: [];
const issueNumbers = [
...new Set(
commitMessages.flatMap((message) =>
[...message.matchAll(referencedIssuePattern)].map((match) => Number(match[1]))
)
)
];
if (issueNumbers.length === 0) {
core.info("No referenced issues found in the completed build commit message.");
return;
}
const ensureLabelExists = async () => {
try {
await github.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: labelName
});
} catch (error) {
if (error.status !== 404) {
throw error;
}
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: labelName,
color: labelColor,
description: labelDescription
});
}
};
const tryEnsureLabelExists = async () => {
try {
await ensureLabelExists();
return true;
} catch (error) {
core.warning(`Skipping label creation check because GitHub rejected the request: ${error.message}`);
return false;
}
};
const canApplyLabel = await tryEnsureLabelExists();
const failures = [];
for (const issueNumber of issueNumbers) {
try {
const issue = await github.rest.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber
});
if (issue.data.pull_request) {
core.info(`Skipped notifying #${issueNumber} because it is a pull request.`);
continue;
}
if (canApplyLabel) {
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
labels: [labelName]
});
} catch (error) {
core.warning(`Skipping label update for issue #${issueNumber}: ${error.message}`);
}
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
per_page: 100
});
const hasExistingNotification = comments.some((comment) => {
const body = comment.body ?? "";
return body.includes(commentMarker) ||
(
comment.user?.type === "Bot" &&
body.includes("This issue was referenced on commit") &&
nightlyUrls.some((nightlyUrl) => body.includes(nightlyUrl))
);
});
if (hasExistingNotification) {
core.info(`Issue #${issueNumber} already has a nightly reference notification comment.`);
if (!issue.data.pull_request && issue.data.state !== "closed") {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
state: "closed",
state_reason: issueClosedReason
});
core.info(`Closed issue #${issueNumber} after existing notification comment.`);
}
continue;
}
const author = issue.data.user.login;
const headSha = workflowRun?.head_sha ?? headCommit?.id ?? context.sha;
const commitUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/commit/${headSha}`;
const commitSha = headSha.substring(0, 7);
const body = [
commentMarker,
`Hey @${author}, This issue was referenced on commit [\`${commitSha}\`](${commitUrl}).`,
"",
"Make sure to try the latest nightly version:",
"",
...nightlyLinks.map((link) => `${link.label}: ${link.url}`),
"",
"This issue is being closed automatically after the referenced fix commit."
].join("\n");
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body
});
if (!issue.data.pull_request && issue.data.state !== "closed") {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
state: "closed",
state_reason: issueClosedReason
});
core.info(`Closed issue #${issueNumber} after notification comment.`);
}
} catch (error) {
failures.push(`#${issueNumber}: ${error.message}`);
core.warning(`Failed to notify issue #${issueNumber}: ${error.message}`);
}
}
if (failures.length > 0) {
throw new Error(`Failed to notify referenced issues: ${failures.join("; ")}`);
}