-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathindex.ts
More file actions
114 lines (98 loc) · 2.87 KB
/
index.ts
File metadata and controls
114 lines (98 loc) · 2.87 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
import * as core from "@actions/core";
import * as github from "@actions/github";
import type { PullRequestEvent } from "@octokit/webhooks-types";
import { GITHUB_ACTIONS_BOT_ID } from "./constants";
async function run(): Promise<void> {
try {
if (!process.env.GITHUB_TOKEN) {
core.setFailed(`Could not find GITHUB_TOKEN in env`);
process.exit();
}
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
const payload = github.context.payload as PullRequestEvent;
const { owner, repo } = github.context.repo;
const pullRequestNumber = payload.number;
const runId = github.context.runId;
const { data: run } = await octokit.rest.actions.listJobsForWorkflowRun({
owner,
repo,
run_id: runId,
});
const ciJobs = run.jobs.filter(
(job) =>
job.name === "Pre Build" ||
job.name === "Build" ||
job.name === "Post Build",
);
if (ciJobs.length === 0) {
core.setFailed(`Could not find Pre Build, Build, or Post Build jobs`);
process.exit();
}
const failedJob = ciJobs.find((job) =>
job.steps?.some((step) => step.conclusion === "failure"),
);
if (failedJob) {
const failedStep = failedJob.steps?.find(
(step) => step.conclusion === "failure",
);
core.info(
`Found failed step ${failedStep?.name} in job ${failedJob.name}`,
);
}
const conclusion = failedJob ? "failure" : "success";
const { data: comments } = await octokit.rest.issues.listComments({
owner,
repo,
issue_number: pullRequestNumber,
per_page: 100,
});
const existingComment = comments.find(
(comment) =>
comment.user?.id === GITHUB_ACTIONS_BOT_ID &&
comment.body?.includes("**CI run failed:**"),
);
if (existingComment) {
core.info(`Found existing comment with ID ${existingComment.id}`);
} else {
core.info(`No existing comment found`);
}
const targetJob = failedJob ?? ciJobs[0];
const url = `https://github.com/${owner}/${repo}/actions/runs/${runId}/job/${targetJob.id}`;
const comment = `**CI run failed:** [build logs](${url})`;
if (conclusion === "failure") {
if (existingComment) {
core.info(
`Updating ${existingComment.id} with ${JSON.stringify(comment)}`,
);
await octokit.rest.issues.updateComment({
owner,
repo,
comment_id: existingComment.id,
body: comment,
});
} else {
core.info(`Creating new comment with ${JSON.stringify(comment)}`);
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: pullRequestNumber,
body: comment,
});
}
} else if (conclusion === "success" && existingComment) {
core.info(`Removing ${existingComment.id}`);
await octokit.rest.issues.deleteComment({
owner,
repo,
issue_number: pullRequestNumber,
comment_id: existingComment.id,
});
}
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
}
process.exit();
}
}
run();