|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import * as colors from 'colors'; |
| 5 | +import * as os from 'os'; |
| 6 | + |
| 7 | +import { GitPolicy } from './GitPolicy'; |
| 8 | +import { RushConfiguration } from '../../api/RushConfiguration'; |
| 9 | +import { AlreadyReportedError } from '../../utilities/AlreadyReportedError'; |
| 10 | +import { Utilities } from '../../utilities/Utilities'; |
| 11 | + |
| 12 | +interface IResultOrError<TResult> { |
| 13 | + error?: Error; |
| 14 | + result?: TResult; |
| 15 | +} |
| 16 | + |
| 17 | +export class GitEmailPolicy extends GitPolicy { |
| 18 | + public static getUserEmail(rushConfiguration: RushConfiguration): string { |
| 19 | + // Determine the user's account |
| 20 | + // Ex: "bob@example.com" |
| 21 | + const emailResult: IResultOrError<string> = GitEmailPolicy.tryGetEmail(); |
| 22 | + if (emailResult.error) { |
| 23 | + console.log( |
| 24 | + [ |
| 25 | + `Error: ${emailResult.error.message}`, |
| 26 | + 'Unable to determine your Git configuration using this command:', |
| 27 | + '', |
| 28 | + ' git config user.email', |
| 29 | + '', |
| 30 | + `If you didn't configure your e-mail yet, try something like this:`, |
| 31 | + ...GitEmailPolicy.getEmailExampleLines(rushConfiguration) |
| 32 | + ].join(os.EOL) |
| 33 | + ); |
| 34 | + throw new AlreadyReportedError(); |
| 35 | + } |
| 36 | + |
| 37 | + return emailResult.result || ''; |
| 38 | + } |
| 39 | + |
| 40 | + private static tryGetEmail(): IResultOrError<string> { |
| 41 | + try { |
| 42 | + return { |
| 43 | + result: Utilities.executeCommandAndCaptureOutput( |
| 44 | + 'git', |
| 45 | + ['config', 'user.email'], |
| 46 | + '.' |
| 47 | + ).trim() |
| 48 | + }; |
| 49 | + } catch (e) { |
| 50 | + return { |
| 51 | + error: e |
| 52 | + }; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static getEmailExampleLines(rushConfiguration: RushConfiguration): string[] { |
| 57 | + return [ |
| 58 | + colors.cyan(' git config --local user.name "Mr. Example"'), |
| 59 | + colors.cyan(` git config --local user.email "${rushConfiguration.gitSampleEmail || 'example@contoso.com'}"`) |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + protected innerValidate(gitPath: string, rushConfiguration: RushConfiguration ): void { |
| 64 | + const userEmail: string = GitEmailPolicy.getUserEmail(rushConfiguration); |
| 65 | + if (!userEmail) { |
| 66 | + console.log(colors.red('Aborting, so you can go fix your settings. (Or use --bypass-policy to skip.)')); |
| 67 | + throw new AlreadyReportedError(); |
| 68 | + } |
| 69 | + |
| 70 | + // sanity check; a valid e-mail should not contain any whitespace |
| 71 | + // if this fails, then we have another issue to report |
| 72 | + if (userEmail && userEmail.match(/^\S+$/g)) { |
| 73 | + console.log('Checking Git policy for this repository.' + os.EOL); |
| 74 | + |
| 75 | + if (rushConfiguration.gitAllowedEmailRegExps.length === 0) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + for (const pattern of rushConfiguration.gitAllowedEmailRegExps) { |
| 80 | + const regex: RegExp = new RegExp('^' + pattern + '$', 'i'); |
| 81 | + if (userEmail.match(regex)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Show the user's name as well. |
| 88 | + // Ex. "Mr. Example <mr@example.com>" |
| 89 | + let fancyEmail: string = colors.cyan(userEmail); |
| 90 | + try { |
| 91 | + const userName: string = Utilities.executeCommandAndCaptureOutput('git', |
| 92 | + ['config', 'user.name'], '.').trim(); |
| 93 | + if (userName) { |
| 94 | + fancyEmail = `${userName} <${fancyEmail}>`; |
| 95 | + } |
| 96 | + } catch (e) { |
| 97 | + // but if it fails, this isn't critical, so don't bother them about it |
| 98 | + } |
| 99 | + |
| 100 | + debugger; |
| 101 | + console.log([ |
| 102 | + 'Hey there! To keep things tidy, this repo asks you to submit your Git commits using an e-mail like ', |
| 103 | + rushConfiguration.gitAllowedEmailRegExps.length > 1 ? 'one of these patterns:' : 'this pattern:', |
| 104 | + '', |
| 105 | + ...rushConfiguration.gitAllowedEmailRegExps.map((pattern) => ' ' + colors.cyan(pattern)), |
| 106 | + '', |
| 107 | + '...but yours is configured like this:', |
| 108 | + '', |
| 109 | + ` ${fancyEmail}`, |
| 110 | + '', |
| 111 | + 'To fix it, you can use commands like this:', |
| 112 | + '', |
| 113 | + ...GitEmailPolicy.getEmailExampleLines(rushConfiguration), |
| 114 | + '' |
| 115 | + ].join(os.EOL)); |
| 116 | + |
| 117 | + console.log(colors.red('Aborting, so you can go fix your settings. (Or use --bypass-policy to skip.)')); |
| 118 | + throw new AlreadyReportedError(); |
| 119 | + } |
| 120 | +} |
0 commit comments