2020-04-28 01:08:00 +02:00
|
|
|
require('colors').setTheme({
|
|
|
|
verbose: 'cyan',
|
|
|
|
warn: 'yellow',
|
|
|
|
error: 'red',
|
|
|
|
});
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2020-04-28 14:16:19 +02:00
|
|
|
const https = require('https');
|
2020-04-28 01:08:00 +02:00
|
|
|
const { Octokit } = require("@octokit/rest");
|
|
|
|
const execa = require('execa');
|
2020-04-28 14:16:19 +02:00
|
|
|
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();
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
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-28 01:17:21 +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
|
|
|
];
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
function pull() {
|
|
|
|
return git.pull('origin', 'master');
|
|
|
|
}
|
|
|
|
|
2020-04-28 01:08:00 +02:00
|
|
|
async function notAlreadyTagged() {
|
2020-04-28 14:16:19 +02:00
|
|
|
if ((await git.tags()).all.includes(tagName)) {
|
|
|
|
throw 'version already tagged';
|
|
|
|
}
|
2020-04-28 01:08:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function lastCommitNotBuild() {
|
|
|
|
return (await git.log(['-1'])).latest.message !== commitMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function isMasterBranch() {
|
|
|
|
return (await git.branch()) === 'master';
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
async function generateChangelog() {
|
|
|
|
const latestTag = (await git.tags()).latest;
|
|
|
|
const title = `v${package.version}` === latestTag ? '[Unreleased]' : `${package.version} (${new Date().toISOString().split('T')[0]})`;
|
|
|
|
|
|
|
|
const logs = await git.log({
|
|
|
|
from: latestTag,
|
|
|
|
to: 'HEAD'
|
|
|
|
});
|
|
|
|
|
|
|
|
const sections = [{
|
|
|
|
type: 'feat',
|
|
|
|
label: 'Added',
|
|
|
|
}, {
|
|
|
|
type: 'fix',
|
|
|
|
label: 'Fixed',
|
|
|
|
}];
|
|
|
|
|
|
|
|
const entries = {};
|
|
|
|
|
|
|
|
logs.all.forEach(log => {
|
|
|
|
let [, type, scope, description] = log.message.match(/^([a-z]+)(?:\((\w+)\))?: (.+)/);
|
|
|
|
let entry = { type, scope, description, issues: [] };
|
|
|
|
|
|
|
|
for (let match of log.body.match(/(?:fix|fixes|closes?|refs?) #(\d+)/g)) {
|
|
|
|
const [, number] = match.match(/(\d+)$/);
|
|
|
|
|
|
|
|
entry.issues.push(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entries[type]) {
|
|
|
|
entries[type] = []
|
|
|
|
}
|
|
|
|
|
|
|
|
entries[type].push(entry);
|
|
|
|
});
|
|
|
|
|
|
|
|
let changeLog = `## ${title}\n`;
|
|
|
|
|
|
|
|
function stringifyEntry(entry) {
|
|
|
|
let issues = entry.issues.map(issue => {
|
|
|
|
return `[#${issue}](https://github.com/sualko/cloud_bbb/issues/${issue})`;
|
|
|
|
}).join('');
|
|
|
|
return `- ${issues} ${entry.description}\n`;
|
|
|
|
}
|
|
|
|
|
|
|
|
sections.forEach(section => {
|
|
|
|
if (!entries[section.type]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
changeLog += `### ${section.label}\n`;
|
|
|
|
|
|
|
|
entries[section.type].forEach(entry => {
|
|
|
|
changeLog += stringifyEntry(entry);
|
|
|
|
});
|
|
|
|
|
|
|
|
delete entries[section.type];
|
|
|
|
|
|
|
|
changeLog += `\n`
|
|
|
|
});
|
|
|
|
|
|
|
|
const miscKeys = Object.keys(entries);
|
|
|
|
|
|
|
|
if (miscKeys && miscKeys.length > 0) {
|
|
|
|
changeLog += `### Misc\n`;
|
|
|
|
|
|
|
|
miscKeys.forEach(type => {
|
|
|
|
entries[type].forEach(entry => {
|
|
|
|
changeLog += stringifyEntry(entry);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return changeLog;
|
|
|
|
}
|
|
|
|
|
2020-04-28 01:08:00 +02:00
|
|
|
function hasChangeLogEntry() {
|
|
|
|
return new Promise(resolve => {
|
2020-04-28 01:17:21 +02:00
|
|
|
fs.readFile(path.join(__dirname, '..', 'CHANGELOG.md'), function (err, data) {
|
2020-04-28 01:08:00 +02:00
|
|
|
if (err) throw err;
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
if (!data.includes(`## ${package.version}`)) {
|
|
|
|
throw `Found no change log entry for ${package.version}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve();
|
2020-04-28 01:08:00 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
async function commitChangeLog() {
|
|
|
|
let status = await git.status();
|
|
|
|
|
|
|
|
if (status.staged.length > 0) {
|
|
|
|
throw 'Repo not clean. Found staged files.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isDryRun) {
|
|
|
|
await git.add('CHANGELOG.md');
|
|
|
|
await git.commit('docs: update change log');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 01:08:00 +02:00
|
|
|
async function hasArchiveAndSignatures() {
|
|
|
|
return files.map(file => fs.existsSync(file)).indexOf(false) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function stageAllFiles() {
|
2020-04-28 14:16:19 +02:00
|
|
|
if (isDryRun) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-28 01:08:00 +02:00
|
|
|
let gitProcess = execa('git', ['add', '-u']);
|
|
|
|
|
|
|
|
gitProcess.stdout.pipe(process.stdout);
|
|
|
|
|
|
|
|
return gitProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
function showStagedDiff() {
|
|
|
|
let gitProcess = execa('git', ['diff', '--staged']);
|
|
|
|
|
|
|
|
gitProcess.stdout.pipe(process.stdout);
|
|
|
|
|
|
|
|
return gitProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function keypress() {
|
2020-04-28 14:16:19 +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-28 14:16:19 +02:00
|
|
|
if (isDryRun) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-28 01:17:21 +02:00
|
|
|
return git.commit(commitMessage, ['-S', '-n']);
|
2020-04-28 01:08:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
async function wantToContinue(message) {
|
|
|
|
let 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-28 14:16:19 +02:00
|
|
|
if (isDryRun) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-28 01:08:00 +02:00
|
|
|
return git.push('origin', 'master');
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
async function createGithubRelease(changeLog) {
|
|
|
|
if (!process.env.GITHUB_TOKEN) {
|
|
|
|
throw 'Github token missing'
|
|
|
|
}
|
|
|
|
|
2020-04-28 01:08:00 +02:00
|
|
|
const octokit = new Octokit({
|
|
|
|
auth: process.env.GITHUB_TOKEN,
|
|
|
|
userAgent: 'custom releaser for sualko/cloud_bbb',
|
|
|
|
});
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
let origin = (await git.remote(['get-url', 'origin'])).trim();
|
|
|
|
let matches = origin.match(/^git@github\.com:(.+)\/(.+)\.git$/);
|
2020-04-28 01:08:00 +02:00
|
|
|
|
|
|
|
if (!matches) {
|
|
|
|
throw 'Origin is not configured or no ssh url';
|
|
|
|
}
|
|
|
|
|
|
|
|
const owner = matches[1];
|
|
|
|
const repo = matches[2];
|
2020-04-28 14:16:19 +02:00
|
|
|
const releaseOptions = {
|
2020-04-28 01:08:00 +02:00
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
tag_name: tagName,
|
|
|
|
name: tagName,
|
2020-04-28 14:16:19 +02:00
|
|
|
body: changeLog,
|
2020-04-28 01:08:00 +02:00
|
|
|
draft: true,
|
|
|
|
prerelease: !/^\d+\.\d+\.\d+$/.test(package.version),
|
2020-04-28 14:16:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (isDryRun) {
|
|
|
|
console.log('github release options', releaseOptions);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let releaseResponse = await octokit.repos.createRelease(releaseOptions);
|
|
|
|
|
|
|
|
console.log(`Draft created, see ${releaseResponse.data.html_url}`.verbose);
|
2020-04-28 01:08:00 +02:00
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
let assetUrls = [];
|
2020-04-28 01:08:00 +02:00
|
|
|
|
|
|
|
files.forEach(async file => {
|
2020-04-28 14:16:19 +02:00
|
|
|
const filename = path.basename(file);
|
|
|
|
const uploadOptions = {
|
2020-04-28 01:08:00 +02:00
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
release_id: releaseResponse.data.id,
|
|
|
|
data: fs.createReadStream(file),
|
2020-04-28 14:16:19 +02:00
|
|
|
headers: {
|
|
|
|
'content-type': getMimeType(filename),
|
|
|
|
'content-length': fs.statSync(file)[size],
|
|
|
|
},
|
|
|
|
name: filename,
|
|
|
|
};
|
|
|
|
|
|
|
|
let assetResponse = await octokit.repos.uploadReleaseAsset(uploadOptions);
|
|
|
|
|
|
|
|
console.log(`Asset uploaded: ${assetResponse.data.name}`.verbose);
|
|
|
|
|
|
|
|
assetUrls.push(assetResponse.data.browser_download_url);
|
|
|
|
});
|
|
|
|
|
|
|
|
return assetUrls;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function uploadToNextcloudStore(archiveUrl) {
|
|
|
|
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);
|
2020-04-28 01:08:00 +02:00
|
|
|
});
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
req.write(data);
|
|
|
|
req.end();
|
|
|
|
});
|
2020-04-28 01:08:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
async function run() {
|
|
|
|
await pull();
|
|
|
|
console.log(`✔ pulled latest changes`.green);
|
|
|
|
|
2020-04-28 01:08:00 +02:00
|
|
|
await notAlreadyTagged();
|
|
|
|
console.log(`✔ not already tagged`.green);
|
|
|
|
|
|
|
|
await lastCommitNotBuild();
|
|
|
|
console.log(`✔ last commit is no build commit`.green);
|
|
|
|
|
|
|
|
await isMasterBranch();
|
|
|
|
console.log(`✔ this is the master branch`.green);
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
const changeLog = await generateChangelog();
|
|
|
|
console.log(changeLog.verbose);
|
|
|
|
console.log(`✔ change log generated`.green);
|
|
|
|
|
|
|
|
console.log('Press any key to continue...');
|
|
|
|
await keypress();
|
|
|
|
|
2020-04-28 01:08:00 +02:00
|
|
|
await hasChangeLogEntry();
|
|
|
|
console.log(`✔ there is a change log entry for this version`.green);
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
await commitChangeLog();
|
|
|
|
console.log(`✔ change log commited`.green);
|
|
|
|
|
2020-04-28 01:08:00 +02:00
|
|
|
await hasArchiveAndSignatures();
|
|
|
|
console.log(`✔ found archive and signatures`.green);
|
|
|
|
|
|
|
|
await stageAllFiles();
|
|
|
|
console.log(`✔ all files staged`.green);
|
|
|
|
|
|
|
|
await showStagedDiff();
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
await wantToContinue('Should I commit those changes?');
|
2020-04-28 01:08:00 +02:00
|
|
|
|
|
|
|
await commit();
|
|
|
|
console.log(`✔ All files commited`.green);
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
await wantToContinue('Should I push all pending commits?');
|
2020-04-28 01:08:00 +02:00
|
|
|
|
|
|
|
await push();
|
|
|
|
console.log(`✔ All commits pushed`.green);
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
await wantToContinue('Should I continue to create a Github release?');
|
|
|
|
|
|
|
|
const assetUrls = await createGithubRelease(changeLog);
|
2020-04-28 01:08:00 +02:00
|
|
|
console.log(`✔ released on github`.green);
|
|
|
|
|
2020-04-28 14:16:19 +02:00
|
|
|
const archiveAssetUrl = assetUrls.find(url => url.endsWith('.tar.gz'));
|
|
|
|
|
|
|
|
await wantToContinue('Should I continue to upload the release to the app store?');
|
|
|
|
|
|
|
|
await uploadToNextcloudStore(archiveAssetUrl);
|
|
|
|
console.log(`✔ released in Nextcloud app store`.green);
|
|
|
|
};
|
|
|
|
|
|
|
|
run().catch(err => {
|
|
|
|
console.log(`✘ ${err.toString()}`.error);
|
|
|
|
});
|