1
1
import path from "node:path" ;
2
2
import { existsSync , mkdirSync , readFileSync , writeFileSync , rmSync } from "node:fs" ;
3
- import { fileURLToPath } from "node:url" ;
4
3
import {
5
4
BreakingChangesCheckType ,
6
5
Context ,
7
6
BreakingChangeReviewRequiredLabel ,
8
7
VersioningReviewRequiredLabel ,
9
8
} from "./types/breaking-change.js" ;
10
9
import { ResultMessageRecord } from "./types/message.js" ;
11
- import { getArgumentValue } from "./utils/common-utils.js" ;
12
10
import { createOadMessageProcessor } from "./utils/oad-message-processor.js" ;
13
11
import { createPullRequestProperties } from "./utils/pull-request.js" ;
14
12
import { getChangedFilesStatuses , swagger } from "@azure-tools/specs-shared/changed-files" ;
15
- import { logMessage , setOutput } from "./log.js" ;
13
+ import { BREAKING_CHANGES_CHECK_TYPES } from "@azure-tools/specs-shared/breaking-change" ;
14
+ import { logError , LogLevel , logMessage , setOutput } from "./log.js" ;
16
15
17
16
/**
18
- * Parse the arguments.
19
- * @returns The runner command input.
17
+ * Interface for parsed CLI arguments
20
18
*/
21
- export function initContext ( ) : Context {
22
- const __filename : string = fileURLToPath ( import . meta. url ) ;
23
- const __dirname : string = path . dirname ( __filename ) ;
19
+ export interface ParsedCliArguments {
20
+ localSpecRepoPath : string ;
21
+ targetRepo : string ;
22
+ sourceRepo : string ;
23
+ prNumber : string ;
24
+ runType : BreakingChangesCheckType ;
25
+ baseBranch : string ;
26
+ headCommit : string ;
27
+ prSourceBranch : string ;
28
+ prTargetBranch : string ;
29
+ }
24
30
25
- // Get the arguments passed to the script
26
- const args : string [ ] = process . argv . slice ( 2 ) ;
27
- const localSpecRepoPath : string = path . resolve (
28
- getArgumentValue ( args , "--srp" , path . join ( __dirname , ".." ) ) ,
29
- ) ;
31
+ /**
32
+ * Create context from parsed CLI arguments
33
+ */
34
+ export function createContextFromParsedArgs (
35
+ parsedArgs : ParsedCliArguments ,
36
+ workingFolder : string ,
37
+ logFileFolder : string ,
38
+ ) : Context {
30
39
const swaggerDirs : string [ ] = [ "specification" , "dev" ] ;
31
- const repo : string = getArgumentValue ( args , "--repo" , "azure/azure-rest-api-specs" ) ;
32
- const prNumber : string = getArgumentValue ( args , "--number" , "" ) ;
33
- const runType = getArgumentValue ( args , "--rt" , "SameVersion" ) as BreakingChangesCheckType ;
34
- const workingFolder : string = path . join ( localSpecRepoPath , ".." ) ;
35
- const logFileFolder : string = path . join ( workingFolder , "out/logs" ) ;
36
-
37
- // Create the log file folder if it does not exist
38
- if ( ! existsSync ( logFileFolder ) ) {
39
- mkdirSync ( logFileFolder , { recursive : true } ) ;
40
- }
41
-
42
- const prUrl = `https://github.com/${ repo } /pull/${ prNumber } ` ;
40
+ const prUrl = `https://github.com/${ parsedArgs . targetRepo } /pull/${ parsedArgs . prNumber } ` ;
43
41
const oadMessageProcessorContext = createOadMessageProcessor ( logFileFolder , prUrl ) ;
42
+
44
43
return {
45
- localSpecRepoPath,
44
+ localSpecRepoPath : parsedArgs . localSpecRepoPath ,
46
45
workingFolder,
47
46
swaggerDirs,
48
47
logFileFolder,
49
- baseBranch : getArgumentValue ( args , "--bb" , "main" ) ,
50
- runType,
51
- checkName : getBreakingChangeCheckName ( runType ) ,
52
- headCommit : getArgumentValue ( args , "--hc" , "HEAD" ) ,
53
- repo,
54
- prNumber,
55
- prSourceBranch : getArgumentValue ( args , "--sb" , "" ) ,
56
- prTargetBranch : getArgumentValue ( args , "--tb" , "" ) ,
48
+ baseBranch : parsedArgs . baseBranch ,
49
+ runType : parsedArgs . runType ,
50
+ checkName : getBreakingChangeCheckName ( parsedArgs . runType ) ,
51
+ headCommit : parsedArgs . headCommit ,
52
+ targetRepo : parsedArgs . targetRepo ,
53
+ sourceRepo : parsedArgs . sourceRepo ,
54
+ prNumber : parsedArgs . prNumber ,
55
+ prSourceBranch : parsedArgs . prSourceBranch ,
56
+ prTargetBranch : parsedArgs . prTargetBranch ,
57
57
oadMessageProcessorContext,
58
58
prUrl,
59
59
} ;
@@ -65,9 +65,10 @@ export function initContext(): Context {
65
65
* Appropriate labels are added to this set by applyRules() function.
66
66
*/
67
67
export const BreakingChangeLabelsToBeAdded = new Set < string > ( ) ;
68
- export let defaultBreakingChangeBaseBranch = "main" ;
69
68
function getBreakingChangeCheckName ( runType : BreakingChangesCheckType ) : string {
70
- return runType === "SameVersion" ? "Swagger BreakingChange" : "BreakingChange(Cross-Version)" ;
69
+ return runType === BREAKING_CHANGES_CHECK_TYPES . SAME_VERSION
70
+ ? "Swagger BreakingChange"
71
+ : "BreakingChange(Cross-Version)" ;
71
72
}
72
73
73
74
/**
@@ -124,7 +125,6 @@ export async function getSwaggerDiffs(
124
125
additions : string [ ] ;
125
126
modifications : string [ ] ;
126
127
deletions : string [ ] ;
127
- renames : { from : string ; to : string } [ ] ;
128
128
total : number ;
129
129
} > {
130
130
try {
@@ -143,25 +143,23 @@ export async function getSwaggerDiffs(
143
143
( rename ) => swagger ( rename . from ) && swagger ( rename . to ) ,
144
144
) ;
145
145
146
+ // Add renamed files to the additions array and deletions array
147
+ filteredAdditions . push ( ...filteredRenames . map ( ( rename ) => rename . to ) ) ;
148
+ filteredDeletions . push ( ...filteredRenames . map ( ( rename ) => rename . from ) ) ;
149
+
146
150
return {
147
151
additions : filteredAdditions ,
148
152
modifications : filteredModifications ,
149
153
deletions : filteredDeletions ,
150
- renames : filteredRenames ,
151
- total :
152
- filteredAdditions . length +
153
- filteredModifications . length +
154
- filteredDeletions . length +
155
- filteredRenames . length ,
154
+ total : filteredAdditions . length + filteredModifications . length + filteredDeletions . length ,
156
155
} ;
157
156
} catch ( error ) {
158
- console . error ( " Error getting categorized changed files:" , error ) ;
157
+ logError ( ` Error getting categorized changed files: ${ error } ` ) ;
159
158
// Return empty result on error
160
159
return {
161
160
additions : [ ] ,
162
161
modifications : [ ] ,
163
162
deletions : [ ] ,
164
- renames : [ ] ,
165
163
total : 0 ,
166
164
} ;
167
165
}
@@ -172,11 +170,6 @@ export async function getSwaggerDiffs(
172
170
* TargetBranches is a set of branches and treat each of them like a service team master branch.
173
171
*/
174
172
export async function buildPrInfo ( context : Context ) : Promise < void > {
175
- /**
176
- * For PR target branch not in `targetBranches`. prepare for switch to master branch,
177
- * if not the switching to master below would failed
178
- */
179
- defaultBreakingChangeBaseBranch = context . baseBranch ;
180
173
const prInfo = await createPullRequestProperties (
181
174
context ,
182
175
context . runType === "CrossVersion" ? "cross-version" : "same-version" ,
@@ -216,15 +209,15 @@ export function changeBaseBranch(context: Context): void {
216
209
* Log the full list of OAD messages to console
217
210
*/
218
211
export function logFullOadMessagesList ( msgs : ResultMessageRecord [ ] ) : void {
219
- logMessage ( "---- Full list of messages ----" ) ;
212
+ logMessage ( "---- Full list of messages ----" , LogLevel . Group ) ;
220
213
logMessage ( "[" ) ;
221
214
// Printing the messages one by one because the console.log appears to elide the messages with "... X more items"
222
215
// after approximately 292 messages.
223
216
for ( const msg of msgs ) {
224
217
logMessage ( JSON . stringify ( msg , null , 4 ) + "," ) ;
225
218
}
226
219
logMessage ( "]" ) ;
227
- logMessage ( "---- End of full list of messages ----" ) ;
220
+ logMessage ( "---- End of full list of messages ----" , LogLevel . EndGroup ) ;
228
221
}
229
222
230
223
/**
@@ -265,7 +258,7 @@ export function cleanDummySwagger(): void {
265
258
* Return true if the type indicates the same version breaking change
266
259
*/
267
260
export function isSameVersionBreakingType ( type : BreakingChangesCheckType ) : boolean {
268
- return type === "SameVersion" ;
261
+ return type === BREAKING_CHANGES_CHECK_TYPES . SAME_VERSION ;
269
262
}
270
263
271
264
/**
0 commit comments