From ba3344ddbe2d3cc8fcd81c704b187544eabb78e5 Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Thu, 1 Apr 2021 16:17:04 -0700 Subject: [PATCH] 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 --- dev-infra/ng-dev.js | 12 +++++++++++- dev-infra/utils/BUILD.bazel | 2 ++ dev-infra/utils/git/index.ts | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/dev-infra/ng-dev.js b/dev-infra/ng-dev.js index e8490cf065..5ca940bbd6 100755 --- a/dev-infra/ng-dev.js +++ b/dev-infra/ng-dev.js @@ -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. diff --git a/dev-infra/utils/BUILD.bazel b/dev-infra/utils/BUILD.bazel index 2b3404c485..2be6504dc0 100644 --- a/dev-infra/utils/BUILD.bazel +++ b/dev-infra/utils/BUILD.bazel @@ -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", diff --git a/dev-infra/utils/git/index.ts b/dev-infra/utils/git/index.ts index 0eb030be52..e47b39aaa9 100644 --- a/dev-infra/utils/git/index.ts +++ b/dev-infra/utils/git/index.ts @@ -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.