Skip to content

Commit

Permalink
Merge branch 'temp' into ci/pr-preflight-external
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Aug 5, 2024
2 parents 30aec62 + c24808b commit 7b5c0eb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
64 changes: 40 additions & 24 deletions .github/workflows/commentCodeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,59 @@ import type { context as ctx, GitHub } from '@actions/github/lib/utils';
*
* @param github A pre-authenticated octokit/rest.js client with pagination plugins
* @param context An object containing the context of the workflow run.
* @param pr_number The number of the pull request to comment on
* @param isSuccess A boolean indicating whether the workflow was successful
*/
export async function script(
github: InstanceType<typeof GitHub>,
context: typeof ctx,
pr_number: number,
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,
...repoArgs,
issue_number: pr_number,
});

const body = `Uncommitted changes were detected after runnning <code>generate:*</code> commands.\nPlease run <code>pnpm run preflight</code> to generate/update the related files, and commit them.`;
const body = `Uncommitted changes were detected after running <code>generate:*</code> commands.\nPlease run <code>pnpm run preflight</code> 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: pr_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,
});
}
}
}
9 changes: 0 additions & 9 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ jobs:
timeout-minutes: 10
name: 'Check Code Generation: node-22, ubuntu-latest'

outputs:
PR_NUMBER: ${{ steps.get_pr_number.outputs.PR_NUMBER }}

steps:
- name: Set output for PR number
id: get_pr_number
run: |
echo "PR_NUMBER=${{ github.event.pull_request.number }}"
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/preflight-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ jobs:
with:
script: |
const { script } = require('${{ github.workspace }}/.github/workflows/commentCodeGeneration.cjs')
console.log("${{ github.event.workflow_run.outputs.PR_NUMBER }}")
await script(github, context, ${{ github.event.workflow_run.outputs.PR_NUMBER }}, ${{ github.event.workflow_run.conclusion == 'success' }})
await script(github, context)

0 comments on commit 7b5c0eb

Please sign in to comment.