Skip to content

Commit 5fb9f45

Browse files
author
Nicholas Pape
authored
Fixes an issue where "rush version" would fail with a bad message if Git user email not specified (microsoft#740)
* Fix an issue where 'rush version' would fail with a useless error message if the Git user email is not specified * Changefile * PR feedback
1 parent 4ff66b7 commit 5fb9f45

3 files changed

Lines changed: 44 additions & 24 deletions

File tree

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import { BumpType, LockStepVersionPolicy } from '../../api/VersionPolicy';
1212
import { VersionPolicyConfiguration } from '../../api/VersionPolicyConfiguration';
1313
import { RushConfiguration } from '../../api/RushConfiguration';
14-
import { Utilities } from '../../utilities/Utilities';
1514
import { VersionControl } from '../../utilities/VersionControl';
1615
import { VersionMismatchFinder } from '../../api/VersionMismatchFinder';
1716
import { RushCommandLineParser } from '../RushCommandLineParser';
@@ -91,15 +90,22 @@ export class VersionAction extends BaseRushAction {
9190
}
9291

9392
protected run(): Promise<void> {
93+
// try to get the user email
94+
const userEmail: string | undefined = GitPolicy.getUserEmail(this.rushConfiguration);
95+
96+
if (!userEmail) {
97+
return Promise.reject(undefined);
98+
}
99+
94100
if (!this._bypassPolicy.value) {
95-
if (!GitPolicy.check(this.rushConfiguration)) {
96-
process.exit(1);
97-
return Promise.resolve();
101+
if (!GitPolicy.check(this.rushConfiguration, userEmail)) {
102+
return Promise.reject(undefined);
98103
}
99104
}
100105
this._validateInput();
101106

102-
this._versionManager = new VersionManager(this.rushConfiguration, this._getUserEmail());
107+
this._versionManager = new VersionManager(this.rushConfiguration, userEmail);
108+
103109
if (this._ensureVersionPolicy.value) {
104110
this._overwritePolicyVersionIfNeeded();
105111
const tempBranch: string = 'version/ensure-' + new Date().getTime();
@@ -188,11 +194,6 @@ export class VersionAction extends BaseRushAction {
188194
}
189195
}
190196

191-
private _getUserEmail(): string {
192-
return Utilities.executeCommandAndCaptureOutput('git',
193-
['config', 'user.email'], '.').trim();
194-
}
195-
196197
private _gitProcess(tempBranch: string): void {
197198
// Validate the result before commit.
198199
this._validateResult();

‎apps/rush-lib/src/logic/GitPolicy.ts‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@ import { RushConfiguration } from '../api/RushConfiguration';
88
import { Utilities } from '../utilities/Utilities';
99

1010
export class GitPolicy {
11-
public static check(rushConfiguration: RushConfiguration): boolean {
12-
if (rushConfiguration.gitAllowedEmailRegExps.length === 0) {
13-
return true;
14-
}
15-
16-
console.log('Checking Git policy for this repository.' + os.EOL);
1711

12+
public static getUserEmail(rushConfiguration: RushConfiguration): string | undefined {
1813
// Determine the user's account
1914
// Ex: "bob@example.com"
2015
let userEmail: string;
@@ -33,26 +28,39 @@ If you didn't configure your e-mail yet, try something like this:`);
3328
console.log(colors.cyan(
3429
`
3530
git config --local user.name "Mr. Example"
36-
git config --local user.email "${rushConfiguration.gitSampleEmail}"
31+
git config --local user.email "${rushConfiguration.gitSampleEmail || 'example@contoso.com'}"
3732
`));
3833

3934
console.log(colors.red('Aborting, so you can go fix your settings. (Or use --bypass-policy to skip.)'));
4035

41-
return false;
36+
return undefined;
4237
}
4338

39+
return userEmail;
40+
}
41+
42+
public static check(rushConfiguration: RushConfiguration, userEmail?: string): boolean {
43+
if (rushConfiguration.gitAllowedEmailRegExps.length === 0) {
44+
return true;
45+
}
46+
47+
console.log('Checking Git policy for this repository.' + os.EOL);
48+
49+
userEmail = userEmail || GitPolicy.getUserEmail(rushConfiguration);
50+
4451
// sanity check; a valid e-mail should not contain any whitespace
45-
if (!userEmail.match(/^\S+$/g)) {
46-
throw new Error('The gitPolicy check failed because "git config" returned unexpected output:'
47-
+ os.EOL + `"${userEmail}"`);
52+
if (!userEmail || !userEmail.match(/^\S+$/g)) {
53+
console.log(colors.red('The gitPolicy check failed because "git config" returned unexpected output:'
54+
+ os.EOL + `"${userEmail}"`));
55+
return false;
4856
}
4957

5058
for (const pattern of rushConfiguration.gitAllowedEmailRegExps) {
5159
const regex: RegExp = new RegExp('^' + pattern + '$', 'i');
52-
if (userEmail.match(regex)) {
60+
if (!userEmail.match(regex)) {
5361
// For debugging:
54-
// console.log(`${userEmail} matched pattern: "${pattern}"`);
55-
return true;
62+
// console.log(`${userEmail} did not match pattern: "${pattern}"`);
63+
return false;
5664
}
5765
}
5866

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Fix an issue where \"rush version\" would fail with a useless error message if the Git user email is not specified.",
5+
"packageName": "@microsoft/rush",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "nickpape-msft@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)