From 57859ff4231b9371886230921d790449d843b234 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Mon, 5 Aug 2024 22:54:47 +0200 Subject: [PATCH] infra(CI): enable preflight comment for external contributors --- .github/workflows/commentCodeGeneration.ts | 66 ++++++++++++++-------- .github/workflows/pr.yml | 13 +---- .github/workflows/preflight-comment.yml | 45 +++++++++++++++ 3 files changed, 88 insertions(+), 36 deletions(-) create mode 100644 .github/workflows/preflight-comment.yml diff --git a/.github/workflows/commentCodeGeneration.ts b/.github/workflows/commentCodeGeneration.ts index ed5258d5899..6842c7814ee 100644 --- a/.github/workflows/commentCodeGeneration.ts +++ b/.github/workflows/commentCodeGeneration.ts @@ -7,42 +7,60 @@ import type { context as ctx, GitHub } from '@actions/github/lib/utils'; * https://github.com/actions/github-script * * @param github A pre-authenticated octokit/rest.js client with pagination plugins - * @param context An object containing the context of the workflow run - * @param isSuccess A boolean indicating whether the workflow was successful + * @param context An object containing the context of the workflow run. */ export async function script( github: InstanceType, - context: typeof ctx, - isSuccess: boolean + context: typeof ctx ): Promise { + const repoArgs = { owner: context.repo.owner, repo: context.repo.repo }; + + // Identify the PR that triggered the workflow + const head_branch: string = context.payload.workflow_run.head_branch; + const { data: prs } = await github.rest.pulls.list({ + ...repoArgs, + state: 'open', + head: head_branch, + }); + + if (prs.length === 0) { + console.log(`No PRs found for branch ${head_branch}`); + return; + } + + const pr_number = prs[0].number; + + // Check if the PR already has a comment from the bot + const { data: comments } = await github.rest.issues.listComments({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, + ...repoArgs, + issue_number: pr_number, }); - const body = `Uncommitted changes were detected after runnning generate:* commands.\nPlease run pnpm run preflight to generate/update the related files, and commit them.`; + const body = `Uncommitted changes were detected after running generate:* commands.\nPlease run pnpm run preflight to generate/update the related files, and commit them.`; const botComment = comments.find( (comment) => comment.user?.type === 'Bot' && comment.body?.includes(body) ); - if (isSuccess) { - if (!botComment) return; - await github.rest.issues.deleteComment({ - owner: context.repo.owner, - repo: context.repo.repo, - comment_id: botComment.id, - }); - return; - } + const isSuccess = context.payload.workflow_run.conclusion === 'success'; - if (!botComment) { - await github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - body, - }); + if (isSuccess) { + // Delete the bot comment if present + if (botComment != null) { + await github.rest.issues.deleteComment({ + ...repoArgs, + comment_id: botComment.id, + }); + } + } else { + // Create the comment if missing + if (botComment == null) { + await github.rest.issues.createComment({ + ...repoArgs, + issue_number: pr_number, + body, + }); + } } } diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 878e427a970..506f17763bc 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -11,8 +11,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 name: 'Check Code Generation: node-22, ubuntu-latest' - permissions: - pull-requests: write + steps: - name: Checkout uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 @@ -52,16 +51,6 @@ jobs: git diff --cached --name-only --exit-code continue-on-error: true - - name: Transpile ts - run: pnpm tsup-node .github/workflows/commentCodeGeneration.ts --format cjs --clean false --out-dir .github/workflows - - - name: Comment - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 - with: - script: | - const { script } = require('${{ github.workspace }}/.github/workflows/commentCodeGeneration.cjs') - await script(github, context, ${{ steps.generate.outcome == 'success' && steps.diff.outcome == 'success' }}) - - name: Status if: ${{ steps.generate.outcome == 'failure' || steps.diff.outcome == 'failure' }} run: exit 1 diff --git a/.github/workflows/preflight-comment.yml b/.github/workflows/preflight-comment.yml new file mode 100644 index 00000000000..b55d41cf6b1 --- /dev/null +++ b/.github/workflows/preflight-comment.yml @@ -0,0 +1,45 @@ +name: Preflight Comment + +on: + workflow_run: + workflows: 'PR' + types: + - completed + +permissions: + pull-requests: write + +jobs: + comment-code-generation: + runs-on: ubuntu-latest + timeout-minutes: 5 + name: 'Comment Code Generation' + steps: + - name: Checkout + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + ref: 'next' + + - name: Install pnpm + uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0 + + - name: Set node version to 22 + uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3 + with: + node-version: 22 + cache: 'pnpm' + + - name: Install deps + run: pnpm install + env: + CYPRESS_INSTALL_BINARY: 0 + + - name: Transpile ts + run: pnpm tsup-node .github/workflows/commentCodeGeneration.ts --format cjs --clean false --out-dir .github/workflows + + - name: Comment + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + with: + script: | + const { script } = require('${{ github.workspace }}/.github/workflows/commentCodeGeneration.cjs') + await script(github, context)