Skip to content

Commit 3615b1f

Browse files
lambertwangqz2017
authored andcommitted
Added hotfix change option for rush (microsoft#434)
1 parent 94646b1 commit 3615b1f

22 files changed

Lines changed: 354 additions & 47 deletions

‎apps/rush-lib/src/cli/actions/ChangeAction.ts‎

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,37 @@ export default class ChangeAction extends BaseRushAction {
4242
private _prompt: inquirer.PromptModule;
4343

4444
constructor(parser: RushCommandLineParser) {
45+
const documentation: string[] = [
46+
'Asks a series of questions and then generates a <branchname>-<timstamp>.json file ' +
47+
'in the common folder. The `publish` command will consume these files and perform the proper ' +
48+
'version bumps. Note these changes will eventually be published in a changelog.md file in each package.',
49+
'',
50+
'The possible types of changes are: ',
51+
'',
52+
'MAJOR - these are breaking changes that are not backwards compatible. ' +
53+
'Examples are: renaming a public class, adding/removing a non-optional ' +
54+
'parameter from a public API, or renaming an variable or function that ' +
55+
'is exported.',
56+
'',
57+
'MINOR - these are changes that are backwards compatible (but not ' +
58+
'forwards compatible). Examples are: adding a new public API or adding an ' +
59+
'optional parameter to a public API',
60+
'',
61+
'PATCH - these are changes that are backwards and forwards compatible. ' +
62+
'Examples are: Modifying a private API or fixing a bug in the logic ' +
63+
'of how an existing API works.',
64+
'',
65+
'HOTFIX (EXPERIMENTAL) - these are changes that are hotfixes targeting a ' +
66+
'specific older version of the package. When a hotfix change is added, ' +
67+
'other changes will not be able to increment the version number.' +
68+
'Enable this feature by setting \'hotfixChangeEnabled\' in your rush.json.',
69+
''
70+
];
4571
super({
4672
actionVerb: 'change',
4773
summary: 'Records changes made to projects, indicating how the package version number should be bumped ' +
4874
'for the next publish.',
49-
documentation: ['Asks a series of questions and then generates a <branchname>-<timstamp>.json file ' +
50-
'in the common folder. The `publish` command will consume these files and perform the proper ' +
51-
'version bumps. Note these changes will eventually be published in a changelog.md file in each package.',
52-
'',
53-
'The possible types of changes are: ',
54-
'',
55-
'MAJOR - these are breaking changes that are not backwards compatible. ' +
56-
'Examples are: renaming a public class, adding/removing a non-optional ' +
57-
'parameter from a public API, or renaming an variable or function that ' +
58-
'is exported.',
59-
'',
60-
'MINOR - these are changes that are backwards compatible (but not ' +
61-
'forwards compatible). Examples are: adding a new public API or adding an ' +
62-
'optional parameter to a public API',
63-
'',
64-
'PATCH - these are changes that are backwards and forwards compatible. ' +
65-
'Examples are: Modifying a private API or fixing a bug in the logic ' +
66-
'of how an existing API works.',
67-
''].join(os.EOL)
75+
documentation: documentation.join(os.EOL)
6876
});
6977
this._parser = parser;
7078
}
@@ -292,6 +300,11 @@ export default class ChangeAction extends BaseRushAction {
292300
'patch': 'patch - for changes that do not affect compatibility, e.g. fixing a bug'
293301
};
294302

303+
if (this.rushConfiguration.hotfixChangeEnabled) {
304+
// tslint:disable-next-line:no-string-literal
305+
bumpOptions['hotfix'] = 'hotfix - for changes that need to be published in a separate hotfix package';
306+
}
307+
295308
if (versionPolicy) {
296309
if (versionPolicy.definitionName === VersionPolicyDefinitionName.lockStepVersion) {
297310
// No need to ask for bump types if project is lockstep versioned.

‎apps/rush-lib/src/cli/actions/PublishAction.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export default class PublishAction extends BaseRushAction {
4040
private _prereleaseToken: PrereleaseToken;
4141
private _versionPolicy: CommandLineStringParameter;
4242

43+
private _hotfixTagOverride: string;
44+
4345
constructor(parser: RushCommandLineParser) {
4446
super({
4547
actionVerb: 'publish',
@@ -103,7 +105,7 @@ export default class PublishAction extends BaseRushAction {
103105
description:
104106
`The tag option to pass to npm publish. By default npm will publish using the 'latest' tag, even if ` +
105107
`the package is older than the current latest, so in publishing workflows for older releases, providing ` +
106-
`a tag is important.`
108+
`a tag is important. When hotfix changes are made, this parameter defaults to 'hotfix'.`
107109
});
108110
this._includeAll = this.defineFlagParameter({
109111
parameterLongName: '--include-all',
@@ -186,6 +188,14 @@ export default class PublishAction extends BaseRushAction {
186188
git.commit();
187189
git.push(tempBranch);
188190

191+
// Override tag parameter if there is a hotfix change.
192+
for (const change of orderedChanges) {
193+
if (change.changeType === ChangeType.hotfix) {
194+
this._hotfixTagOverride = 'hotfix';
195+
break;
196+
}
197+
}
198+
189199
// NPM publish the things that need publishing.
190200
for (const change of orderedChanges) {
191201
if (change.changeType && change.changeType > ChangeType.dependency) {
@@ -263,6 +273,8 @@ export default class PublishAction extends BaseRushAction {
263273

264274
if (this._npmTag.value) {
265275
args.push(`--tag`, this._npmTag.value);
276+
} else if (this._hotfixTagOverride) {
277+
args.push(`--tag`, this._hotfixTagOverride);
266278
}
267279

268280
if (this._force.value) {

‎apps/rush-lib/src/cli/utilities/ChangelogGenerator.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,25 @@ import {
1919
IChangeLogComment
2020
} from '../../data/Changelog';
2121
import RushConfigurationProject from '../../data/RushConfigurationProject';
22+
import RushConfiguration from '../../data/RushConfiguration';
2223

2324
const CHANGELOG_JSON: string = 'CHANGELOG.json';
2425
const CHANGELOG_MD: string = 'CHANGELOG.md';
2526
const EOL: string = '\n';
2627

2728
export default class ChangelogGenerator {
29+
private static _rushConfiguration: RushConfiguration;
30+
31+
public static get rushConfiguration(): RushConfiguration {
32+
if (!ChangelogGenerator._rushConfiguration) {
33+
ChangelogGenerator._rushConfiguration = RushConfiguration.loadFromDefaultLocation();
34+
}
35+
return ChangelogGenerator._rushConfiguration;
36+
}
37+
38+
public static set rushConfiguration(newConfiguration: RushConfiguration) {
39+
ChangelogGenerator._rushConfiguration = newConfiguration;
40+
}
2841

2942
/**
3043
* Updates the appropriate changelogs with the given changes.
@@ -43,7 +56,11 @@ export default class ChangelogGenerator {
4356
// Changelogs should only be generated for publishable projects.
4457
// Do not update changelog or delete the change files for prerelease.
4558
// Save them for the official release.
46-
if (!!project && project.shouldPublish && !semver.prerelease(project.packageJson.version)) {
59+
// Unless the package is a hotfix, in which case do delete the change files.
60+
if (!!project && project.shouldPublish &&
61+
(!semver.prerelease(project.packageJson.version) ||
62+
allChanges[packageName].changeType === ChangeType.hotfix)
63+
) {
4764
const changeLog: IChangelog | undefined = ChangelogGenerator.updateIndividualChangelog(
4865
allChanges[packageName],
4966
project.projectFolder,
@@ -211,6 +228,12 @@ export default class ChangelogGenerator {
211228
'Patches',
212229
entry.comments[ChangeType[ChangeType.patch]]);
213230

231+
if (this.rushConfiguration.hotfixChangeEnabled) {
232+
comments += ChangelogGenerator._getChangeComments(
233+
'Hotfixes',
234+
entry.comments[ChangeType[ChangeType.hotfix]]);
235+
}
236+
214237
if (!comments) {
215238
markdown += ((changelog.entries.length === index + 1) ?
216239
'*Initial release*' :

‎apps/rush-lib/src/cli/utilities/PublishUtilities.ts‎

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ChangeType
1818
} from '../../data/ChangeManagement';
1919
import RushConfigurationProject from '../../data/RushConfigurationProject';
20+
import RushConfiguration from '../../data/RushConfiguration';
2021
import Utilities from '../../utilities/Utilities';
2122
import { execSync } from 'child_process';
2223
import PrereleaseToken from './PrereleaseToken';
@@ -27,6 +28,19 @@ export interface IChangeInfoHash {
2728
}
2829

2930
export default class PublishUtilities {
31+
private static _rushConfiguration: RushConfiguration;
32+
33+
public static get rushConfiguration(): RushConfiguration {
34+
if (!PublishUtilities._rushConfiguration) {
35+
PublishUtilities._rushConfiguration = RushConfiguration.loadFromDefaultLocation();
36+
}
37+
return PublishUtilities._rushConfiguration;
38+
}
39+
40+
public static set rushConfiguration(newConfiguration: RushConfiguration) {
41+
PublishUtilities._rushConfiguration = newConfiguration;
42+
}
43+
3044
/**
3145
* Finds change requests in the given folder.
3246
* @param changesPath Path to the changes folder.
@@ -85,9 +99,10 @@ export default class PublishUtilities {
8599
if (skipVersionBump) {
86100
change.newVersion = pkg.version;
87101
} else {
102+
// For hotfix changes, do not re-write new version
88103
change.newVersion = (change.changeType! >= ChangeType.patch) ?
89104
semver.inc(pkg.version, PublishUtilities._getReleaseType(change.changeType!)) :
90-
pkg.version;
105+
(change.changeType === ChangeType.hotfix ? change.newVersion : pkg.version);
91106
}
92107

93108
if (deps) {
@@ -205,7 +220,6 @@ export default class PublishUtilities {
205220
): string {
206221
const currentDependencyVersion: string = dependencies[dependencyName];
207222
let newDependencyVersion: string;
208-
209223
if (PublishUtilities.isRangeDependency(currentDependencyVersion)) {
210224
newDependencyVersion = PublishUtilities._getNewRangeDependency(newProjectVersion);
211225
} else if (currentDependencyVersion.lastIndexOf('~', 0) === 0) {
@@ -226,6 +240,8 @@ export default class PublishUtilities {
226240
return 'minor';
227241
case ChangeType.patch:
228242
return 'patch';
243+
case ChangeType.hotfix:
244+
return 'prerelease';
229245
default:
230246
throw new Error(`Wrong change type ${changeType}`);
231247
}
@@ -353,7 +369,7 @@ export default class PublishUtilities {
353369
// For prelease, the newVersion needs to be appended with prerelease name.
354370
// And dependency should specify the specific prerelease version.
355371
dependencies[depName] = PublishUtilities._getChangeInfoNewVersion(depChange, prereleaseToken);
356-
} else if (depChange && depChange.changeType! >= ChangeType.patch) {
372+
} else if (depChange && depChange.changeType! >= ChangeType.hotfix) {
357373
PublishUtilities._updateDependencyVersion(
358374
packageName,
359375
dependencies,
@@ -431,6 +447,13 @@ export default class PublishUtilities {
431447

432448
const oldChangeType: ChangeType = currentChange.changeType!;
433449

450+
if (oldChangeType === ChangeType.hotfix && change.changeType! > oldChangeType) {
451+
throw new Error(`Cannot apply ${this._getReleaseType(change.changeType!)} change after hotfix on same package`);
452+
}
453+
if (change.changeType! === ChangeType.hotfix && oldChangeType > change.changeType!) {
454+
throw new Error(`Cannot apply hotfix alongside ${this._getReleaseType(oldChangeType!)} change on same package`);
455+
}
456+
434457
currentChange.changeType = Math.max(currentChange.changeType!, change.changeType!);
435458
currentChange.changes!.push(change);
436459

@@ -444,10 +467,27 @@ export default class PublishUtilities {
444467
hasChanged = false;
445468
currentChange.changeType = ChangeType.none;
446469
} else {
447-
currentChange.newVersion = change.changeType! >= ChangeType.patch ?
448-
semver.inc(pkg.version, PublishUtilities._getReleaseType(currentChange.changeType!)) :
449-
pkg.version;
450-
currentChange.newRangeDependency = PublishUtilities._getNewRangeDependency(currentChange.newVersion);
470+
if (change.changeType === ChangeType.hotfix) {
471+
const prereleaseComponents: string[] = semver.prerelease(pkg.version);
472+
if (!this.rushConfiguration.hotfixChangeEnabled) {
473+
throw new Error(`Cannot add hotfix change; hotfixChangeEnabled is false in configuration.`);
474+
}
475+
476+
currentChange.newVersion = pkg.version;
477+
if (!prereleaseComponents) {
478+
currentChange.newVersion += '-hotfix';
479+
}
480+
currentChange.newVersion = semver.inc(currentChange.newVersion, 'prerelease');
481+
} else {
482+
currentChange.newVersion = change.changeType! >= ChangeType.patch ?
483+
semver.inc(pkg.version, PublishUtilities._getReleaseType(currentChange.changeType!)) :
484+
pkg.version;
485+
}
486+
487+
// If hotfix change, force new range dependency to be the exact new version
488+
currentChange.newRangeDependency = change.changeType === ChangeType.hotfix ?
489+
currentChange.newVersion :
490+
PublishUtilities._getNewRangeDependency(currentChange.newVersion);
451491
}
452492
return hasChanged;
453493
}
@@ -465,7 +505,7 @@ export default class PublishUtilities {
465505

466506
// Iterate through all downstream dependencies for the package.
467507
if (downstreamNames) {
468-
if ((change.changeType! >= ChangeType.patch) ||
508+
if ((change.changeType! >= ChangeType.hotfix) ||
469509
(prereleaseToken && prereleaseToken.hasValue)) {
470510
for (const depName of downstreamNames) {
471511
const pkg: IPackageJson = allPackages.get(depName)!.packageJson;
@@ -497,11 +537,17 @@ export default class PublishUtilities {
497537
// If the version range exists and has not yet been updated to this version, update it.
498538
if (requiredVersion !== change.newRangeDependency || alwaysUpdate) {
499539

500-
// Either it already satisfies the new version, or doesn't.
501-
// If not, the downstream dep needs to be republished.
502-
const changeType: ChangeType = semver.satisfies(change.newVersion!, requiredVersion) ?
503-
ChangeType.dependency :
504-
ChangeType.patch;
540+
let changeType: ChangeType;
541+
// Propagate hotfix changes to dependencies
542+
if (change.changeType === ChangeType.hotfix) {
543+
changeType = ChangeType.hotfix;
544+
} else {
545+
// Either it already satisfies the new version, or doesn't.
546+
// If not, the downstream dep needs to be republished.
547+
changeType = semver.satisfies(change.newVersion!, requiredVersion) ?
548+
ChangeType.dependency :
549+
ChangeType.patch;
550+
}
505551

506552
const hasChanged: boolean = PublishUtilities._addChange({
507553
packageName: parentPackageName,
@@ -545,8 +591,8 @@ export default class PublishUtilities {
545591
packageName: packageName,
546592
changeType: ChangeType.dependency,
547593
comment:
548-
`Updating dependency "${dependencyName}" from \`${currentDependencyVersion}\`` +
549-
` to \`${dependencies[dependencyName]}\``
594+
`Updating dependency "${dependencyName}" from \`${currentDependencyVersion}\`` +
595+
` to \`${dependencies[dependencyName]}\``
550596
},
551597
allChanges,
552598
allPackages

‎apps/rush-lib/src/cli/utilities/test/ChangeFiles.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,11 @@ describe('ChangeFiles', () => {
9797
expect(changeFiles.deleteAll(false, updatedChangelogs)).equals(2,
9898
'Changes files for a and b should be deleted.');
9999
});
100+
101+
it('delete all files when there are hotfixes', () => {
102+
const changesPath: string = path.join(__dirname, 'multipleHotfixChanges');
103+
const changeFiles: ChangeFiles = new ChangeFiles(changesPath);
104+
expect(changeFiles.deleteAll(false)).equals(3);
105+
});
100106
});
101107
});

‎apps/rush-lib/src/cli/utilities/test/ChangelogGenerator.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,32 @@ describe('updateChangelogs', () => {
245245
expect(updatedChangeLogs.length).eqls(1);
246246
expect(updatedChangeLogs[0].name).eqls('b');
247247
});
248+
249+
it('writes changelog for hotfix changes', () => {
250+
const changeHash: IChangeInfoHash = {};
251+
// Package a is a hotfix
252+
changeHash['a'] = {
253+
packageName: 'a',
254+
changeType: ChangeType.hotfix,
255+
newVersion: '1.0.1-hotfix.1',
256+
changes: []
257+
};
258+
// Package b is not a hotfix
259+
changeHash['b'] = {
260+
packageName: 'b',
261+
changeType: ChangeType.patch,
262+
newVersion: '1.0.1',
263+
changes: []
264+
};
265+
// Makes package 'a' hotfix package.
266+
const rushProjectA: RushConfigurationProject = rushConfiguration.projectsByName.get('a')!;
267+
rushProjectA.packageJson.version = '1.0.1-hotfix.1';
268+
269+
const updatedChangeLogs: IChangelog[] = ChangelogGenerator.updateChangelogs(
270+
changeHash, rushConfiguration.projectsByName, false);
271+
expect(updatedChangeLogs.length).eqls(2);
272+
expect(updatedChangeLogs[0].name).eqls('a');
273+
expect(updatedChangeLogs[1].name).eqls('b');
274+
});
248275
/* tslint:enable:no-string-literal */
249276
});

0 commit comments

Comments
 (0)