Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
chore: change to manifest based premajor flow
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Jan 4, 2019
1 parent baaf4d5 commit 4827f4c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 64 deletions.
116 changes: 52 additions & 64 deletions packages/tools/alva-trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ const yargs = require('yargs-parser');
const got = require('got');
const pgu = require('parse-github-url');
const semverUtils = require('semver-utils');
const sortBy = require('lodash').sortBy;
const semver = require('semver');
const Util = require('util');

const writeFile = Util.promisify(Fs.writeFile);

const TAG = process.env.CIRCLE_TAG;
const PR_NUMBER = process.env.CI_PULL_REQUEST ? Path.basename(process.env.CI_PULL_REQUEST) : '';
const CHANNEL = PR_NUMBER ? `issue-${PR_NUMBER}` : `branch`;

Expand All @@ -26,76 +25,65 @@ async function main(cli) {
process.exit(1);
}

if (!TAG) {
if (process.env.CI === 'true' && !cli.dryRun) {
await execa('git', ['config', 'user.name', 'Alva Release']);
await execa('git', ['config', 'user.email', '[email protected]']);
}
const prefix = cli.dryRun ? 'dry run: ' : '';

const prefix = cli.dryRun ? 'dry run: ' : '';
const projectPath = Path.resolve(process.cwd(), cli.project);
const manifest = require(Path.join(projectPath, 'package.json'));
if (process.env.CI === 'true' && !cli.dryRun) {
await execa('git', ['config', 'user.name', 'Alva Release']);
await execa('git', ['config', 'user.email', '[email protected]']);
}

const projectPath = Path.resolve(process.cwd(), cli.project);
const manifest = require(Path.join(projectPath, 'package.json'));

const giturl =
typeof manifest.repository === 'object' ? manifest.repository.url : manifest.repository;

const repo = pgu(giturl);
const { body: releases } = await got(`https://api.github.com/repos/${repo.repo}/releases`, {
json: true,
auth: process.env.GH_TOKEN
});

const sortedReleases = releases.slice(0).sort((a, b) => {
return semver.rcompare(a.tag_name, b.tag_name);
});

const [branch] = (await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'])).stdout.split(
'\n'
);
const latestRelease = sortedReleases[0];

const [hash] = (await execa('git', ['rev-parse', '--short', 'HEAD'])).stdout.split('\n');
if (latestRelease && semver.gt(manifest.version, latestRelease.tag_name)) {
console.log(`${prefix}trigger full draft release for ${manifest.name}@${manifest.version}`);
return;
}

const [branch] = (await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'])).stdout.split('\n');
const [hash] = (await execa('git', ['rev-parse', '--short', 'HEAD'])).stdout.split('\n');

const channel = branch === 'master' ? 'alpha' : CHANNEL;
const channel = branch === 'master' ? 'alpha' : CHANNEL;

const giturl =
typeof manifest.repository === 'object' ? manifest.repository.url : manifest.repository;
const major = semverUtils.parse( branch === 'master' ? semver.inc(manifest.version, 'major') : manifest.version);
const majorTarget = `${major.major}.${major.minor}.${major.patch}-${channel}.0+${hash}`;

const repo = pgu(giturl);
const { body: releases } = await got(`https://api.github.com/repos/${repo.repo}/releases`, {
json: true,
auth: process.env.GH_TOKEN
const releaseVersions = sortedReleases
.map(release => semverUtils.parse(release.tag_name))
.filter(r => r.release)
.filter(r => {
return r.release.indexOf(channel) === 0;
});

const current = semverUtils.parse(manifest.version);

const versions = sortBy(
releases
.map(release => semverUtils.parse(release.name))
.filter(
r =>
r.major === current.major &&
r.minor === current.minor &&
r.patch === current.patch
)
.filter(r => r.release.indexOf(channel) === 0)
.map(r => {
const raw = r.release.replace(new RegExp(`${channel}.`), '');

if (!/^[0-9]{1,4}$/.test(raw)) {
r.iteration = 0;
return r;
}

r.iteration = parseInt(raw, 10);
return r;
}),
'iteration'
).reverse();

const iteration = versions[0] ? versions[0].iteration + 1 : 1;

console.log(`${prefix}trigger release for ${manifest.name}@${manifest.version} on ${branch}`);

const version = `${manifest.version}-${channel}.${iteration}+${hash}`;
console.log(`${prefix}${manifest.name}@${manifest.version} => ${manifest.name}@${version}`);

if (cli.dryRun) {
console.log(`${prefix}dry run: ${version}`);
} else {
manifest.version = version;
await writeFile(Path.join(projectPath, 'package.json'), JSON.stringify(manifest, null, ' '));
}

console.log(`${prefix}triggered release via ${version}`);
} else {
console.log(`${prefix}tag is set, skipping: ${TAG}`);
const sv = r =>
r.major === majorTarget.major &&
r.minor === majorTarget.minor &&
r.patch === majorTarget.patch;

const iterations = releaseVersions.filter(sv);
const iteration = iterations[0] || majorTarget;
const version = semver.inc(iteration, 'prerelease');

console.log(`${prefix}${manifest.name}@${version}`);

if (!cli.dryRun) {
manifest.version = version;
await writeFile(Path.join(projectPath, 'package.json'), JSON.stringify(manifest, null, ' '));
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"lodash": "^4.17.11",
"node-fetch": "2.1.2",
"parse-github-url": "^1.0.2",
"semver": "^5.6.0",
"semver-utils": "^1.1.4",
"surge": "0.20.1",
"yargs-parser": "10.1.0"
Expand Down

1 comment on commit 4827f4c

@marionebl
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.