Skip to content

Commit 878a1bf

Browse files
authored
Add changeset formatting check (#3980)
1 parent 9c0a47f commit 878a1bf

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

.github/workflows/check-changeset.yml

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,42 @@ jobs:
2222
- name: Run changeset script
2323
run: yarn ts-node-script scripts/check_changeset.ts
2424
id: check-changeset
25-
- name: Read output
26-
run: echo "${{steps.check-changeset.outputs.MISSING_PACKAGES}}"
25+
- name: Print changeset checker output
26+
run: echo "${{steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}"
2727
- name: Find Comment
2828
uses: peter-evans/find-comment@v1
2929
id: fc
3030
with:
3131
issue-number: ${{github.event.number}}
3232
body-includes: Changeset File Check
3333
- name: Create comment (missing packages)
34-
if: ${{!steps.fc.outputs.comment-id && steps.check-changeset.outputs.MISSING_PACKAGES}}
34+
if: ${{!steps.fc.outputs.comment-id && steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
3535
uses: peter-evans/create-or-update-comment@v1
3636
with:
3737
issue-number: ${{github.event.number}}
3838
body: |
3939
### Changeset File Check :warning:
40-
Warning: This PR modifies files in the following packages but they have not been included in the changeset file:
41-
${{steps.check-changeset.outputs.MISSING_PACKAGES}}
42-
43-
Make sure this was intentional.
40+
${{steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
4441
- name: Update comment (missing packages)
4542
if: ${{steps.fc.outputs.comment-id}}
4643
uses: peter-evans/create-or-update-comment@v1
4744
with:
48-
comment-id: ${{steps.fc.outputs.comment-id}} && steps.check-changeset.outputs.MISSING_PACKAGES}}
45+
comment-id: ${{steps.fc.outputs.comment-id}} && steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
4946
edit-mode: replace
5047
body: |
5148
### Changeset File Check :warning:
52-
Warning: This PR modifies files in the following packages but they have not been included in the changeset file:
53-
${{steps.check-changeset.outputs.MISSING_PACKAGES}}
54-
55-
Make sure this was intentional.
49+
${{steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
5650
- name: Update comment (no missing packages)
57-
if: ${{steps.fc.outputs.comment-id && !steps.check-changeset.outputs.MISSING_PACKAGES}}
51+
if: ${{steps.fc.outputs.comment-id && !steps.check-changeset.outputs.CHANGESET_ERROR_MESSAGE}}
5852
uses: peter-evans/create-or-update-comment@v1
5953
with:
6054
comment-id: ${{steps.fc.outputs.comment-id}}
6155
edit-mode: replace
6256
body: |
6357
### Changeset File Check :white_check_mark:
64-
No modified packages are missing from the changeset file.
58+
- No modified packages are missing from the changeset file.
59+
- No changeset formatting errors detected.
60+
# Don't want it to throw before editing the comment.
61+
- name: Fail if checker script logged a blocking failure
62+
if: ${{steps.check-changeset.outputs.BLOCKING_FAILURE}}
63+
run: exit 1

scripts/check_changeset.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import { resolve } from 'path';
1919
import { existsSync } from 'fs';
20+
import { exec } from 'child-process-promise';
2021
import chalk from 'chalk';
2122
import simpleGit from 'simple-git/promise';
2223
import fs from 'mz/fs';
@@ -83,6 +84,31 @@ async function parseChangesetFile(changesetFile: string) {
8384
}
8485

8586
async function main() {
87+
const errors = [];
88+
try {
89+
await exec('yarn changeset status');
90+
} catch (e) {
91+
const messageLines = e.message.replace(/🦋 error /g, '').split('\n');
92+
let formattedStatusError =
93+
'- Changeset formatting error in following file:%0A';
94+
formattedStatusError += ' ```%0A';
95+
formattedStatusError += messageLines
96+
.filter(
97+
(line: string) => !line.match(/^ at [\w\.]+ \(.+:[0-9]+:[0-9]+\)/)
98+
)
99+
.filter((line: string) => !line.includes('Command failed'))
100+
.filter((line: string) => !line.includes('exited with error code 1'))
101+
.map((line: string) => ` ${line}`)
102+
.join('%0A');
103+
formattedStatusError += '%0A ```%0A';
104+
errors.push(formattedStatusError);
105+
/**
106+
* Sets Github Actions output for a step. Pass changeset error message to next
107+
* step. See:
108+
* https://github.com/actions/toolkit/blob/master/docs/commands.md#set-outputs
109+
*/
110+
console.log(`::set-output name=BLOCKING_FAILURE::true`);
111+
}
86112
try {
87113
const diffData = await getDiffData();
88114
if (diffData == null) {
@@ -94,23 +120,32 @@ async function main() {
94120
changedPkg => !changesetPackages.includes(changedPkg)
95121
);
96122
if (missingPackages.length > 0) {
97-
/**
98-
* Sets Github Actions output for a step. Pass missing package list to next
99-
* step. See:
100-
* https://github.com/actions/toolkit/blob/master/docs/commands.md#set-outputs
101-
*/
102-
console.log(
103-
`::set-output name=MISSING_PACKAGES::${missingPackages
104-
.map(pkg => `- ${pkg}`)
105-
.join('%0A')}`
106-
);
123+
const missingPackagesLines = [
124+
'- Warning: This PR modifies files in the following packages but they have not been included in the changeset file:'
125+
];
126+
for (const missingPackage of missingPackages) {
127+
missingPackagesLines.push(` - ${missingPackage}`);
128+
}
129+
missingPackagesLines.push('');
130+
missingPackagesLines.push(' Make sure this was intentional.');
131+
errors.push(missingPackagesLines.join('%0A'));
107132
}
108-
process.exit();
109133
}
110134
} catch (e) {
111135
console.error(chalk`{red ${e}}`);
112136
process.exit(1);
113137
}
138+
139+
/**
140+
* Sets Github Actions output for a step. Pass changeset error message to next
141+
* step. See:
142+
* https://github.com/actions/toolkit/blob/master/docs/commands.md#set-outputs
143+
*/
144+
if (errors.length > 0)
145+
console.log(
146+
`::set-output name=CHANGESET_ERROR_MESSAGE::${errors.join('%0A')}`
147+
);
148+
process.exit();
114149
}
115150

116151
main();

0 commit comments

Comments
 (0)