Skip to content

Commit

Permalink
Clean up git email validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
iclanton committed Mar 30, 2024
1 parent 8f2fc47 commit 0062d82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
26 changes: 11 additions & 15 deletions libraries/rush-lib/src/logic/Git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { RushConstants } from './RushConstants';

export const DEFAULT_GIT_TAG_SEPARATOR: string = '_';

interface IResultOrError<TResult> {
export interface IResultOrError<TResult> {
error?: Error;
result?: TResult;
}
Expand Down Expand Up @@ -89,18 +89,6 @@ export class Git {
}
}

/**
* If a Git email address is configured and is nonempty, this returns it.
* Otherwise, undefined is returned.
*/
public async tryGetGitEmailAsync(): Promise<string | undefined> {
const emailResult: IResultOrError<string> = await this._tryGetGitEmailAsync();
if (emailResult.result !== undefined && emailResult.result.length > 0) {
return emailResult.result;
}
return undefined;
}

/**
* If a Git email address is configured and is nonempty, this returns it.
* Otherwise, configuration instructions are printed to the console,
Expand All @@ -109,7 +97,11 @@ export class Git {
public async getGitEmailAsync(): Promise<string> {
// Determine the user's account
// Ex: "bob@example.com"
const emailResult: IResultOrError<string> = await this._tryGetGitEmailAsync();
const emailResult: IResultOrError<string> = await this.tryGetGitEmailAsync();
return this.validateGitEmail(emailResult);
}

public validateGitEmail(emailResult: IResultOrError<string>): string | never {
if (emailResult.error) {
// eslint-disable-next-line no-console
console.log(
Expand Down Expand Up @@ -518,7 +510,11 @@ export class Git {
return result;
}

private async _tryGetGitEmailAsync(): Promise<IResultOrError<string>> {
/**
* Returns an object containing either the result of the `git config user.email`
* command or an error.
*/
public async tryGetGitEmailAsync(): Promise<IResultOrError<string>> {
if (this._gitEmailResult === undefined) {
const gitPath: string = this.getGitPathOrThrow();
try {
Expand Down
7 changes: 4 additions & 3 deletions libraries/rush-lib/src/logic/policy/GitEmailPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Colorize } from '@rushstack/terminal';

import type { RushConfiguration } from '../../api/RushConfiguration';
import { Utilities } from '../../utilities/Utilities';
import { Git } from '../Git';
import { Git, type IResultOrError } from '../Git';
import { RushConstants } from '../RushConstants';
import type { IPolicyValidatorOptions } from './PolicyValidator';

Expand Down Expand Up @@ -34,10 +34,11 @@ export async function validateAsync(
return;
}

const getUserEmailResult: IResultOrError<string> = await git.tryGetGitEmailAsync();
// If there isn't a Git policy, then we don't care whether the person configured
// a Git email address at all. This helps people who don't
if (rushConfiguration.gitAllowedEmailRegExps.length === 0) {
if (git.tryGetGitEmailAsync() === undefined) {
if (getUserEmailResult.result === undefined) {
return;
}

Expand All @@ -47,7 +48,7 @@ export async function validateAsync(

let userEmail: string;
try {
userEmail = await git.getGitEmailAsync();
userEmail = git.validateGitEmail(getUserEmailResult);

// sanity check; a valid email should not contain any whitespace
// if this fails, then we have another issue to report
Expand Down

0 comments on commit 0062d82

Please sign in to comment.