|
| 1 | +import * as core from "@actions/core"; |
| 2 | +import * as github from "@actions/github"; |
| 3 | +import type { PullRequestEvent } from "@octokit/webhooks-types"; |
| 4 | + |
| 5 | +import { GITHUB_ACTIONS_BOT_ID } from "./constants"; |
| 6 | + |
| 7 | +async function run(): Promise<void> { |
| 8 | + try { |
| 9 | + if (!process.env.GITHUB_TOKEN) { |
| 10 | + core.setFailed(`Could not find GITHUB_TOKEN in env`); |
| 11 | + process.exit(); |
| 12 | + } |
| 13 | + |
| 14 | + const octokit = github.getOctokit(process.env.GITHUB_TOKEN); |
| 15 | + const payload = github.context.payload as PullRequestEvent; |
| 16 | + |
| 17 | + const { owner, repo } = github.context.repo; |
| 18 | + const pullRequestNumber = payload.number; |
| 19 | + const runId = github.context.runId; |
| 20 | + |
| 21 | + const { data: run } = await octokit.rest.actions.listJobsForWorkflowRun({ |
| 22 | + owner, |
| 23 | + repo, |
| 24 | + run_id: runId, |
| 25 | + }); |
| 26 | + |
| 27 | + const job = run.jobs.findLast((job) => job.name === "Compiles"); |
| 28 | + |
| 29 | + if (!job) { |
| 30 | + core.setFailed(`Could not find a job called 'Compiles'`); |
| 31 | + process.exit(); |
| 32 | + } |
| 33 | + |
| 34 | + const failedStep = job.steps?.find((step) => step.conclusion === "failure"); |
| 35 | + |
| 36 | + if (failedStep) { |
| 37 | + core.info(`Found failed step ${failedStep.name}`); |
| 38 | + } |
| 39 | + |
| 40 | + const conclusion = failedStep ? "failure" : "success"; |
| 41 | + |
| 42 | + const { data: comments } = await octokit.rest.issues.listComments({ |
| 43 | + owner, |
| 44 | + repo, |
| 45 | + issue_number: pullRequestNumber, |
| 46 | + per_page: 100, |
| 47 | + }); |
| 48 | + |
| 49 | + const existingComment = comments.find( |
| 50 | + (comment) => |
| 51 | + comment.user?.id === GITHUB_ACTIONS_BOT_ID && |
| 52 | + comment.body?.includes("**CI run failed:**"), |
| 53 | + ); |
| 54 | + |
| 55 | + if (existingComment) { |
| 56 | + core.info(`Found existing comment with ID ${existingComment.id}`); |
| 57 | + } else { |
| 58 | + core.info(`No existing comment found`); |
| 59 | + } |
| 60 | + |
| 61 | + const url = `https://github.com/${owner}/${repo}/actions/runs/${runId}/job/${job.id}`; |
| 62 | + const comment = `**CI run failed:** [build logs](${url})`; |
| 63 | + |
| 64 | + if (conclusion === "failure") { |
| 65 | + if (existingComment) { |
| 66 | + core.info( |
| 67 | + `Updating ${existingComment.id} with ${JSON.stringify(comment)}`, |
| 68 | + ); |
| 69 | + await octokit.rest.issues.updateComment({ |
| 70 | + owner, |
| 71 | + repo, |
| 72 | + comment_id: existingComment.id, |
| 73 | + body: comment, |
| 74 | + }); |
| 75 | + } else { |
| 76 | + core.info(`Creating new comment with ${JSON.stringify(comment)}`); |
| 77 | + await octokit.rest.issues.createComment({ |
| 78 | + owner, |
| 79 | + repo, |
| 80 | + issue_number: pullRequestNumber, |
| 81 | + body: comment, |
| 82 | + }); |
| 83 | + } |
| 84 | + } else if (conclusion === "success" && existingComment) { |
| 85 | + core.info(`Removing ${existingComment.id}`); |
| 86 | + await octokit.rest.issues.deleteComment({ |
| 87 | + owner, |
| 88 | + repo, |
| 89 | + issue_number: pullRequestNumber, |
| 90 | + comment_id: existingComment.id, |
| 91 | + }); |
| 92 | + } |
| 93 | + } catch (error) { |
| 94 | + if (error instanceof Error) { |
| 95 | + core.setFailed(error.message); |
| 96 | + } |
| 97 | + process.exit(); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +run(); |
0 commit comments