- Notifications
You must be signed in to change notification settings - Fork 924
/
Copy pathcheck_changeset.ts
230 lines (214 loc) · 7.74 KB
/
check_changeset.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import{resolve}from'path';
import{existsSync}from'fs';
import{writeFile}from'fs/promises';
import{exec}from'child-process-promise';
importchalkfrom'chalk';
importsimpleGitfrom'simple-git';
importfsfrom'mz/fs';
constroot=resolve(__dirname,'../..');
constgit=simpleGit(root);
constbaseRef=process.env.GITHUB_PULL_REQUEST_BASE_SHA||'main';
constheadRef=process.env.GITHUB_PULL_REQUEST_HEAD_SHA||'HEAD';
constgithubOutputFile=(function(): string{
constvalue=process.env.GITHUB_OUTPUT;
if(!value){
thrownewError('GITHUB_OUTPUT environment variable must be set');
}
returnvalue;
})();
// Version bump text converted to rankable numbers.
constbumpRank: Record<string,number>={
'patch': 0,
'minor': 1,
'major': 2
};
/**
* Get highest bump that isn't the main firebase package, return
* numerical rank, bump text, package name.
*/
functiongetHighestBump(changesetPackages: Record<string,string>){
constfirebasePkgJson=require(resolve(
root,
'packages/firebase/package.json'
));
lethighestBump=bumpRank.patch;
lethighestBumpText='patch';
letbumpPackage='';
for(constpkgNameofObject.keys(changesetPackages)){
if(
pkgName!=='firebase'&&
pkgNameinfirebasePkgJson.dependencies&&
bumpRank[changesetPackages[pkgName]]>highestBump
){
highestBump=bumpRank[changesetPackages[pkgName]];
highestBumpText=changesetPackages[pkgName];
bumpPackage=pkgName;
}
}
return{ highestBump,bumpText: highestBumpText, bumpPackage };
}
/**
* Identify modified packages.
*/
asyncfunctiongetDiffData(): Promise<{
changedPackages: Set<string>;
changesetFile: string;
}|null>{
constdiff=awaitgit.diff(['--name-only',`${baseRef}...${headRef}`]);
constchangedFiles=diff.split('\n');
letchangesetFile='';
constchangedPackages=newSet<string>();
for(constfilenameofchangedFiles){
// Check for an existing .changeset
constchangesetMatch=filename.match(/^\.changeset\/[a-zA-Z-]+\.md/);
if(changesetMatch){
changesetFile=changesetMatch[0];
}
// Check for changed files inside package dirs.
constpkgMatch=filename.match('^(packages/[a-zA-Z0-9-]+)/.*');
if(pkgMatch&&pkgMatch[1]){
// skip packages without package.json
// It could happen when we rename a package or remove a package from the repo
constpkgJsonPath=resolve(root,pkgMatch[1],'package.json');
if(!existsSync(pkgJsonPath)){
continue;
}
constchangedPackage=require(pkgJsonPath);
if(changedPackage){
// Add the package itself.
changedPackages.add(changedPackage.name);
}
}
}
if(!changesetFile||changedPackages.size===0){
returnnull;
}
return{ changedPackages, changesetFile };
}
asyncfunctionparseChangesetFile(changesetFile: string){
constfileExists=awaitfs.exists(changesetFile);
if(!fileExists){
process.exit();
}
constfileText: string=awaitfs.readFile(changesetFile,'utf8');
constfileParts=fileText.split('---\n');
constpackageLines=fileParts[1].split('\n');
constchangesetPackages: Record<string,string>={};
packageLines
.filter(line=>line)
.forEach(line=>{
const[packageName,bumpType]=line.split(':');
changesetPackages[packageName.replace(/['"]/g,'')]=bumpType.trim();
});
returnchangesetPackages;
}
asyncfunctionmain(){
consterrors=[];
try{
awaitexec(`yarn changeset status`);
awaitexec(`echo "BLOCKING_FAILURE=false" >> $GITHUB_OUTPUT`);
}catch(e){
consterror=easError;
if(
error.message.match('No changesets present')||
error.message.match('no changesets were found')
){
awaitexec(`echo "BLOCKING_FAILURE=false" >> $GITHUB_OUTPUT`);
}else{
constmessageLines=error.message.replace(/🦋error/g,'').split('\n');
letformattedStatusError=
'- 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://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
*/
awaitexec(`echo "BLOCKING_FAILURE=true" >> $GITHUB_OUTPUT`);
}
}
try{
constdiffData=awaitgetDiffData();
if(diffData!=null){
const{ changedPackages, changesetFile }=diffData;
constchangesetPackages=awaitparseChangesetFile(changesetFile);
// Check for packages where files were modified but there's no changeset.
constmissingPackages=[...changedPackages].filter(
changedPkg=>!Object.keys(changesetPackages).includes(changedPkg)
);
if(missingPackages.length>0){
constmissingPackagesLines=[
'- Warning: This PR modifies files in the following packages but they have not been included in the changeset file:'
];
for(constmissingPackageofmissingPackages){
missingPackagesLines.push(` - ${missingPackage}`);
}
missingPackagesLines.push('');
missingPackagesLines.push(' Make sure this was intentional.');
errors.push(missingPackagesLines.join('%0A'));
}
// Check for packages with a minor or major bump where 'firebase' hasn't been
// bumped high enough or at all.
const{ highestBump, bumpText, bumpPackage }=
getHighestBump(changesetPackages);
if(highestBump>bumpRank.patch){
if(changesetPackages['firebase']==null){
errors.push(
`- Package ${bumpPackage} has a ${bumpText} bump which requires an `+
`additional line to bump the main "firebase" package to ${bumpText}.`
);
awaitexec(`echo "BLOCKING_FAILURE=true" >> $GITHUB_OUTPUT`);
}elseif(bumpRank[changesetPackages['firebase']]<highestBump){
errors.push(
`- Package ${bumpPackage} has a ${bumpText} bump. `+
`Increase the bump for the main "firebase" package to ${bumpText}.`
);
awaitexec(`echo "BLOCKING_FAILURE=true" >> $GITHUB_OUTPUT`);
}
}
}
}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://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter
*/
if(errors.length>0){
awaitwriteFile(
githubOutputFile,
`CHANGESET_ERROR_MESSAGE=${errors.join('%0A')}\n`,
{flag: 'a'}
);
}
process.exit();
}
main();