fix(dev-infra): ensure that building environment stamp fails silently (#42985)

Previously when a failure occurred in part of building the environment stamp, the entire
process errored.  This should instead fail silently providing no value for the stamp.

PR Close #42985
This commit is contained in:
Joey Perrott 2021-07-28 12:17:19 -07:00 committed by Dylan Hunn
parent 8116895ece
commit 2a0e28cf61
2 changed files with 99 additions and 51 deletions

View File

@ -7746,8 +7746,13 @@ function buildEnvStamp(mode) {
} }
/** Whether the repo has local changes. */ /** Whether the repo has local changes. */
function hasLocalChanges() { function hasLocalChanges() {
try {
const git = GitClient.get(); const git = GitClient.get();
return git.hasUncommittedChanges(); return git.hasUncommittedChanges();
}
catch (_a) {
return true;
}
} }
/** /**
* Get the versions for generated packages. * Get the versions for generated packages.
@ -7756,13 +7761,8 @@ function hasLocalChanges() {
* In release mode, the version is based on the base package.json version. * In release mode, the version is based on the base package.json version.
*/ */
function getSCMVersions(mode) { function getSCMVersions(mode) {
try {
const git = GitClient.get(); const git = GitClient.get();
if (mode === 'release') {
const packageJsonPath = path.join(git.baseDir, 'package.json');
const { version } = new semver.SemVer(require(packageJsonPath).version);
const { version: experimentalVersion } = createExperimentalSemver(new semver.SemVer(version));
return { version, experimentalVersion };
}
if (mode === 'snapshot') { if (mode === 'snapshot') {
const localChanges = hasLocalChanges() ? '.with-local-changes' : ''; const localChanges = hasLocalChanges() ? '.with-local-changes' : '';
const { stdout: rawVersion } = git.run(['describe', '--match', '*[0-9]*.[0-9]*.[0-9]*', '--abbrev=7', '--tags', 'HEAD~100']); const { stdout: rawVersion } = git.run(['describe', '--match', '*[0-9]*.[0-9]*.[0-9]*', '--abbrev=7', '--tags', 'HEAD~100']);
@ -7773,24 +7773,51 @@ function getSCMVersions(mode) {
experimentalVersion: `${experimentalVersion.replace(/-([0-9]+)-g/, '+$1.sha-')}${localChanges}`, experimentalVersion: `${experimentalVersion.replace(/-([0-9]+)-g/, '+$1.sha-')}${localChanges}`,
}; };
} }
throw Error('No environment stamp mode was provided.'); else {
const packageJsonPath = path.join(git.baseDir, 'package.json');
const { version } = new semver.SemVer(require(packageJsonPath).version);
const { version: experimentalVersion } = createExperimentalSemver(new semver.SemVer(version));
return { version, experimentalVersion };
}
}
catch (_a) {
return {
version: '',
experimentalVersion: '',
};
}
} }
/** Get the current branch or revision of HEAD. */ /** Get the current branch or revision of HEAD. */
function getCurrentBranchOrRevision() { function getCurrentBranchOrRevision() {
try {
const git = GitClient.get(); const git = GitClient.get();
return git.getCurrentBranchOrRevision(); return git.getCurrentBranchOrRevision();
}
catch (_a) {
return '';
}
} }
/** Get the currently checked out branch. */ /** Get the currently checked out branch. */
function getCurrentBranch() { function getCurrentBranch() {
try {
const git = GitClient.get(); const git = GitClient.get();
return git.run(['symbolic-ref', '--short', 'HEAD']).stdout.trim(); return git.run(['symbolic-ref', '--short', 'HEAD']).stdout.trim();
}
catch (_a) {
return '';
}
} }
/** Get the current git user based on the git config. */ /** Get the current git user based on the git config. */
function getCurrentGitUser() { function getCurrentGitUser() {
try {
const git = GitClient.get(); const git = GitClient.get();
let userName = git.runGraceful(['config', 'user.name']).stdout.trim() || 'Unknown User'; let userName = git.runGraceful(['config', 'user.name']).stdout.trim() || 'Unknown User';
let userEmail = git.runGraceful(['config', 'user.email']).stdout.trim() || 'unknown_email'; let userEmail = git.runGraceful(['config', 'user.email']).stdout.trim() || 'unknown_email';
return `${userName} <${userEmail}>`; return `${userName} <${userEmail}>`;
}
catch (_a) {
return '';
}
} }
/** /**

View File

@ -37,8 +37,12 @@ export function buildEnvStamp(mode: EnvStampMode) {
/** Whether the repo has local changes. */ /** Whether the repo has local changes. */
function hasLocalChanges() { function hasLocalChanges() {
try {
const git = GitClient.get(); const git = GitClient.get();
return git.hasUncommittedChanges(); return git.hasUncommittedChanges();
} catch {
return true;
}
} }
/** /**
@ -48,13 +52,8 @@ function hasLocalChanges() {
* In release mode, the version is based on the base package.json version. * In release mode, the version is based on the base package.json version.
*/ */
function getSCMVersions(mode: EnvStampMode): {version: string, experimentalVersion: string} { function getSCMVersions(mode: EnvStampMode): {version: string, experimentalVersion: string} {
try {
const git = GitClient.get(); const git = GitClient.get();
if (mode === 'release') {
const packageJsonPath = join(git.baseDir, 'package.json');
const {version} = new SemVer(require(packageJsonPath).version);
const {version: experimentalVersion} = createExperimentalSemver(new SemVer(version));
return {version, experimentalVersion};
}
if (mode === 'snapshot') { if (mode === 'snapshot') {
const localChanges = hasLocalChanges() ? '.with-local-changes' : ''; const localChanges = hasLocalChanges() ? '.with-local-changes' : '';
const {stdout: rawVersion} = git.run( const {stdout: rawVersion} = git.run(
@ -66,26 +65,48 @@ function getSCMVersions(mode: EnvStampMode): {version: string, experimentalVersi
experimentalVersion: experimentalVersion:
`${experimentalVersion.replace(/-([0-9]+)-g/, '+$1.sha-')}${localChanges}`, `${experimentalVersion.replace(/-([0-9]+)-g/, '+$1.sha-')}${localChanges}`,
}; };
} else {
const packageJsonPath = join(git.baseDir, 'package.json');
const {version} = new SemVer(require(packageJsonPath).version);
const {version: experimentalVersion} = createExperimentalSemver(new SemVer(version));
return {version, experimentalVersion};
}
} catch {
return {
version: '',
experimentalVersion: '',
};
} }
throw Error('No environment stamp mode was provided.');
} }
/** Get the current branch or revision of HEAD. */ /** Get the current branch or revision of HEAD. */
function getCurrentBranchOrRevision() { function getCurrentBranchOrRevision() {
try {
const git = GitClient.get(); const git = GitClient.get();
return git.getCurrentBranchOrRevision(); return git.getCurrentBranchOrRevision();
} catch {
return '';
}
} }
/** Get the currently checked out branch. */ /** Get the currently checked out branch. */
function getCurrentBranch() { function getCurrentBranch() {
try {
const git = GitClient.get(); const git = GitClient.get();
return git.run(['symbolic-ref', '--short', 'HEAD']).stdout.trim(); return git.run(['symbolic-ref', '--short', 'HEAD']).stdout.trim();
} catch {
return '';
}
} }
/** Get the current git user based on the git config. */ /** Get the current git user based on the git config. */
function getCurrentGitUser() { function getCurrentGitUser() {
try {
const git = GitClient.get(); const git = GitClient.get();
let userName = git.runGraceful(['config', 'user.name']).stdout.trim() || 'Unknown User'; let userName = git.runGraceful(['config', 'user.name']).stdout.trim() || 'Unknown User';
let userEmail = git.runGraceful(['config', 'user.email']).stdout.trim() || 'unknown_email'; let userEmail = git.runGraceful(['config', 'user.email']).stdout.trim() || 'unknown_email';
return `${userName} <${userEmail}>`; return `${userName} <${userEmail}>`;
} catch {
return '';
}
} }