diff options
| author | ST-DDT <[email protected]> | 2024-08-15 00:47:29 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-08-14 22:47:29 +0000 |
| commit | ff808d5093f6722e2e3f79decf5997c13fdaf245 (patch) | |
| tree | d4fbf38bcd3ff02ee039968077495154a1fad780 /.github/workflows/commentCodeGeneration.ts | |
| parent | 1e9d65bce14cb446388a61e2c9a74c5f898a985e (diff) | |
| download | faker-ff808d5093f6722e2e3f79decf5997c13fdaf245.tar.xz faker-ff808d5093f6722e2e3f79decf5997c13fdaf245.zip | |
infra: fix preflight comment for external contributors (#3038)
Diffstat (limited to '.github/workflows/commentCodeGeneration.ts')
| -rw-r--r-- | .github/workflows/commentCodeGeneration.ts | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/.github/workflows/commentCodeGeneration.ts b/.github/workflows/commentCodeGeneration.ts index 96a520d1..2e8a115c 100644 --- a/.github/workflows/commentCodeGeneration.ts +++ b/.github/workflows/commentCodeGeneration.ts @@ -7,18 +7,34 @@ 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<typeof GitHub>, - context: typeof ctx, - isSuccess: boolean + context: typeof ctx ): Promise<void> { + 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 = `GitHub Actions has found some problems running the preflight checks. @@ -34,22 +50,24 @@ Please make sure to: (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, + }); + } } } |
