diff options
| author | sunadoi <[email protected]> | 2022-10-10 05:11:25 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-10-09 22:11:25 +0200 |
| commit | f934792db4c3c1b44550b79cee893aa545164c59 (patch) | |
| tree | cf4b32e8a1574d79cf005b781f0181e6fbfd5dd9 /.github/workflows/commentCodeGeneration.ts | |
| parent | 13dac4f4c6879933ceba045e1235161ed3c6b4f2 (diff) | |
| download | faker-f934792db4c3c1b44550b79cee893aa545164c59.tar.xz faker-f934792db4c3c1b44550b79cee893aa545164c59.zip | |
infra: add CI step to detect generate script diffs (#1405)
Diffstat (limited to '.github/workflows/commentCodeGeneration.ts')
| -rw-r--r-- | .github/workflows/commentCodeGeneration.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/.github/workflows/commentCodeGeneration.ts b/.github/workflows/commentCodeGeneration.ts new file mode 100644 index 00000000..715854cd --- /dev/null +++ b/.github/workflows/commentCodeGeneration.ts @@ -0,0 +1,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> and <code>pnpm run generate:api-docs</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, + }); + } +}; |
