Skip to content

Commit d097f78

Browse files
committed
[rush] Fix version bump removing policy fields
1 parent ba5ffaa commit d097f78

File tree

4 files changed

+113
-88
lines changed

4 files changed

+113
-88
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "Ensure that `rush version` and `rush publish` preserve all fields in `version-policies-json`.",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush"
10+
}

common/reviews/api/rush-lib.api.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,15 @@ export interface ILogger {
575575

576576
// @public
577577
export class IndividualVersionPolicy extends VersionPolicy {
578-
// Warning: (ae-forgotten-export) The symbol "IIndividualVersionJson" needs to be exported by the entry point index.d.ts
579-
//
580578
// @internal
581579
constructor(versionPolicyJson: IIndividualVersionJson);
582580
bump(bumpType?: BumpType, identifier?: string): void;
583581
ensure(project: IPackageJson, force?: boolean): IPackageJson | undefined;
584-
// @internal
585-
get _json(): IIndividualVersionJson;
586-
readonly lockedMajor: number | undefined;
582+
// Warning: (ae-forgotten-export) The symbol "IIndividualVersionJson" needs to be exported by the entry point index.d.ts
583+
//
584+
// (undocumented)
585+
readonly _json: IIndividualVersionJson;
586+
get lockedMajor(): number | undefined;
587587
validate(versionString: string, packageName: string): void;
588588
}
589589

@@ -929,16 +929,16 @@ export interface _IYarnOptionsJson extends IPackageManagerOptionsJsonBase {
929929

930930
// @public
931931
export class LockStepVersionPolicy extends VersionPolicy {
932-
// Warning: (ae-forgotten-export) The symbol "ILockStepVersionJson" needs to be exported by the entry point index.d.ts
933-
//
934932
// @internal
935933
constructor(versionPolicyJson: ILockStepVersionJson);
936934
bump(bumpType?: BumpType, identifier?: string): void;
937935
ensure(project: IPackageJson, force?: boolean): IPackageJson | undefined;
938-
// @internal
939-
get _json(): ILockStepVersionJson;
940-
readonly mainProject: string | undefined;
941-
readonly nextBump: BumpType | undefined;
936+
// Warning: (ae-forgotten-export) The symbol "ILockStepVersionJson" needs to be exported by the entry point index.d.ts
937+
//
938+
// (undocumented)
939+
_json: ILockStepVersionJson;
940+
get mainProject(): string | undefined;
941+
get nextBump(): BumpType | undefined;
942942
update(newVersionString: string): boolean;
943943
validate(versionString: string, packageName: string): void;
944944
get version(): string;
@@ -1603,21 +1603,21 @@ export class SubspacesConfiguration {
16031603

16041604
// @public
16051605
export abstract class VersionPolicy {
1606-
// Warning: (ae-forgotten-export) The symbol "IVersionPolicyJson" needs to be exported by the entry point index.d.ts
1607-
//
16081606
// @internal
16091607
constructor(versionPolicyJson: IVersionPolicyJson);
16101608
abstract bump(bumpType?: BumpType, identifier?: string): void;
1611-
readonly definitionName: VersionPolicyDefinitionName;
1609+
get definitionName(): VersionPolicyDefinitionName;
16121610
abstract ensure(project: IPackageJson, force?: boolean): IPackageJson | undefined;
1613-
readonly exemptFromRushChange: boolean;
1614-
readonly includeEmailInChangeFile: boolean;
1611+
get exemptFromRushChange(): boolean;
1612+
get includeEmailInChangeFile(): boolean;
16151613
get isLockstepped(): boolean;
1614+
// Warning: (ae-forgotten-export) The symbol "IVersionPolicyJson" needs to be exported by the entry point index.d.ts
1615+
//
16161616
// @internal
1617-
abstract get _json(): IVersionPolicyJson;
1617+
readonly _json: IVersionPolicyJson;
16181618
// @internal
16191619
static load(versionPolicyJson: IVersionPolicyJson): VersionPolicy | undefined;
1620-
readonly policyName: string;
1620+
get policyName(): string;
16211621
setDependenciesBeforeCommit(packageName: string, configuration: RushConfiguration): void;
16221622
setDependenciesBeforePublish(packageName: string, configuration: RushConfiguration): void;
16231623
abstract validate(versionString: string, packageName: string): void;

libraries/rush-lib/src/api/VersionPolicy.ts

Lines changed: 42 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import {
99
type ILockStepVersionJson,
1010
type IIndividualVersionJson,
1111
VersionFormatForCommit,
12-
VersionFormatForPublish,
13-
type IVersionPolicyDependencyJson
12+
VersionFormatForPublish
1413
} from './VersionPolicyConfiguration';
1514
import type { PackageJsonEditor } from './PackageJsonEditor';
1615
import type { RushConfiguration } from './RushConfiguration';
@@ -57,42 +56,54 @@ export enum VersionPolicyDefinitionName {
5756
* @public
5857
*/
5958
export abstract class VersionPolicy {
60-
private _versionFormatForCommit: VersionFormatForCommit;
61-
private _versionFormatForPublish: VersionFormatForPublish;
59+
/**
60+
* Serialized json for the policy
61+
*
62+
* @internal
63+
*/
64+
public readonly _json: IVersionPolicyJson;
65+
66+
private get _versionFormatForCommit(): VersionFormatForCommit {
67+
return this._json.dependencies?.versionFormatForCommit ?? VersionFormatForCommit.original;
68+
}
69+
70+
private get _versionFormatForPublish(): VersionFormatForPublish {
71+
return this._json.dependencies?.versionFormatForPublish ?? VersionFormatForPublish.original;
72+
}
6273

6374
/**
6475
* Version policy name
6576
*/
66-
public readonly policyName: string;
77+
public get policyName(): string {
78+
return this._json.policyName;
79+
}
6780

6881
/**
6982
* Version policy definition name
7083
*/
71-
public readonly definitionName: VersionPolicyDefinitionName;
84+
public get definitionName(): VersionPolicyDefinitionName {
85+
return Enum.getValueByKey(VersionPolicyDefinitionName, this._json.definitionName);
86+
}
7287

7388
/**
7489
* Determines if a version policy wants to opt out of changelog files.
7590
*/
76-
public readonly exemptFromRushChange: boolean;
91+
public get exemptFromRushChange(): boolean {
92+
return this._json.exemptFromRushChange ?? false;
93+
}
7794

7895
/**
7996
* Determines if a version policy wants to opt in to including email.
8097
*/
81-
public readonly includeEmailInChangeFile: boolean;
98+
public get includeEmailInChangeFile(): boolean {
99+
return this._json.includeEmailInChangeFile ?? false;
100+
}
82101

83102
/**
84103
* @internal
85104
*/
86105
public constructor(versionPolicyJson: IVersionPolicyJson) {
87-
this.policyName = versionPolicyJson.policyName;
88-
this.definitionName = Enum.getValueByKey(VersionPolicyDefinitionName, versionPolicyJson.definitionName);
89-
this.exemptFromRushChange = versionPolicyJson.exemptFromRushChange || false;
90-
this.includeEmailInChangeFile = versionPolicyJson.includeEmailInChangeFile || false;
91-
92-
const jsonDependencies: IVersionPolicyDependencyJson = versionPolicyJson.dependencies || {};
93-
this._versionFormatForCommit = jsonDependencies.versionFormatForCommit || VersionFormatForCommit.original;
94-
this._versionFormatForPublish =
95-
jsonDependencies.versionFormatForPublish || VersionFormatForPublish.original;
106+
this._json = versionPolicyJson;
96107
}
97108

98109
/**
@@ -140,13 +151,6 @@ export abstract class VersionPolicy {
140151
*/
141152
public abstract bump(bumpType?: BumpType, identifier?: string): void;
142153

143-
/**
144-
* Serialized json for the policy
145-
*
146-
* @internal
147-
*/
148-
public abstract get _json(): IVersionPolicyJson;
149-
150154
/**
151155
* Validates the specified version and throws if the version does not satisfy the policy.
152156
*
@@ -211,34 +215,34 @@ export abstract class VersionPolicy {
211215
* @public
212216
*/
213217
export class LockStepVersionPolicy extends VersionPolicy {
218+
public declare _json: ILockStepVersionJson;
214219
private _version: semver.SemVer;
215220

216221
/**
217222
* The type of bump for next bump.
218223
*/
219224
// nextBump is probably not needed. It can be prerelease only.
220225
// Other types of bumps can be passed in as a parameter to bump method, so can identifier.
221-
public readonly nextBump: BumpType | undefined;
226+
public get nextBump(): BumpType | undefined {
227+
return this._json.nextBump !== undefined ? Enum.getValueByKey(BumpType, this._json.nextBump) : undefined;
228+
}
222229

223230
/**
224231
* The main project for the version policy.
225232
*
226233
* If the value is provided, change logs will only be generated in that project.
227234
* If the value is not provided, change logs will be hosted in each project associated with the policy.
228235
*/
229-
public readonly mainProject: string | undefined;
236+
public get mainProject(): string | undefined {
237+
return this._json.mainProject;
238+
}
230239

231240
/**
232241
* @internal
233242
*/
234243
public constructor(versionPolicyJson: ILockStepVersionJson) {
235244
super(versionPolicyJson);
236245
this._version = new semver.SemVer(versionPolicyJson.version);
237-
this.nextBump =
238-
versionPolicyJson.nextBump !== undefined
239-
? Enum.getValueByKey(BumpType, versionPolicyJson.nextBump)
240-
: undefined;
241-
this.mainProject = versionPolicyJson.mainProject;
242246
}
243247

244248
/**
@@ -248,26 +252,6 @@ export class LockStepVersionPolicy extends VersionPolicy {
248252
return this._version.format();
249253
}
250254

251-
/**
252-
* Serialized json for this policy
253-
*
254-
* @internal
255-
*/
256-
public get _json(): ILockStepVersionJson {
257-
const json: ILockStepVersionJson = {
258-
policyName: this.policyName,
259-
definitionName: VersionPolicyDefinitionName[this.definitionName],
260-
version: this.version
261-
};
262-
if (this.nextBump !== undefined) {
263-
json.nextBump = BumpType[this.nextBump];
264-
}
265-
if (this.mainProject !== undefined) {
266-
json.mainProject = this.mainProject;
267-
}
268-
return json;
269-
}
270-
271255
/**
272256
* Returns an updated package json that satisfies the version policy.
273257
*
@@ -303,6 +287,7 @@ export class LockStepVersionPolicy extends VersionPolicy {
303287
}
304288

305289
this._version.inc(this._getReleaseType(nextBump), identifier);
290+
this._json.version = this.version;
306291
}
307292

308293
/**
@@ -315,6 +300,7 @@ export class LockStepVersionPolicy extends VersionPolicy {
315300
return false;
316301
}
317302
this._version = newVersion;
303+
this._json.version = this.version;
318304
return true;
319305
}
320306

@@ -348,33 +334,20 @@ export class LockStepVersionPolicy extends VersionPolicy {
348334
* @public
349335
*/
350336
export class IndividualVersionPolicy extends VersionPolicy {
337+
public declare readonly _json: IIndividualVersionJson;
338+
351339
/**
352340
* The major version that has been locked
353341
*/
354-
public readonly lockedMajor: number | undefined;
342+
public get lockedMajor(): number | undefined {
343+
return this._json.lockedMajor;
344+
}
355345

356346
/**
357347
* @internal
358348
*/
359349
public constructor(versionPolicyJson: IIndividualVersionJson) {
360350
super(versionPolicyJson);
361-
this.lockedMajor = versionPolicyJson.lockedMajor;
362-
}
363-
364-
/**
365-
* Serialized json for this policy
366-
*
367-
* @internal
368-
*/
369-
public get _json(): IIndividualVersionJson {
370-
const json: IIndividualVersionJson = {
371-
policyName: this.policyName,
372-
definitionName: VersionPolicyDefinitionName[this.definitionName]
373-
};
374-
if (this.lockedMajor !== undefined) {
375-
json.lockedMajor = this.lockedMajor;
376-
}
377-
return json;
378351
}
379352

380353
/**

libraries/rush-lib/src/api/test/VersionPolicy.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33

44
import type { IPackageJson } from '@rushstack/node-core-library';
55

6-
import { VersionPolicyConfiguration } from '../VersionPolicyConfiguration';
6+
import {
7+
type ILockStepVersionJson,
8+
VersionFormatForCommit,
9+
VersionFormatForPublish,
10+
VersionPolicyConfiguration,
11+
type IIndividualVersionJson
12+
} from '../VersionPolicyConfiguration';
713
import { VersionPolicy, LockStepVersionPolicy, IndividualVersionPolicy, BumpType } from '../VersionPolicy';
814

915
describe(VersionPolicy.name, () => {
@@ -113,6 +119,25 @@ describe(VersionPolicy.name, () => {
113119
lockStepVersionPolicy.update(newVersion);
114120
expect(lockStepVersionPolicy.version).toEqual(newVersion);
115121
});
122+
123+
it('preserves fields', () => {
124+
const originalJson: ILockStepVersionJson = {
125+
definitionName: 'lockStepVersion',
126+
policyName: 'test',
127+
dependencies: {
128+
versionFormatForCommit: VersionFormatForCommit.original,
129+
versionFormatForPublish: VersionFormatForPublish.original
130+
},
131+
exemptFromRushChange: true,
132+
includeEmailInChangeFile: true,
133+
version: '1.1.0',
134+
mainProject: 'main-project',
135+
nextBump: 'major'
136+
};
137+
138+
const nextJson: ILockStepVersionJson = new LockStepVersionPolicy(originalJson)._json;
139+
expect(nextJson).toMatchObject(originalJson);
140+
});
116141
});
117142

118143
describe(IndividualVersionPolicy.name, () => {
@@ -159,5 +184,22 @@ describe(VersionPolicy.name, () => {
159184
individualVersionPolicy.ensure(originalPackageJson);
160185
}).toThrow();
161186
});
187+
188+
it('preserves fields', () => {
189+
const originalJson: IIndividualVersionJson = {
190+
definitionName: 'individualVersion',
191+
policyName: 'test',
192+
dependencies: {
193+
versionFormatForCommit: VersionFormatForCommit.original,
194+
versionFormatForPublish: VersionFormatForPublish.original
195+
},
196+
exemptFromRushChange: true,
197+
includeEmailInChangeFile: true,
198+
lockedMajor: 3
199+
};
200+
201+
const nextJson: IIndividualVersionJson = new IndividualVersionPolicy(originalJson)._json;
202+
expect(nextJson).toMatchObject(originalJson);
203+
});
162204
});
163205
});

0 commit comments

Comments
 (0)