feat(dev-infra): add utility method to GitClient to get latest SemVer tag (#41455)

Create a utility method for the latest git tag, sorted by committerdate, which
matches SemVer, representing the latest version released on the branch.

PR Close #41455
This commit is contained in:
Joey Perrott 2021-04-01 16:17:04 -07:00 committed by atscott
parent f7c294ee0f
commit ba3344ddbe
3 changed files with 27 additions and 1 deletions

View File

@ -12,11 +12,11 @@ var path = require('path');
var shelljs = require('shelljs');
var url = require('url');
var child_process = require('child_process');
var semver = require('semver');
var graphql = require('@octokit/graphql');
var Octokit = require('@octokit/rest');
var typedGraphqlify = require('typed-graphqlify');
var fetch = _interopDefault(require('node-fetch'));
var semver = require('semver');
var multimatch = require('multimatch');
var yaml = require('yaml');
var conventionalCommitsParser = require('conventional-commits-parser');
@ -704,6 +704,16 @@ var GitClient = /** @class */ (function () {
}
return this.runGraceful(['checkout', branchOrRevision], { stdio: 'ignore' }).status === 0;
};
/** Gets the latest git tag on the current branch that matches SemVer. */
GitClient.prototype.getLatestSemverTag = function () {
var semVerOptions = { loose: true };
var tags = this.runGraceful(['tag', '--sort=-committerdate', '--merged']).stdout.split('\n');
var latestTag = tags.find(function (tag) { return semver.parse(tag, semVerOptions); });
if (latestTag === undefined) {
throw new Error("Unable to find a SemVer matching tag on \"" + this.getCurrentBranchOrRevision() + "\"");
}
return new semver.SemVer(latestTag, semVerOptions);
};
/**
* Assert the GitClient instance is using a token with permissions for the all of the
* provided OAuth scopes.

View File

@ -17,10 +17,12 @@ ts_library(
"@npm//@octokit/types",
"@npm//@types/inquirer",
"@npm//@types/node",
"@npm//@types/semver",
"@npm//@types/shelljs",
"@npm//@types/yargs",
"@npm//chalk",
"@npm//inquirer",
"@npm//semver",
"@npm//shelljs",
"@npm//tslib",
"@npm//typed-graphqlify",

View File

@ -8,6 +8,7 @@
import * as Octokit from '@octokit/rest';
import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process';
import {Options as SemVerOptions, parse, SemVer} from 'semver';
import {getConfig, getRepoBaseDir, NgDevConfig} from '../config';
import {debug, info, yellow} from '../console';
@ -181,6 +182,19 @@ export class GitClient {
return this.runGraceful(['checkout', branchOrRevision], {stdio: 'ignore'}).status === 0;
}
/** Gets the latest git tag on the current branch that matches SemVer. */
getLatestSemverTag(): SemVer {
const semVerOptions: SemVerOptions = {loose: true};
const tags = this.runGraceful(['tag', '--sort=-committerdate', '--merged']).stdout.split('\n');
const latestTag = tags.find((tag: string) => parse(tag, semVerOptions));
if (latestTag === undefined) {
throw new Error(
`Unable to find a SemVer matching tag on "${this.getCurrentBranchOrRevision()}"`);
}
return new SemVer(latestTag, semVerOptions);
}
/**
* Assert the GitClient instance is using a token with permissions for the all of the
* provided OAuth scopes.