Skip to content

Add changeset formatting check #3980

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

Merged
merged 12 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 13 additions & 14 deletions .github/workflows/check-changeset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,42 @@ jobs:
- name: Run changeset script
run: yarn ts-node-script scripts/check_changeset.ts
id: check-changeset
- name: Read output
run: echo "${{steps.check-changeset.outputs.MISSING_PACKAGES}}"
- name: Print changeset checker output
run: echo "${{steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}"
- name: Find Comment
uses: peter-evans/find-comment@v1
id: fc
with:
issue-number: ${{github.event.number}}
body-includes: Changeset File Check
- name: Create comment (missing packages)
if: ${{!steps.fc.outputs.comment-id && steps.check-changeset.outputs.MISSING_PACKAGES}}
if: ${{!steps.fc.outputs.comment-id && steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
uses: peter-evans/create-or-update-comment@v1
with:
issue-number: ${{github.event.number}}
body: |
### Changeset File Check :warning:
Warning: This PR modifies files in the following packages but they have not been included in the changeset file:
${{steps.check-changeset.outputs.MISSING_PACKAGES}}

Make sure this was intentional.
${{steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
- name: Update comment (missing packages)
if: ${{steps.fc.outputs.comment-id}}
uses: peter-evans/create-or-update-comment@v1
with:
comment-id: ${{steps.fc.outputs.comment-id}} && steps.check-changeset.outputs.MISSING_PACKAGES}}
comment-id: ${{steps.fc.outputs.comment-id}} && steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
edit-mode: replace
body: |
### Changeset File Check :warning:
Warning: This PR modifies files in the following packages but they have not been included in the changeset file:
${{steps.check-changeset.outputs.MISSING_PACKAGES}}

Make sure this was intentional.
${{steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
- name: Update comment (no missing packages)
if: ${{steps.fc.outputs.comment-id && !steps.check-changeset.outputs.MISSING_PACKAGES}}
if: ${{steps.fc.outputs.comment-id && !steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
uses: peter-evans/create-or-update-comment@v1
with:
comment-id: ${{steps.fc.outputs.comment-id}}
edit-mode: replace
body: |
### Changeset File Check :white_check_mark:
No modified packages are missing from the changeset file.
- No modified packages are missing from the changeset file.
- No changeset formatting errors detected.
# Don't want it to throw before editing the comment.
- name: Fail if checker script logged a blocking failure
if: ${{steps.check-changeset.outputs.BLOCKING_FAILURE}}
run: exit 1
57 changes: 46 additions & 11 deletions scripts/check_changeset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { resolve } from 'path';
import { existsSync } from 'fs';
import { exec } from 'child-process-promise';
import chalk from 'chalk';
import simpleGit from 'simple-git/promise';
import fs from 'mz/fs';
Expand Down Expand Up @@ -83,6 +84,31 @@ async function parseChangesetFile(changesetFile: string) {
}

async function main() {
const errors = [];
try {
await exec('yarn changeset status');
} catch (e) {
const messageLines = e.message.replace(/🦋 error /g, '').split('\n');
let formattedStatusError =
'- Changeset formatting error in following file:%0A';
formattedStatusError += ' ```%0A';
formattedStatusError += messageLines
.filter(
(line: string) => !line.match(/^ at [\w\.]+ \(.+:[0-9]+:[0-9]+\)/)
)
.filter((line: string) => !line.includes('Command failed'))
.filter((line: string) => !line.includes('exited with error code 1'))
.map((line: string) => ` ${line}`)
.join('%0A');
formattedStatusError += '%0A ```%0A';
errors.push(formattedStatusError);
/**
* Sets Github Actions output for a step. Pass changeset error message to next
* step. See:
* https://github.com/actions/toolkit/blob/master/docs/commands.md#set-outputs
*/
console.log(`::set-output name=BLOCKING_FAILURE::true`);
}
try {
const diffData = await getDiffData();
if (diffData == null) {
Expand All @@ -94,23 +120,32 @@ async function main() {
changedPkg => !changesetPackages.includes(changedPkg)
);
if (missingPackages.length > 0) {
/**
* Sets Github Actions output for a step. Pass missing package list to next
* step. See:
* https://github.com/actions/toolkit/blob/master/docs/commands.md#set-outputs
*/
console.log(
`::set-output name=MISSING_PACKAGES::${missingPackages
.map(pkg => `- ${pkg}`)
.join('%0A')}`
);
const missingPackagesLines = [
'- Warning: This PR modifies files in the following packages but they have not been included in the changeset file:'
];
for (const missingPackage of missingPackages) {
missingPackagesLines.push(` - ${missingPackage}`);
}
missingPackagesLines.push('');
missingPackagesLines.push(' Make sure this was intentional.');
errors.push(missingPackagesLines.join('%0A'));
}
process.exit();
}
} catch (e) {
console.error(chalk`{red ${e}}`);
process.exit(1);
}

/**
* Sets Github Actions output for a step. Pass changeset error message to next
* step. See:
* https://github.com/actions/toolkit/blob/master/docs/commands.md#set-outputs
*/
if (errors.length > 0)
console.log(
`::set-output name=CHANGESET_ERROR_MESSAGE::${errors.join('%0A')}`
);
process.exit();
}

main();