|
| 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 { loadOwners, matchFile } from "codeowners-utils"; |
| 6 | +import { GITHUB_ACTIONS_BOT_ID, COMMENT_PREFIX } from "./constants.ts"; |
| 7 | + |
| 8 | +async function run(): Promise<void> { |
| 9 | + try { |
| 10 | + if (!process.env.GITHUB_TOKEN) { |
| 11 | + core.setFailed(`Could not find GITHUB_TOKEN in env`); |
| 12 | + process.exit(); |
| 13 | + } |
| 14 | + |
| 15 | + const octokit = github.getOctokit(process.env.GITHUB_TOKEN); |
| 16 | + const payload = github.context.payload as PullRequestEvent; |
| 17 | + |
| 18 | + const { owner, repo } = github.context.repo; |
| 19 | + const pullRequestNumber = payload.number; |
| 20 | + |
| 21 | + const files = await octokit.paginate(octokit.rest.pulls.listFiles, { |
| 22 | + owner, |
| 23 | + repo, |
| 24 | + pull_number: pullRequestNumber, |
| 25 | + }); |
| 26 | + |
| 27 | + const codeowners = await loadOwners(process.cwd()); |
| 28 | + |
| 29 | + if (!codeowners) { |
| 30 | + throw new Error("Unable to load CODEOWNERS file."); |
| 31 | + } |
| 32 | + |
| 33 | + const matchedPatterns = [ |
| 34 | + ...new Set( |
| 35 | + files.flatMap((file) => matchFile(file.filename, codeowners) ?? []), |
| 36 | + ), |
| 37 | + ]; |
| 38 | + |
| 39 | + const { data: comments } = await octokit.rest.issues.listComments({ |
| 40 | + owner, |
| 41 | + repo, |
| 42 | + issue_number: pullRequestNumber, |
| 43 | + per_page: 100, |
| 44 | + }); |
| 45 | + |
| 46 | + const existingComment = comments.find( |
| 47 | + (comment) => |
| 48 | + comment.user?.id === GITHUB_ACTIONS_BOT_ID && |
| 49 | + comment.body?.includes(COMMENT_PREFIX), |
| 50 | + ); |
| 51 | + |
| 52 | + if (existingComment) { |
| 53 | + core.info(`Found existing comment with ID ${existingComment.id}`); |
| 54 | + } else { |
| 55 | + core.info(`No existing comment found`); |
| 56 | + } |
| 57 | + |
| 58 | + const comment = [ |
| 59 | + COMMENT_PREFIX, |
| 60 | + "| Pattern | Owners |", |
| 61 | + "| ------- | ------ |", |
| 62 | + ...matchedPatterns.map( |
| 63 | + (pattern) => |
| 64 | + `| ${pattern.pattern} | ${pattern.owners.map((owner) => "`" + owner + "`").join(", ")} |`, |
| 65 | + ), |
| 66 | + ].join("\n"); |
| 67 | + |
| 68 | + if (existingComment) { |
| 69 | + core.info( |
| 70 | + `Updating ${existingComment.id} with ${JSON.stringify(comment)}`, |
| 71 | + ); |
| 72 | + await octokit.rest.issues.updateComment({ |
| 73 | + owner, |
| 74 | + repo, |
| 75 | + comment_id: existingComment.id, |
| 76 | + body: comment, |
| 77 | + }); |
| 78 | + } else { |
| 79 | + core.info(`Creating new comment with ${JSON.stringify(comment)}`); |
| 80 | + await octokit.rest.issues.createComment({ |
| 81 | + owner, |
| 82 | + repo, |
| 83 | + issue_number: pullRequestNumber, |
| 84 | + body: comment, |
| 85 | + }); |
| 86 | + } |
| 87 | + } catch (error) { |
| 88 | + if (error instanceof Error) { |
| 89 | + core.setFailed(error.message); |
| 90 | + } |
| 91 | + process.exit(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +run(); |
0 commit comments