forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build-cli): add check latestVersions command to build-tools (mic…
…rosoft#22252) added check latestVersions command to build-tools. Usage for build-docs pipeline scenario where we want to check if triggering branch is the latest minor version of a major version.
- Loading branch information
1 parent
542021f
commit fc486fe
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
build-tools/packages/build-cli/src/commands/check/latestVersions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { isInternalVersionScheme } from "@fluid-tools/version-tools"; | ||
import * as semver from "semver"; | ||
import { findPackageOrReleaseGroup, packageOrReleaseGroupArg, semverArg } from "../../args.js"; | ||
import { BaseCommand, sortVersions } from "../../library/index.js"; | ||
|
||
type MajorVersion = number; | ||
|
||
export default class LatestVersionsCommand extends BaseCommand<typeof LatestVersionsCommand> { | ||
static readonly summary = | ||
"Determines if an input version matches a latest minor release version. Intended to be used in the Fluid Framework CI pipeline only."; | ||
|
||
static readonly description = | ||
"This command is used in CI to determine if a pipeline was triggered by a release branch with the latest minor version of a major version."; | ||
|
||
static readonly args = { | ||
version: semverArg({ | ||
required: true, | ||
description: | ||
"The version to check. When running in CI, this value corresponds to the pipeline trigger branch.", | ||
}), | ||
package_or_release_group: packageOrReleaseGroupArg({ required: true }), | ||
} as const; | ||
|
||
public async run(): Promise<void> { | ||
const { args } = this; | ||
const context = await this.getContext(); | ||
const versionInput = this.args.version; | ||
|
||
const rgOrPackage = findPackageOrReleaseGroup(args.package_or_release_group, context); | ||
if (rgOrPackage === undefined) { | ||
this.error(`Package not found: ${args.package_or_release_group}`); | ||
} | ||
|
||
const versions = await context.getAllVersions(rgOrPackage.name); | ||
|
||
if (!versions) { | ||
this.error(`No versions found for ${rgOrPackage.name}`); | ||
} | ||
|
||
// Filter out pre-release versions | ||
const stableVersions = versions.filter((v) => { | ||
return !isInternalVersionScheme(v.version); | ||
}); | ||
|
||
// Sort the semver versions ordered from highest to lowest | ||
const sortedByVersion = sortVersions(stableVersions, "version"); | ||
|
||
const inputMajorVersion: MajorVersion = semver.major(versionInput.version); | ||
|
||
for (const v of sortedByVersion) { | ||
const majorVersion: MajorVersion = semver.major(v.version); | ||
|
||
// Since sortedByVersion is sorted, the first encountered version is the highest one | ||
if (majorVersion === inputMajorVersion) { | ||
if (v.version === versionInput.version) { | ||
// Check if the input version is the latest version for the major version | ||
this.log( | ||
`Version ${versionInput.version} is the latest version for major version ${majorVersion}`, | ||
); | ||
return; | ||
} | ||
|
||
// If versions do not match on first major version encounter, then the input version is not the latest | ||
this.error( | ||
`skipping deployment stage. input version ${versionInput.version} does not match the latest version ${v.version}`, | ||
{ exit: 1 }, | ||
); | ||
} | ||
} | ||
|
||
// Error if no major version corresponds to input version | ||
this.error( | ||
`No major version found corresponding to input version ${versionInput.version}`, | ||
{ exit: 1 }, | ||
); | ||
} | ||
} |