This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: change to manifest based premajor flow
- Loading branch information
Showing
2 changed files
with
53 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`; | ||
|
||
|
@@ -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, ' ')); | ||
} | ||
} | ||
|
||
|
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
4827f4c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deployed at: https://alva-commits-4827f.surge.sh