2020-07-24 11:47:30 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fetch from 'node-fetch';
|
|
|
|
import * as semver from 'semver';
|
|
|
|
|
|
|
|
import {promptConfirm, red, warn, yellow} from '../../../utils/console';
|
|
|
|
import {InvalidTargetBranchError} from '../target-label';
|
|
|
|
|
2020-09-01 06:32:09 -04:00
|
|
|
import {getVersionOfBranch, GithubRepoWithApi} from './branches';
|
2020-07-24 11:47:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of months a major version in Angular is actively supported. See:
|
|
|
|
* https://angular.io/guide/releases#support-policy-and-schedule.
|
|
|
|
*/
|
2020-09-01 13:48:03 -04:00
|
|
|
export const majorActiveSupportDuration = 6;
|
2020-07-24 11:47:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of months a major version has active long-term support. See:
|
|
|
|
* https://angular.io/guide/releases#support-policy-and-schedule.
|
|
|
|
*/
|
2020-09-01 13:48:03 -04:00
|
|
|
export const majorActiveTermSupportDuration = 12;
|
|
|
|
|
|
|
|
/** Regular expression that matches LTS NPM dist tags. */
|
|
|
|
export const ltsNpmDistTagRegex = /^v(\d+)-lts$/;
|
2020-07-24 11:47:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that the given branch corresponds to an active LTS version-branch that can receive
|
2020-09-01 04:46:41 -04:00
|
|
|
* backport fixes. Throws an error if LTS expired or an invalid branch is selected.
|
|
|
|
*
|
|
|
|
* @param repo Github repository for which the given branch exists.
|
|
|
|
* @param representativeNpmPackage NPM package representing the given repository. Angular
|
|
|
|
* repositories usually contain multiple packages in a monorepo scheme, but packages commonly
|
|
|
|
* are released with the same versions. This means that a single package can be used for querying
|
|
|
|
* NPM about previously published versions (e.g. to determine active LTS versions). The package
|
|
|
|
* name is used to check if the given branch is containing an active LTS version.
|
|
|
|
* @param branchName Branch that is checked to be an active LTS version-branch.
|
|
|
|
* */
|
|
|
|
export async function assertActiveLtsBranch(
|
2020-09-01 06:32:09 -04:00
|
|
|
repo: GithubRepoWithApi, representativeNpmPackage: string, branchName: string) {
|
2020-07-24 11:47:30 -04:00
|
|
|
const version = await getVersionOfBranch(repo, branchName);
|
|
|
|
const {'dist-tags': distTags, time} =
|
2020-09-01 04:46:41 -04:00
|
|
|
await (await fetch(`https://registry.npmjs.org/${representativeNpmPackage}`)).json();
|
2020-07-24 11:47:30 -04:00
|
|
|
|
|
|
|
// LTS versions should be tagged in NPM in the following format: `v{major}-lts`.
|
2020-09-01 13:48:03 -04:00
|
|
|
const ltsNpmTag = getLtsNpmDistTagOfMajor(version.major);
|
|
|
|
const ltsVersion = semver.parse(distTags[ltsNpmTag]);
|
2020-07-24 11:47:30 -04:00
|
|
|
|
|
|
|
// Ensure that there is a LTS version tagged for the given version-branch major. e.g.
|
|
|
|
// if the version branch is `9.2.x` then we want to make sure that there is a LTS
|
|
|
|
// version tagged in NPM for `v9`, following the `v{major}-lts` tag convention.
|
|
|
|
if (ltsVersion === null) {
|
|
|
|
throw new InvalidTargetBranchError(`No LTS version tagged for v${version.major} in NPM.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the correct branch is used for the LTS version. We do not want to merge
|
|
|
|
// changes to older minor version branches that do not reflect the current LTS version.
|
|
|
|
if (branchName !== `${ltsVersion.major}.${ltsVersion.minor}.x`) {
|
|
|
|
throw new InvalidTargetBranchError(
|
|
|
|
`Not using last-minor branch for v${version.major} LTS version. PR ` +
|
|
|
|
`should be updated to target: ${ltsVersion.major}.${ltsVersion.minor}.x`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const today = new Date();
|
2020-09-01 13:48:03 -04:00
|
|
|
const majorReleaseDate = new Date(time[`${version.major}.0.0`]);
|
|
|
|
const ltsEndDate = computeLtsEndDateOfMajor(majorReleaseDate);
|
2020-07-24 11:47:30 -04:00
|
|
|
|
|
|
|
// Check if LTS has already expired for the targeted major version. If so, we do not
|
|
|
|
// allow the merge as per our LTS guarantees. Can be forcibly overridden if desired.
|
|
|
|
// See: https://angular.io/guide/releases#support-policy-and-schedule.
|
|
|
|
if (today > ltsEndDate) {
|
|
|
|
const ltsEndDateText = ltsEndDate.toLocaleDateString();
|
|
|
|
warn(red(`Long-term support ended for v${version.major} on ${ltsEndDateText}.`));
|
|
|
|
warn(yellow(
|
|
|
|
`Merging of pull requests for this major is generally not ` +
|
|
|
|
`desired, but can be forcibly ignored.`));
|
|
|
|
if (await promptConfirm('Do you want to forcibly proceed with merging?')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new InvalidTargetBranchError(
|
|
|
|
`Long-term supported ended for v${version.major} on ${ltsEndDateText}. ` +
|
|
|
|
`Pull request cannot be merged into the ${branchName} branch.`);
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 13:48:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the date when long-term support ends for a major released at the
|
|
|
|
* specified date.
|
|
|
|
*/
|
|
|
|
export function computeLtsEndDateOfMajor(majorReleaseDate: Date): Date {
|
|
|
|
return new Date(
|
|
|
|
majorReleaseDate.getFullYear(),
|
|
|
|
majorReleaseDate.getMonth() + majorActiveSupportDuration + majorActiveTermSupportDuration,
|
|
|
|
majorReleaseDate.getDate(), majorReleaseDate.getHours(), majorReleaseDate.getMinutes(),
|
|
|
|
majorReleaseDate.getSeconds(), majorReleaseDate.getMilliseconds());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets the long-term support NPM dist tag for a given major version. */
|
|
|
|
export function getLtsNpmDistTagOfMajor(major: number): string {
|
|
|
|
// LTS versions should be tagged in NPM in the following format: `v{major}-lts`.
|
|
|
|
return `v${major}-lts`;
|
|
|
|
}
|