Skip to content

Commit e5e5b68

Browse files
devversionjelbourn
authored andcommitted
build: prevent publishing from the wrong branch (#12831)
* Patch releases should be published from patch publish branches with the format: `{}.{}.x` * Minor releases should be published from minor publish branches with the format: `{}.x` * Major releases should be published from the `master` branch. Closes #12655
1 parent 29d5173 commit e5e5b68

File tree

4 files changed

+86
-25
lines changed

4 files changed

+86
-25
lines changed

tools/gulp/gulpfile.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import './tasks/example-module';
2828
import './tasks/lint';
2929
import './tasks/material-release';
3030
import './tasks/payload';
31-
import './tasks/publish';
3231
import './tasks/unit-test';
3332
import './tasks/universal';
34-
import './tasks/validate-release';
33+
34+
import './tasks/publish/publish-task';
35+
import './tasks/publish/validate-release';
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {bold} from 'chalk';
2+
import {spawnSync} from 'child_process';
3+
import {buildConfig} from 'material2-build-tools';
4+
5+
/** Regular expression that matches version names and the individual version segments. */
6+
export const versionNameRegex = /^(\d+)\.(\d+)\.(\d+)(?:-(alpha|beta|rc)\.(\d)+)?/;
7+
8+
/** Regular expression that matches publish branch names and their Semver digits. */
9+
const publishBranchNameRegex = /^([0-9]+)\.([x0-9]+)(?:\.([x0-9]+))?$/;
10+
11+
/** Checks if the specified version can be released from the current Git branch. */
12+
export function checkPublishBranch(version: string) {
13+
const versionType = getSemverVersionType(version);
14+
const branchName = spawnSync('git', ['symbolic-ref', '--short', 'HEAD'],
15+
{cwd: buildConfig.projectDir}).stdout.toString().trim();
16+
17+
if (branchName === 'master') {
18+
if (versionType === 'major') {
19+
return;
20+
}
21+
22+
throw `Publishing of "${versionType}" releases should not happen inside of the ` +
23+
`${bold('master')} branch.`;
24+
}
25+
26+
const branchNameMatch = branchName.match(publishBranchNameRegex) || [];
27+
const branchDigits = branchNameMatch.slice(1, 4);
28+
29+
if (branchDigits[2] === 'x' && versionType !== 'patch') {
30+
throw `Cannot publish a "${versionType}" release inside of a patch branch (${branchName})`;
31+
}
32+
33+
if (branchDigits[1] === 'x' && versionType !== 'minor') {
34+
throw `Cannot publish a "${versionType}" release inside of a minor branch (${branchName})`;
35+
}
36+
37+
throw `Cannot publish a "${versionType}" release from branch: "${branchName}". Releases should `
38+
+ `be published from "master" or the according publish branch (e.g. "6.x", "6.4.x")`;
39+
}
40+
41+
/**
42+
* Determines the type of the specified semver version. Can be either a major, minor or
43+
* patch version.
44+
*/
45+
export function getSemverVersionType(version: string): 'major' | 'minor' | 'patch' {
46+
const versionNameMatch = version.match(versionNameRegex);
47+
48+
if (!versionNameMatch) {
49+
throw `Could not parse version: ${version}. Cannot properly determine version type.`;
50+
}
51+
52+
const versionDigits = versionNameMatch.slice(1, 4);
53+
54+
if (versionDigits[1] === '0' && versionDigits[2] === '0') {
55+
return 'major';
56+
} else if (versionDigits[2] === '0') {
57+
return 'minor';
58+
} else {
59+
return 'patch';
60+
}
61+
}

tools/gulp/tasks/publish.ts renamed to tools/gulp/tasks/publish/publish-task.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import {green, grey, red, yellow} from 'chalk';
12
import {spawn} from 'child_process';
23
import {existsSync, statSync} from 'fs-extra';
3-
import {join} from 'path';
44
import {task} from 'gulp';
5-
import {execTask} from '../util/task_helpers';
65
import {buildConfig, sequenceTask} from 'material2-build-tools';
7-
import {yellow, green, red, grey} from 'chalk';
86
import * as minimist from 'minimist';
7+
import {join} from 'path';
8+
import {execTask} from '../../util/task_helpers';
9+
import {checkPublishBranch, versionNameRegex} from './branch-check';
910

1011
/** Packages that will be published to NPM by the release task. */
1112
export const releasePackages = [
@@ -16,9 +17,6 @@ export const releasePackages = [
1617
'material-moment-adapter'
1718
];
1819

19-
/** Regular Expression that matches valid version numbers of Angular Material. */
20-
export const validVersionRegex = /^\d+\.\d+\.\d+(-(alpha|beta|rc)\.\d+)?$/;
21-
2220
/** Parse command-line arguments for release task. */
2321
const argv = minimist(process.argv.slice(3));
2422

@@ -50,38 +48,39 @@ task(':publish', async () => {
5048
const version = buildConfig.projectVersion;
5149
const currentDir = process.cwd();
5250

53-
if (!version.match(validVersionRegex)) {
54-
console.log(red(`Error: Cannot publish due to an invalid version name. Version "${version}" ` +
55-
`is not following our semver format.`));
56-
console.log(yellow(`A version should follow this format: X.X.X, X.X.X-beta.X, X.X.X-alpha.X, ` +
57-
`X.X.X-rc.X`));
51+
if (!version.match(versionNameRegex)) {
52+
console.error(red(`Error: Cannot publish due to an invalid version name. Version ` +
53+
`"${version}" is not following our semver format.`));
54+
console.error(yellow(`A version should follow this format: X.X.X, X.X.X-beta.X, ` +
55+
`X.X.X-alpha.X, X.X.X-rc.X`));
5856
return;
5957
}
6058

61-
console.log('');
59+
console.log();
6260
if (!tag) {
6361
console.log(grey('> You can specify the tag by passing --tag=labelName.\n'));
6462
console.log(green(`Publishing version "${version}" to the latest tag...`));
6563
} else {
6664
console.log(yellow(`Publishing version "${version}" to the ${tag} tag...`));
6765
}
68-
console.log('');
69-
66+
console.log();
7067

7168
if (version.match(/(alpha|beta|rc)/) && (!tag || tag === 'latest')) {
72-
console.log(red(`Publishing ${version} to the "latest" tag is not allowed.`));
73-
console.log(red(`Alpha, Beta or RC versions shouldn't be published to "latest".`));
74-
console.log();
69+
console.error(red(`Publishing ${version} to the "latest" tag is not allowed.`));
70+
console.error(red(`Alpha, Beta or RC versions shouldn't be published to "latest".`));
71+
console.error();
7572
return;
7673
}
7774

7875
if (releasePackages.length > 1) {
79-
console.warn(red('Warning: Multiple packages will be released if proceeding.'));
80-
console.warn(red('Warning: Packages to be released:', releasePackages.join(', ')));
81-
console.log();
76+
console.warn(yellow('Warning: Multiple packages will be released.'));
77+
console.warn(yellow('Warning: Packages to be released:', releasePackages.join(', ')));
78+
console.warn();
8279
}
8380

84-
console.log(yellow('> Make sure to check the "angularVersion" in the build config.'));
81+
checkPublishBranch(version);
82+
83+
console.log(yellow('> Make sure to check the "requiredAngularVersion" in the package.json.'));
8584
console.log(yellow('> The version in the config defines the peer dependency of Angular.'));
8685
console.log();
8786

tools/gulp/tasks/validate-release.ts renamed to tools/gulp/tasks/publish/validate-release.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import {task} from 'gulp';
22
import {readFileSync, existsSync} from 'fs';
33
import {join} from 'path';
44
import {green, red} from 'chalk';
5-
import {releasePackages} from './publish';
5+
import {releasePackages} from './publish-task';
66
import {sync as glob} from 'glob';
77
import {spawnSync} from 'child_process';
88
import {buildConfig, sequenceTask} from 'material2-build-tools';
99

1010
const {projectDir, projectVersion, outputDir} = buildConfig;
1111

1212
/** Git repository URL that has been read out from the project package.json file. */
13-
const repositoryGitUrl = require('../../../package.json').repository.url;
13+
const repositoryGitUrl = require('../../../../package.json').repository.url;
1414

1515
/** Path to the directory where all releases are created. */
1616
const releasesDir = join(outputDir, 'releases');

0 commit comments

Comments
 (0)