forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeFiles.ts
More file actions
165 lines (145 loc) · 5.37 KB
/
ChangeFiles.ts
File metadata and controls
165 lines (145 loc) · 5.37 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { EOL } from 'os';
import * as glob from 'glob';
import { Utilities } from '../utilities/Utilities';
import { IChangeInfo } from '../api/ChangeManagement';
import { IChangelog } from '../api/Changelog';
import { JsonFile } from '@rushstack/node-core-library';
import { RushConfiguration } from '../api/RushConfiguration';
/**
* This class represents the collection of change files existing in the repo and provides operations
* for those change files.
*/
export class ChangeFiles {
/**
* Change file path relative to changes folder.
*/
private _files: string[];
private _changesPath: string;
public constructor(changesPath: string) {
this._changesPath = changesPath;
}
/**
* Validate if the newly added change files match the changed packages.
*/
public static validate(
newChangeFilePaths: string[],
changedPackages: string[],
rushConfiguration: RushConfiguration
): void {
const projectsWithChangeDescriptions: Set<string> = new Set<string>();
newChangeFilePaths.forEach((filePath) => {
console.log(`Found change file: ${filePath}`);
const changeFile: IChangeInfo = JsonFile.load(filePath);
if (rushConfiguration.hotfixChangeEnabled) {
if (changeFile && changeFile.changes) {
for (const change of changeFile.changes) {
if (change.type !== 'none' && change.type !== 'hotfix') {
throw new Error(
`Change file ${filePath} specifies a type of '${change.type}' ` +
`but only 'hotfix' and 'none' change types may be used in a branch with 'hotfixChangeEnabled'.`);
}
}
}
}
if (changeFile && changeFile.changes) {
changeFile.changes.forEach(change => projectsWithChangeDescriptions.add(change.packageName));
} else {
throw new Error(`Invalid change file: ${filePath}`);
}
});
const projectsMissingChangeDescriptions: Set<string> = new Set(changedPackages);
projectsWithChangeDescriptions.forEach((name) => projectsMissingChangeDescriptions.delete(name));
if (projectsMissingChangeDescriptions.size > 0) {
const projectsMissingChangeDescriptionsArray: string[] = [];
projectsMissingChangeDescriptions.forEach(name => projectsMissingChangeDescriptionsArray.push(name));
throw new Error([
'The following projects have been changed and require change descriptions, but change descriptions were not ' +
'detected for them:',
...projectsMissingChangeDescriptionsArray.map((projectName) => `- ${projectName}`),
'To resolve this error, run "rush change." This will generate change description files that must be ' +
'committed to source control.'
].join(EOL));
}
}
public static getChangeComments(
newChangeFilePaths: string[]
): Map<string, string[]> {
const changes: Map<string, string[]> = new Map<string, string[]>();
newChangeFilePaths.forEach((filePath) => {
console.log(`Found change file: ${filePath}`);
const changeRequest: IChangeInfo = JsonFile.load(filePath);
if (changeRequest && changeRequest.changes) {
changeRequest.changes!.forEach(change => {
if (!changes.get(change.packageName)) {
changes.set(change.packageName, []);
}
if (change.comment && change.comment.length) {
changes.get(change.packageName)!.push(change.comment);
}
});
} else {
throw new Error(`Invalid change file: ${filePath}`);
}
});
return changes;
}
/**
* Get the array of absolute paths of change files.
*/
public getFiles(): string[] {
if (this._files) {
return this._files;
}
this._files = glob.sync(`${this._changesPath}/**/*.json`);
return this._files || [];
}
/**
* Get the path of changes folder.
*/
public getChangesPath(): string {
return this._changesPath;
}
/**
* Delete all change files
*/
public deleteAll(shouldDelete: boolean, updatedChangelogs?: IChangelog[]): number {
if (updatedChangelogs) {
// Skip changes files if the package's change log is not updated.
const packagesToInclude: Set<string> = new Set<string>();
updatedChangelogs.forEach((changelog) => {
packagesToInclude.add(changelog.name);
});
const filesToDelete: string[] = this.getFiles().filter((filePath) => {
const changeRequest: IChangeInfo = JsonFile.load(filePath);
for (const changeInfo of changeRequest.changes!) {
if (!packagesToInclude.has(changeInfo.packageName)) {
return false;
}
}
return true;
});
return this._deleteFiles(filesToDelete, shouldDelete);
} else {
// Delete all change files.
return this._deleteFiles(this.getFiles(), shouldDelete);
}
}
private _deleteFiles(files: string[], shouldDelete: boolean): number {
if (files.length) {
console.log(
`${EOL}* ` +
`${shouldDelete ? 'DELETING:' : 'DRYRUN: Deleting'} ` +
`${files.length} change file(s).`
);
for (const filePath of files) {
console.log(` - ${filePath}`);
if (shouldDelete) {
Utilities.deleteFile(filePath);
}
}
}
return files.length;
}
}