cloud_bbb/scripts/publish-release.js

438 lines
10 KiB
JavaScript
Raw Normal View History

2020-04-29 14:16:57 +02:00
/* eslint-disable @typescript-eslint/no-var-requires */
2020-04-28 01:08:00 +02:00
require('colors').setTheme({
2020-04-29 14:16:57 +02:00
verbose: 'cyan',
warn: 'yellow',
error: 'red',
2020-04-28 01:08:00 +02:00
});
2020-04-29 14:16:57 +02:00
const fs = require('fs');
const path = require('path');
const https = require('https');
2020-04-29 14:16:57 +02:00
const { Octokit } = require('@octokit/rest');
2020-04-28 01:08:00 +02:00
const execa = require('execa');
const inquirer = require('inquirer');
2020-04-28 01:08:00 +02:00
const git = require('simple-git/promise')();
const package = require('../package.json');
require('dotenv').config();
const isDryRun = process.argv.indexOf('--dry-run') > 1;
const commitMessage = `release: ${package.version} :tada:`;
2020-04-28 01:08:00 +02:00
const tagName = `v${package.version}`;
const files = [
2020-04-29 14:16:57 +02:00
path.join(__dirname, '..', 'archives', `bbb-v${package.version}.tar.gz`),
path.join(__dirname, '..', 'archives', `bbb-v${package.version}.tar.gz.asc`),
path.join(__dirname, '..', 'archives', `bbb-v${package.version}.tar.gz.ncsig`),
path.join(__dirname, '..', 'archives', `bbb-v${package.version}.tar.gz.sig`),
2020-04-28 01:08:00 +02:00
];
function pull() {
2020-04-29 14:16:57 +02:00
return git.pull('origin', 'master');
}
2020-04-28 01:08:00 +02:00
async function notAlreadyTagged() {
2020-04-29 14:16:57 +02:00
if ((await git.tags()).all.includes(tagName)) {
throw 'version already tagged';
}
2020-04-28 01:08:00 +02:00
}
async function lastCommitNotBuild() {
2020-04-29 14:16:57 +02:00
return (await git.log(['-1'])).latest.message !== commitMessage;
2020-04-28 01:08:00 +02:00
}
async function isMasterBranch() {
2020-04-29 14:16:57 +02:00
return (await git.branch()) === 'master';
2020-04-28 01:08:00 +02:00
}
async function generateChangelog() {
2020-04-29 14:16:57 +02:00
const latestTag = (await git.tags()).latest;
const title = `v${package.version}` === latestTag ? '[Unreleased]' : `${package.version} (${new Date().toISOString().split('T')[0]})`;
2020-04-29 14:16:57 +02:00
const logs = await git.log({
from: latestTag,
to: 'HEAD',
});
2020-04-29 14:16:57 +02:00
const sections = [{
type: 'feat',
label: 'Added',
}, {
type: 'fix',
label: 'Fixed',
}];
2020-04-29 14:16:57 +02:00
const entries = {};
2020-04-29 14:16:57 +02:00
logs.all.forEach(log => {
2020-05-17 16:56:12 +02:00
const match = log.message.match(/^([a-z]+)(?:\((\w+)\))?: (.+)/);
if (!match) {
return;
}
const [, type, scope, description] = match;
2020-04-29 14:16:57 +02:00
const entry = { type, scope, description, issues: [] };
2020-04-29 14:16:57 +02:00
if(log.body) {
const matches = log.body.match(/(?:fix|fixes|closes?|refs?) #(\d+)/g) || [];
2020-04-29 14:16:57 +02:00
for (const match of matches) {
const [, number] = match.match(/(\d+)$/);
2020-04-29 13:30:30 +02:00
2020-04-29 14:16:57 +02:00
entry.issues.push(number);
}
}
2020-04-29 14:16:57 +02:00
if (!entries[type]) {
entries[type] = [];
}
2020-04-29 14:16:57 +02:00
entries[type].push(entry);
});
2020-04-29 14:16:57 +02:00
let changeLog = `## ${title}\n`;
2020-04-29 14:16:57 +02:00
function stringifyEntry(entry) {
const issues = entry.issues.map(issue => {
return `[#${issue}](https://github.com/sualko/cloud_bbb/issues/${issue})`;
}).join('');
2020-04-29 14:14:30 +02:00
2020-04-29 14:16:57 +02:00
return `- ${issues}${issues.length > 0 ? ' ' : ''}${entry.description}\n`;
}
2020-04-29 14:16:57 +02:00
sections.forEach(section => {
if (!entries[section.type]) {
return;
}
2020-04-29 14:16:57 +02:00
changeLog += `### ${section.label}\n`;
2020-04-29 14:16:57 +02:00
entries[section.type].forEach(entry => {
changeLog += stringifyEntry(entry);
});
2020-04-29 14:16:57 +02:00
delete entries[section.type];
2020-04-29 14:16:57 +02:00
changeLog += '\n';
});
2020-04-29 14:16:57 +02:00
const miscKeys = Object.keys(entries);
2020-04-29 14:16:57 +02:00
if (miscKeys && miscKeys.length > 0) {
changeLog += '### Misc\n';
2020-04-29 14:16:57 +02:00
miscKeys.forEach(type => {
entries[type].forEach(entry => {
changeLog += stringifyEntry(entry);
});
});
}
2020-04-29 14:16:57 +02:00
return changeLog;
}
2020-04-29 14:14:30 +02:00
async function editChangeLog(changeLog) {
2020-04-29 14:16:57 +02:00
const answers = await inquirer.prompt([{
type: 'editor',
name: 'changeLog',
message: 'You have now the possibility to edit the change log',
default: changeLog,
}]);
return answers.changeLog;
2020-04-29 14:14:30 +02:00
}
2020-04-28 01:08:00 +02:00
function hasChangeLogEntry() {
2020-04-29 14:16:57 +02:00
return new Promise(resolve => {
fs.readFile(path.join(__dirname, '..', 'CHANGELOG.md'), function (err, data) {
if (err) throw err;
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
if (!data.includes(`## ${package.version}`)) {
throw `Found no change log entry for ${package.version}`;
}
2020-04-29 14:16:57 +02:00
resolve();
});
});
2020-04-28 01:08:00 +02:00
}
async function commitChangeLog() {
2020-04-29 14:16:57 +02:00
const status = await git.status();
2020-04-29 14:16:57 +02:00
if (status.staged.length > 0) {
throw 'Repo not clean. Found staged files.';
}
2020-04-29 14:16:57 +02:00
if (!isDryRun) {
await git.add('CHANGELOG.md');
await git.commit('docs: update change log', ['-n']);
}
}
2020-04-28 01:08:00 +02:00
async function hasArchiveAndSignatures() {
2020-04-29 14:16:57 +02:00
return files.map(file => fs.existsSync(file)).indexOf(false) < 0;
2020-04-28 01:08:00 +02:00
}
async function stageAllFiles() {
2020-04-29 14:16:57 +02:00
if (isDryRun) {
return;
}
2020-04-29 14:16:57 +02:00
const gitProcess = execa('git', ['add', '-u']);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
gitProcess.stdout.pipe(process.stdout);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
return gitProcess;
2020-04-28 01:08:00 +02:00
}
function showStagedDiff() {
2020-04-29 14:16:57 +02:00
const gitProcess = execa('git', ['diff', '--staged']);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
gitProcess.stdout.pipe(process.stdout);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
return gitProcess;
2020-04-28 01:08:00 +02:00
}
async function keypress() {
2020-04-29 14:16:57 +02:00
return inquirer.prompt([{
type: 'input',
name: 'keypress',
message: 'Press any key to continue... (where is the any key?)',
}]);
2020-04-28 01:08:00 +02:00
}
function commit() {
2020-04-29 14:16:57 +02:00
if (isDryRun) {
return;
}
2020-04-29 14:16:57 +02:00
return git.commit(commitMessage, ['-S', '-n']);
2020-04-28 01:08:00 +02:00
}
async function wantToContinue(message) {
2020-04-29 14:16:57 +02:00
const answers = await inquirer.prompt([{
type: 'confirm',
name: 'continue',
message,
default: false,
}]);
if (!answers.continue) {
process.exit(10);
}
}
2020-04-28 01:08:00 +02:00
function push() {
2020-04-29 14:16:57 +02:00
if (isDryRun) {
return;
}
2020-04-29 14:16:57 +02:00
return git.push('origin', 'master');
2020-04-28 01:08:00 +02:00
}
async function createGithubRelease(changeLog) {
2020-04-29 14:16:57 +02:00
if (!process.env.GITHUB_TOKEN) {
throw 'Github token missing';
}
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
userAgent: 'custom releaser for sualko/cloud_bbb',
});
const origin = (await git.remote(['get-url', 'origin'])).trim();
const matches = origin.match(/^git@github\.com:(.+)\/(.+)\.git$/);
if (!matches) {
throw 'Origin is not configured or no ssh url';
}
const owner = matches[1];
const repo = matches[2];
const releaseOptions = {
owner,
repo,
// eslint-disable-next-line @typescript-eslint/camelcase
tag_name: tagName,
2020-05-24 18:48:58 +02:00
name: `BigBlueButton Integration ${tagName}`,
2020-04-29 14:16:57 +02:00
body: changeLog.replace(/^## [^\n]+\n/, ''),
prerelease: !/^\d+\.\d+\.\d+$/.test(package.version),
};
if (isDryRun) {
console.log('github release options', releaseOptions);
return [];
}
const releaseResponse = await octokit.repos.createRelease(releaseOptions);
console.log(`Draft created, see ${releaseResponse.data.html_url}`.verbose);
function getMimeType(filename) {
if (filename.endsWith('.asc') || filename.endsWith('sig')) {
return 'application/pgp-signature';
}
if (filename.endsWith('.tar.gz')) {
return 'application/gzip';
}
if (filename.endsWith('.ncsig')) {
return 'text/plain';
}
return 'application/octet-stream';
}
const assetUrls = await Promise.all(files.map(async file => {
const filename = path.basename(file);
const uploadOptions = {
owner,
repo,
// eslint-disable-next-line @typescript-eslint/camelcase
release_id: releaseResponse.data.id,
data: fs.createReadStream(file),
headers: {
'content-type': getMimeType(filename),
'content-length': fs.statSync(file)['size'],
},
name: filename,
};
const assetResponse = await octokit.repos.uploadReleaseAsset(uploadOptions);
console.log(`Asset uploaded: ${assetResponse.data.name}`.verbose);
return assetResponse.data.browser_download_url;
}));
return assetUrls;
}
async function uploadToNextcloudStore(archiveUrl) {
2020-04-29 14:16:57 +02:00
if(!process.env.NEXTCLOUD_TOKEN) {
throw 'Nextcloud token missing';
}
const hostname = 'apps.nextcloud.com';
const apiEndpoint = '/api/v1/apps/releases';
const signatureFile = files.find(file => file.endsWith('.ncsig'));
const data = JSON.stringify({
download: archiveUrl,
signature: fs.readFileSync(signatureFile, 'utf-8'),
nightly: false,
});
const options = {
hostname,
port: 443,
path: apiEndpoint,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
Authorization: `Token ${process.env.NEXTCLOUD_TOKEN}`,
},
};
if (isDryRun) {
console.log('nextcloud app store request', options, data);
return;
}
return new Promise((resolve, reject) => {
const req = https.request(options, res => {
if (res.statusCode === 200) {
console.log('App release was updated successfully'.verbose);
resolve();
} else if (res.statusCode === 201) {
console.log('App release was created successfully'.verbose);
resolve();
} else if (res.statusCode === 400) {
reject('App release was not accepted');
} else {
reject('App release rejected with status ' + res.statusCode);
}
res.on('data', d => {
process.stdout.write(d);
});
});
req.on('error', error => {
reject(error);
});
req.write(data);
req.end();
});
2020-04-28 01:08:00 +02:00
}
async function run() {
2020-04-29 14:16:57 +02:00
await pull();
console.log('✔ pulled latest changes'.green);
2020-06-15 13:27:57 +02:00
await execa('yarn', ['composer:install:dev']);
console.log('✔ composer dev dependencies installed'.green);
2020-04-29 14:16:57 +02:00
await notAlreadyTagged();
console.log('✔ not already tagged'.green);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await lastCommitNotBuild();
console.log('✔ last commit is no build commit'.green);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await isMasterBranch();
console.log('✔ this is the master branch'.green);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
let changeLog = await generateChangelog();
console.log('✔ change log generated'.green);
2020-04-29 14:16:57 +02:00
changeLog = await editChangeLog(changeLog);
console.log('✔ change log updated'.green);
2020-04-29 14:14:30 +02:00
console.log(changeLog);
2020-04-29 14:16:57 +02:00
console.log('Press any key to continue...');
await keypress();
2020-04-29 14:16:57 +02:00
await hasChangeLogEntry();
console.log('✔ there is a change log entry for this version'.green);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await commitChangeLog();
console.log('✔ change log commited'.green);
2020-04-29 14:16:57 +02:00
await hasArchiveAndSignatures();
console.log('✔ found archive and signatures'.green);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await stageAllFiles();
console.log('✔ all files staged'.green);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await showStagedDiff();
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await wantToContinue('Should I commit those changes?');
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await commit();
console.log('✔ All files commited'.green);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await wantToContinue('Should I push all pending commits?');
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await push();
console.log('✔ All commits pushed'.green);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
await wantToContinue('Should I continue to create a Github release?');
2020-04-29 14:16:57 +02:00
const assetUrls = await createGithubRelease(changeLog);
console.log('✔ released on github'.green);
2020-04-28 01:08:00 +02:00
2020-04-29 14:16:57 +02:00
const archiveAssetUrl = assetUrls.find(url => url.endsWith('.tar.gz'));
console.log(`Asset url for Nextcloud store: ${archiveAssetUrl}`.verbose);
2020-04-29 14:16:57 +02:00
await wantToContinue('Should I continue to upload the release to the app store?');
2020-04-29 14:16:57 +02:00
await uploadToNextcloudStore(archiveAssetUrl);
console.log('✔ released in Nextcloud app store'.green);
};
run().catch(err => {
2020-04-29 14:16:57 +02:00
console.log(`${err.toString()}`.error);
});