From db4bda713414929781feb7e8a4d7a4601ad4b9c2 Mon Sep 17 00:00:00 2001 From: Kian Newman-Hazel Date: Mon, 12 May 2025 19:15:11 +0100 Subject: [PATCH 1/5] [Docs Site] Comment matched CODEOWNERS entries on PRs --- .github/workflows/ci.yml | 2 + bin/post-codeowners-comment/constants.ts | 3 + bin/post-codeowners-comment/index.ts | 95 ++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 bin/post-codeowners-comment/constants.ts create mode 100644 bin/post-codeowners-comment/index.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e7644f6b1979fd3..b2d684273e26119 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,8 @@ jobs: node_modules/.astro/assets key: static + - run: npx tsx bin/post-codeowners-comment/index.ts + - run: npm ci - run: npm run check diff --git a/bin/post-codeowners-comment/constants.ts b/bin/post-codeowners-comment/constants.ts new file mode 100644 index 000000000000000..e5cd61eaad403d2 --- /dev/null +++ b/bin/post-codeowners-comment/constants.ts @@ -0,0 +1,3 @@ +export const GITHUB_ACTIONS_BOT_ID = 41898282; +export const COMMENT_PREFIX = + "This pull request requires reviews from **CODEOWNERS** as it changes files that match the following patterns:"; diff --git a/bin/post-codeowners-comment/index.ts b/bin/post-codeowners-comment/index.ts new file mode 100644 index 000000000000000..515d1650217a081 --- /dev/null +++ b/bin/post-codeowners-comment/index.ts @@ -0,0 +1,95 @@ +import * as core from "@actions/core"; +import * as github from "@actions/github"; +import type { PullRequestEvent } from "@octokit/webhooks-types"; + +import { loadOwners, matchFile } from "codeowners-utils"; +import { GITHUB_ACTIONS_BOT_ID, COMMENT_PREFIX } from "./constants.ts"; + +async function run(): Promise { + 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 files = await octokit.paginate(octokit.rest.pulls.listFiles, { + owner, + repo, + pull_number: pullRequestNumber, + }); + + const codeowners = await loadOwners(process.cwd()); + + if (!codeowners) { + throw new Error("Unable to load CODEOWNERS file."); + } + + const matchedPatterns = [ + ...new Set( + files.flatMap((file) => matchFile(file.filename, codeowners) ?? []), + ), + ]; + + 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(COMMENT_PREFIX), + ); + + if (existingComment) { + core.info(`Found existing comment with ID ${existingComment.id}`); + } else { + core.info(`No existing comment found`); + } + + const comment = [ + COMMENT_PREFIX, + "| Pattern | Owners |", + "| ------- | ------ |", + ...matchedPatterns.map( + (pattern) => + `| ${pattern.pattern} | ${pattern.owners.map((owner) => "`" + owner + "`").join(", ")} |`, + ), + ].join("\n"); + + 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, + }); + } + } catch (error) { + if (error instanceof Error) { + core.setFailed(error.message); + } + process.exit(); + } +} + +run(); From ae895bee3ff3fa4ac6a6b995498a7671863e87ca Mon Sep 17 00:00:00 2001 From: Kian Newman-Hazel Date: Mon, 12 May 2025 19:16:19 +0100 Subject: [PATCH 2/5] fix order --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b2d684273e26119..841b61e3ed540dd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,9 +27,10 @@ jobs: node_modules/.astro/assets key: static + - run: npm ci + - run: npx tsx bin/post-codeowners-comment/index.ts - - run: npm ci - run: npm run check - uses: reviewdog/action-eslint@v1 From 38b299c895c9402b5c34abefbae5138cf9120d63 Mon Sep 17 00:00:00 2001 From: Kian Newman-Hazel Date: Mon, 12 May 2025 19:17:45 +0100 Subject: [PATCH 3/5] add token --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 841b61e3ed540dd..3fd3d84e2e1f45c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,8 @@ jobs: - run: npm ci - run: npx tsx bin/post-codeowners-comment/index.ts + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: npm run check From 8f9abfd5a05a53c5d0c876d35487fdaba7f713c6 Mon Sep 17 00:00:00 2001 From: Kian Newman-Hazel Date: Mon, 12 May 2025 19:21:02 +0100 Subject: [PATCH 4/5] wrap pattern in backticks too --- bin/post-codeowners-comment/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/post-codeowners-comment/index.ts b/bin/post-codeowners-comment/index.ts index 515d1650217a081..976c54fc2d911d3 100644 --- a/bin/post-codeowners-comment/index.ts +++ b/bin/post-codeowners-comment/index.ts @@ -61,7 +61,7 @@ async function run(): Promise { "| ------- | ------ |", ...matchedPatterns.map( (pattern) => - `| ${pattern.pattern} | ${pattern.owners.map((owner) => "`" + owner + "`").join(", ")} |`, + `| \`${pattern.pattern}\` | ${pattern.owners.map((owner) => `\`${owner}\``).join(", ")} |`, ), ].join("\n"); From b2f8f7ee0b5321c5d05c167a719590d3db826816 Mon Sep 17 00:00:00 2001 From: Kian Newman-Hazel Date: Mon, 12 May 2025 19:58:10 +0100 Subject: [PATCH 5/5] continue on error --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3fd3d84e2e1f45c..86abef0c2a01848 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,7 @@ jobs: - run: npm ci - run: npx tsx bin/post-codeowners-comment/index.ts + continue-on-error: true env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}