Skip to content

Commit

Permalink
chore: setup gh workflow (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed May 8, 2020
1 parent 9a08855 commit e5952e0
Show file tree
Hide file tree
Showing 20 changed files with 656 additions and 656 deletions.
11 changes: 11 additions & 0 deletions .github/actions/bump-version/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Prepare PR
description: Prepare Pull Request for the current change
outputs:
release:
description: Does this patch contains a release
version:
description: The new version
runs:
using: 'node12'
main: index.js

107 changes: 107 additions & 0 deletions .github/actions/bump-version/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const fs = require('fs');
const core = require('@actions/core');
const convBump = require('conventional-recommended-bump');
const semver = require('semver');
const conventionalChangelog = require('conventional-changelog')

const DRY = !process.env.CI;

/**
* @returns {Promise<import('conventional-recommended-bump').Callback.Recommendation>}
*/
async function getBumpSuggestion() {
const result = await new Promise((resolve, reject) => {
convBump({
whatBump(comments) {
const reasons = comments.filter(c => c.type === 'feat' || c.type === 'fix' || c.type === 'refactor' || c.header.startsWith('BREAKING CHANGE:'));
const feats = comments.filter(c => c.type === 'feat');
const fixes = comments.filter(c => c.type === 'fix' || c.type === 'refactor');
const breakings = comments.filter(c => c.header.startsWith('BREAKING CHANGE:'));
if (comments.some(c => c.header.startsWith('BREAKING CHANGE:'))) {
return { level: 0, reasons, feats, fixes, breakings }; // major
} else if (comments.some(c => c.type === 'feat')) {
return { level: 1, reasons, feats, fixes, breakings }; // minor
} else if (comments.some(c => c.type === 'fix' || c.type === 'refactor')) {
return { level: 2, reasons, feats, fixes, breakings }; // patch
}
}
}, function (err, result) {
if (err) reject(err);
else resolve(result);
});
});
return result;
}

/**
* @param {string} newVersion
* @returns {Promise<string>}
*/
function generateChangelog(newVersion, oldVersion) {
return new Promise((resolve, reject) => {
let content = ''
const context = { version: newVersion }
const changelogStream = conventionalChangelog({
// tagPrefix: args.tagPrefix
preset: 'conventional-changelog-config-spec',
}, context, { merges: null })
.on('error', function (err) {
return reject(err)
})

changelogStream.on('data', function (buffer) {
content += buffer.toString()
})

changelogStream.on('end', function () {
content = content.replace(/## .+/, `## [${newVersion}](https://github.com/voxelum/voxelauncher/compare/v${oldVersion}...v${newVersion})`)
content = `\n${content.trim()}\n`;
return resolve(content)
})
})
}

function writeFile(name, content) {
if (!DRY) {
fs.writeFileSync(name, content);
} else {
console.log(`Write file ${name}`);
}
}

async function main(output) {
const suggesstion = await getBumpSuggestion();
const package = JSON.parse(fs.readFileSync(`package.json`).toString());
const packageLock = JSON.parse(fs.readFileSync(`package-lock.json`).toString());

if (suggesstion.releaseType) {
const newVersion = semver.inc(package.version, suggesstion.releaseType);
writeFile('pacakge.json', JSON.stringify(Object.assign(package, { version: newVersion }), null, 4));
writeFile('pacakge-lock.json', JSON.stringify(Object.assign(packageLock, { version: newVersion }), null, 4));
const newChangelog = await generateChangelog(newVersion, package.version);
console.log(newChangelog);

const changelog = fs.readFileSync('CHANGELOG.md').toString();
const changelogLines = changelog.split('\n')

const start = changelogLines.findIndex(l => l.startsWith('## ')) - 1;

console.log(start);

const result = [...changelogLines.slice(0, start), '', '', ...newChangelog.split('\n'), ...changelogLines.slice(start)].join('\n');

writeFile('CHANGELOG.md', result);

output('release', true);
output('version', newVersion);
} else {
output('release', false);
}
// const changelog = await generateChangelog('0.1');
// console.log(changelog);
}

main(core ? core.setOutput : (k, v) => {
console.log(k)
console.log(v)
});
6 changes: 6 additions & 0 deletions .github/actions/bump-version/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"devDependencies": {
"@actions/core": "1.2.4"
}
}
13 changes: 13 additions & 0 deletions .github/actions/prepare-pr/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Prepare PR
description: Prepare Pull Request for the current change
outputs:
body:
description: The pull request body
title:
description: The pull request title
message:
description: The pull request message
runs:
using: 'node12'
main: index.js

38 changes: 38 additions & 0 deletions .github/actions/prepare-pr/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require('fs');
const core = require('@actions/core');

function prTitle(version) {
return `Prepare Release ${version}`
}
function prBody(b) {
let body = `This PR is auto-generated by
[create-pull-request](https://github.com/peter-evans/create-pull-request)
to prepare new releases for changed packages.\n\n`;
body += b;
return body;
}
function commitMessage(version) {
return `chore(release): version ${version}`
}

async function main(output) {
const { version } = JSON.parse(fs.readFileSync(`package.json`).toString());
const changelog = fs.readFileSync('CHANGELOG.md').toString();
const changelogLines = changelog.split('\n')

const start = changelogLines.findIndex(l => l.startsWith('## '));
const end = changelogLines.slice(start + 1).findIndex(l => l.startsWith('## '))
const body = changelogLines.slice(start, end).join('\n') + '\n';

console.log(body);

output('title', prTitle(version));
output('body', prBody(body));
output('message', commitMessage(version));
output('release', true);
}

main(core ? core.setOutput : (k, v) => {
console.log(k)
console.log(v)
});
6 changes: 6 additions & 0 deletions .github/actions/prepare-pr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"devDependencies": {
"@actions/core": "1.2.4"
}
}
17 changes: 17 additions & 0 deletions .github/actions/prepare-release/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Prepare Release
description: Prepare Release for the current change
outputs:
body:
description: The release body
draft:
description: The release is draft
prerelease:
description: The release is prerelease
tag:
description: The release tag
release:
description: The release name
runs:
using: 'node12'
main: index.js

29 changes: 29 additions & 0 deletions .github/actions/prepare-release/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs');
const core = require('@actions/core');

async function main(output) {
const { version } = JSON.parse(fs.readFileSync(`package.json`).toString());
const changelog = fs.readFileSync('CHANGELOG.md').toString();
const changelogLines = changelog.split('\n')

const start = changelogLines.findIndex(l => l.startsWith(`## [${version}]`));

let body = 'Manual Release';
if (start !== -1) {
const end = changelogLines.slice(start + 1).findIndex(l => l.startsWith('## '))
body = changelogLines.slice(start, end).join('\n') + '\n';
}

console.log(body);

output('release', `v${version}`);
output('body', body);
output('tag', `v${version}`);
output('prerelease', true);
output('draft', false);
}

main(core ? core.setOutput : (k, v) => {
console.log(k)
console.log(v)
});
6 changes: 6 additions & 0 deletions .github/actions/prepare-release/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"devDependencies": {
"@actions/core": "1.2.4"
}
}
26 changes: 0 additions & 26 deletions .github/workflow/build.yml

This file was deleted.

78 changes: 0 additions & 78 deletions .github/workflow/release.yml

This file was deleted.

Loading

0 comments on commit e5952e0

Please sign in to comment.