Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: add CI step to detect generate script diffs #1405

Merged
merged 16 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,56 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true

check-code-generation:
runs-on: ubuntu-latest
name: 'Check Code Generation: node-18, ubuntu-latest'
permissions:
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install pnpm
uses: pnpm/action-setup@v2.2.2
with:
version: 7

- name: Set node version to 18
uses: actions/setup-node@v3
with:
node-version: 18
cache: 'pnpm'

- name: Install deps
run: pnpm install
env:
CYPRESS_INSTALL_BINARY: 0

- name: Generate code
run: |
pnpm run generate:locales
pnpm run generate:api-docs

- name: Check diff
id: diff
run: |
git add .
git diff --cached --exit-code
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
continue-on-error: true

- name: Transpile ts
run: pnpm exec tsc .github/workflows/commentCodeGeneration.ts --outDir .github/workflows

- name: Comment
uses: actions/github-script@v6
with:
script: |
const script = require('${{ github.workspace }}/.github/workflows/commentCodeGeneration.js')
await script(github, context, ${{ steps.diff.outcome == 'success' }})

- name: Status
if: ${{ steps.diff.outcome == 'failure' }}
run: exit 1
48 changes: 48 additions & 0 deletions .github/workflows/commentCodeGeneration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { context as ctx, GitHub } from '@actions/github/lib/utils';
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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,
});
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
]
},
"devDependencies": {
"@actions/github": "^5.1.1",
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
"@algolia/client-search": "~4.14.2",
"@types/glob": "~8.0.0",
"@types/markdown-it": "~12.2.3",
Expand Down
151 changes: 151 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.