aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/commentCodeGeneration.ts
blob: 74a8092735a839c4d0bbe301c29205e18800c38d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { context as ctx, GitHub } from '@actions/github/lib/utils';

/**
 * Notifies the PR about uncommitted changes generated by the `generate:*` commands.
 *
 * This script is used by github-script
 * 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
 */
module.exports = async (
  github: InstanceType<typeof GitHub>,
  context: typeof ctx,
  isSuccess: boolean
) => {
  const { data: comments } = await github.rest.issues.listComments({
    owner: context.repo.owner,
    repo: context.repo.repo,
    issue_number: context.issue.number,
  });

  const body = `Uncommitted changes were detected after runnning <code>generate:*</code> commands.\nPlease run <code>pnpm run generate:locales</code>, <code>pnpm run generate:api-docs</code>, and <code>pnpm run test -u</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;
  }

  if (!botComment) {
    await github.rest.issues.createComment({
      issue_number: context.issue.number,
      owner: context.repo.owner,
      repo: context.repo.repo,
      body,
    });
  }
};