Skip to content

Commit

Permalink
fix: better version metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Gummersall committed Mar 5, 2021
1 parent bbad733 commit f33cd91
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
10 changes: 3 additions & 7 deletions libraries/botbuilder-repo-utils/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ export async function gitRoot(): Promise<string> {
* @param {number} truncate how many characters to include in commit sha
* @returns {Promise<string>} returns commit sha `ref` points to
*/
export async function gitSha(ref: string, truncate?: number): Promise<string> {
const { stdout } = await execp(`git rev-parse ${ref}`);
let sha = stdout.trim();
if (truncate != null) {
sha = sha.slice(0, truncate);
}
return sha;
export async function gitSha(ref: string, truncate = 7): Promise<string> {
const { stdout } = await execp(`git rev-parse --short=${truncate} ${ref}`);
return stdout.trim();
}
34 changes: 24 additions & 10 deletions libraries/botbuilder-repo-utils/src/updateVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import { failure, isFailure, Result, run, success } from './run';
import { gitRoot, gitSha } from './git';
import { readJsonFile, writeJsonFile } from './file';

const GIT_SHA_REF = 'HEAD';
const GIT_SHA_LENGTH = 12;

// Represents options for controlling package version update
export interface PackageVersionOptions {
buildLabel?: string;
commitSha?: string;
date?: string;
deprecated?: string;
Expand All @@ -36,17 +34,32 @@ export const getPackageVersion = (
newVersion: string,
options: PackageVersionOptions
): string => {
const extra = [options.date, options.commitSha];
const metadata = [];

// Build label like "dev" should come first in metadata, if defined
if (options.buildLabel) {
metadata.push(options.buildLabel);
}

// Include package status
if (pkg.deprecated) {
extra.unshift(options.deprecated);
metadata.push(options.deprecated);
} else if (pkg.internal) {
extra.unshift(options.internal);
metadata.push(options.internal);
} else if (pkg.preview) {
extra.unshift(options.preview);
metadata.push(options.preview);
}

// Include extra metadata, if defined
if (options.date) {
metadata.push(`date-${options.date}`);
}

if (options.commitSha) {
metadata.push(`sha-${options.commitSha}`);
}

return R.compact([newVersion, R.compact(extra).join('.')]).join('-');
return R.compact([newVersion, R.compact(metadata).join('.')]).join('-');
};

export const command = (argv: string[], quiet = false) => async (): Promise<Result> => {
Expand All @@ -69,7 +82,7 @@ export const command = (argv: string[], quiet = false) => async (): Promise<Resu
internal: 'internal',
preview: 'preview',
},
string: ['date', 'deprecated', 'git', 'internal', 'preview'],
string: ['buildLabel', 'date', 'deprecated', 'git', 'internal', 'preview'],
});

// If `maybeNewVersion` is falsy use version from the root packge.json file
Expand All @@ -82,7 +95,7 @@ export const command = (argv: string[], quiet = false) => async (): Promise<Resu
const date = flags.date ? dayjs().format(flags.date) : undefined;

// Read git commit sha if instructed (JSON.parse properly coerces strings to boolean)
const commitSha = JSON.parse(flags.git) ? await gitSha(GIT_SHA_REF, GIT_SHA_LENGTH) : undefined;
const commitSha = JSON.parse(flags.git) ? await gitSha('HEAD') : undefined;

// Collect all non-private workspaces from the repo root. Returns workspaces with absolute paths.
const workspaces = await collectWorkspacePackages(repoRoot, packageFile.workspaces, { noPrivate: true });
Expand All @@ -92,6 +105,7 @@ export const command = (argv: string[], quiet = false) => async (): Promise<Resu
(acc, { pkg }) => ({
...acc,
[pkg.name]: getPackageVersion(pkg, newVersion, {
buildLabel: flags.buildLabel,
commitSha,
date,
deprecated: flags.deprecated,
Expand Down
45 changes: 31 additions & 14 deletions libraries/botbuilder-repo-utils/tests/updateVersions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('updateVersions', () => {
const testCases: Array<{
expected: string;
label: string;
options: Partial<Record<'commitSha' | 'date' | 'deprecated' | 'preview', string>>;
options: Partial<Record<'buildLabel' | 'commitSha' | 'date' | 'deprecated' | 'preview', string>>;
pkg?: Partial<Package>;
}> = [
{
Expand All @@ -35,17 +35,22 @@ describe('updateVersions', () => {
{
label: 'standard package with date',
options: { ...defaultOptions, date: 'DATE' },
expected: `${newVersion}-DATE`,
expected: `${newVersion}-date-DATE`,
},
{
label: 'standard package with commitSha',
options: { ...defaultOptions, commitSha: 'COMMIT' },
expected: `${newVersion}-COMMIT`,
expected: `${newVersion}-sha-COMMIT`,
},
{
label: 'standard package with date and commitSha',
options: { ...defaultOptions, commitSha: 'COMMIT', date: 'DATE' },
expected: `${newVersion}-DATE.COMMIT`,
expected: `${newVersion}-date-DATE.sha-COMMIT`,
},
{
label: 'standard package with date, commitSha, and buildLabel',
options: { ...defaultOptions, buildLabel: 'BUILD', commitSha: 'COMMIT', date: 'DATE' },
expected: `${newVersion}-BUILD.date-DATE.sha-COMMIT`,
},
{
label: 'preview package with defaults',
Expand All @@ -57,19 +62,25 @@ describe('updateVersions', () => {
label: 'preview package with date',
pkg: previewPackage,
options: { ...defaultOptions, date: 'DATE' },
expected: `${newVersion}-PREVIEW.DATE`,
expected: `${newVersion}-PREVIEW.date-DATE`,
},
{
label: 'preview package with commitSha',
pkg: previewPackage,
options: { ...defaultOptions, commitSha: 'COMMIT' },
expected: `${newVersion}-PREVIEW.COMMIT`,
expected: `${newVersion}-PREVIEW.sha-COMMIT`,
},
{
label: 'preview package with date and commitSha',
pkg: previewPackage,
options: { ...defaultOptions, commitSha: 'COMMIT', date: 'DATE' },
expected: `${newVersion}-PREVIEW.DATE.COMMIT`,
expected: `${newVersion}-PREVIEW.date-DATE.sha-COMMIT`,
},
{
label: 'preview package with date, commitSha, and build label',
pkg: previewPackage,
options: { ...defaultOptions, buildLabel: 'BUILD', commitSha: 'COMMIT', date: 'DATE' },
expected: `${newVersion}-BUILD.PREVIEW.date-DATE.sha-COMMIT`,
},
{
label: 'deprecated package with defaults',
Expand All @@ -81,19 +92,25 @@ describe('updateVersions', () => {
label: 'deprecated package with date',
pkg: deprecatedPackage,
options: { ...defaultOptions, date: 'DATE' },
expected: `${newVersion}-DEPRECATED.DATE`,
expected: `${newVersion}-DEPRECATED.date-DATE`,
},
{
label: 'deprecated package with commitSha',
pkg: deprecatedPackage,
options: { ...defaultOptions, commitSha: 'COMMIT' },
expected: `${newVersion}-DEPRECATED.COMMIT`,
expected: `${newVersion}-DEPRECATED.sha-COMMIT`,
},
{
label: 'deprecated package with date and commitSha',
pkg: deprecatedPackage,
options: { ...defaultOptions, commitSha: 'COMMIT', date: 'DATE' },
expected: `${newVersion}-DEPRECATED.DATE.COMMIT`,
expected: `${newVersion}-DEPRECATED.date-DATE.sha-COMMIT`,
},
{
label: 'deprecated package with date, commitSha, and buildLabel',
pkg: deprecatedPackage,
options: { ...defaultOptions, buildLabel: 'BUILD', commitSha: 'COMMIT', date: 'DATE' },
expected: `${newVersion}-BUILD.DEPRECATED.date-DATE.sha-COMMIT`,
},
];

Expand Down Expand Up @@ -267,9 +284,9 @@ describe('updateVersions', () => {
const dateFormat = 'YYYYMM';
const formattedDate = dayjs().format(dateFormat);

const expectedVersion = `${packageVersion}-${formattedDate}.COMMIT`;
const expectedPreviewVersion = `${packageVersion}-preview.${formattedDate}.COMMIT`;
const expectedDeprecatedVersion = `${packageVersion}-deprecated.${formattedDate}.COMMIT`;
const expectedVersion = `${packageVersion}-dev.date-${formattedDate}.sha-COMMIT`;
const expectedPreviewVersion = `${packageVersion}-dev.preview.date-${formattedDate}.sha-COMMIT`;
const expectedDeprecatedVersion = `${packageVersion}-dev.deprecated.date-${formattedDate}.sha-COMMIT`;

await runAndVerify(
[
Expand Down Expand Up @@ -297,7 +314,7 @@ describe('updateVersions', () => {
},
},
],
['--git', 'true', '--date', dateFormat],
['--buildLabel', 'dev', '--git', 'true', '--date', dateFormat],
{ commitSha: 'COMMIT' }
);
});
Expand Down

0 comments on commit f33cd91

Please sign in to comment.