forked from Azure/azure-rest-api-specs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-status.js
More file actions
225 lines (199 loc) · 6.36 KB
/
set-status.js
File metadata and controls
225 lines (199 loc) · 6.36 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// @ts-check
import {
CheckConclusion,
CheckStatus,
CommitStatusState,
PER_PAGE_MAX,
} from "../../shared/src/github.js";
import { extractInputs } from "./context.js";
// TODO: Add tests
/* v8 ignore start */
/**
* @param {import('@actions/github-script').AsyncFunctionArguments} AsyncFunctionArguments
* @param {string} monitoredWorkflowName
* @param {string} requiredStatusName
* @param {string} overridingLabel
* @returns {Promise<void>}
*/
export default async function setStatus(
{ github, context, core },
monitoredWorkflowName,
requiredStatusName,
overridingLabel,
) {
const { owner, repo, head_sha, issue_number } = await extractInputs(github, context, core);
// Default target is this run itself
let target_url =
`https://github.com/${context.repo.owner}/${context.repo.repo}` +
`/actions/runs/${context.runId}`;
return await setStatusImpl({
owner,
repo,
head_sha,
issue_number,
target_url,
github,
core,
monitoredWorkflowName,
requiredStatusName,
overridingLabel,
});
}
/* v8 ignore stop */
/**
* @param {Object} params
* @param {string} params.owner
* @param {string} params.repo
* @param {string} params.head_sha
* @param {number} params.issue_number
* @param {string} params.target_url
* @param {(import("@octokit/core").Octokit & import("@octokit/plugin-rest-endpoint-methods/dist-types/types.js").Api & { paginate: import("@octokit/plugin-paginate-rest").PaginateInterface; })} params.github
* @param {typeof import("@actions/core")} params.core
* @param {string} params.monitoredWorkflowName
* @param {string} params.requiredStatusName
* @param {string} params.overridingLabel
* @returns {Promise<void>}
*/
export async function setStatusImpl({
owner,
repo,
head_sha,
issue_number,
target_url,
github,
core,
monitoredWorkflowName,
requiredStatusName,
overridingLabel,
}) {
core.setOutput("issue_number", issue_number);
// TODO: Try to extract labels from context (when available) to avoid unnecessary API call
const labels = await github.paginate(github.rest.issues.listLabelsOnIssue, {
owner: owner,
repo: repo,
issue_number: issue_number,
per_page: PER_PAGE_MAX,
});
const prLabels = labels.map((label) => label.name);
core.info(`Labels: ${prLabels}`);
// Parse overriding labels (comma-separated string to array)
const overridingLabelsArray = overridingLabel
? overridingLabel
.split(",")
.map((label) => label.trim())
.filter((label) => label) // Filter out empty labels
: [];
// Check if any overriding label is present
const foundOverridingLabel = overridingLabelsArray.find((label) => prLabels.includes(label));
if (foundOverridingLabel) {
const description = `Found label '${foundOverridingLabel}'`;
core.info(description);
const state = CheckConclusion.SUCCESS;
core.info(`Setting status to '${state}' for '${requiredStatusName}'`);
await github.rest.repos.createCommitStatus({
owner,
repo,
sha: head_sha,
state,
context: requiredStatusName,
description,
target_url,
});
return;
}
const workflowRuns = await github.paginate(github.rest.actions.listWorkflowRunsForRepo, {
owner,
repo,
event: "pull_request",
head_sha,
per_page: PER_PAGE_MAX,
});
core.info("Workflow Runs:");
workflowRuns.forEach((wf) => {
core.info(`- ${wf.name}: ${wf.conclusion || wf.status}`);
});
const targetRuns = workflowRuns
.filter((wf) => wf.name == monitoredWorkflowName)
// Sort by "updated_at" descending
.sort((a, b) => new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime());
// Sorted by "updated_at" descending, so most recent run is at index 0.
// If "targetRuns.length === 0", run will be "undefined", which the following
// code must handle.
const run = targetRuns[0];
if (!run) {
console.log(`No workflow runs found for '${monitoredWorkflowName}'.`);
}
if (run) {
/**
* Update target to the "Analyze Code" run, which contains the meaningful output.
*
* @example https://github.com/Azure/azure-rest-api-specs/actions/runs/14509047569
*/
target_url = run.html_url;
if (run.conclusion === CheckConclusion.FAILURE) {
const jobSummaryArtifactName = "job-summary";
// Check if run has a custom job summary
core.info(
`listWorkflowRunArtifacts(${owner}, ${repo}, ${run.id}, ${jobSummaryArtifactName})`,
);
const jobSummaryArtifacts = await github.paginate(
github.rest.actions.listWorkflowRunArtifacts,
{
owner: owner,
repo: repo,
run_id: run.id,
name: jobSummaryArtifactName,
per_page: PER_PAGE_MAX,
},
);
const hasJobSummary = jobSummaryArtifacts.length > 0;
core.info(`hasJobSummary: ${hasJobSummary}`);
if (!hasJobSummary) {
/**
* Update target to point directly to the first failed job
*
* @example https://github.com/Azure/azure-rest-api-specs/actions/runs/14509047569/job/40703679014?pr=18
*/
const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, {
owner,
repo,
run_id: run.id,
per_page: PER_PAGE_MAX,
});
const failedJobs = jobs.filter((job) => job.conclusion === CheckConclusion.FAILURE);
const failedJob = failedJobs[0];
if (failedJob?.html_url) {
target_url = `${failedJob.html_url}?pr=${issue_number}`;
}
}
}
}
if (run?.status === CheckStatus.COMPLETED) {
const state =
run.conclusion === CheckConclusion.SUCCESS
? CheckConclusion.SUCCESS
: CheckConclusion.FAILURE;
core.info(`Setting status to '${state}' for '${requiredStatusName}'`);
await github.rest.repos.createCommitStatus({
owner,
repo,
sha: head_sha,
state,
context: requiredStatusName,
target_url,
});
} else {
core.info(
`No workflow runs found for '${monitoredWorkflowName}'. Setting status to ${CommitStatusState.PENDING} for required status: ${requiredStatusName}.`,
);
// Run was not found (not started), or not completed
await github.rest.repos.createCommitStatus({
owner,
repo,
sha: head_sha,
state: CommitStatusState.PENDING,
context: requiredStatusName,
target_url,
});
}
}