Files
grafana-zabbix/.circleci/github/publishRelease.js
Alexander Zobnin ec48fa0e55 New release process
2020-06-04 15:05:54 +03:00

85 lines
2.5 KiB
JavaScript

const GithubClient = require('./githubClient');
const fs = require('fs');
const path = require('path');
const GRAFANA_ZABBIX_OWNER = 'alexanderzobnin';
const GRAFANA_ZABBIX_REPO = 'grafana-zabbix';
const github = new GithubClient(GRAFANA_ZABBIX_OWNER, GRAFANA_ZABBIX_REPO, true);
async function main() {
const tag = process.env.CIRCLE_TAG;
const tagRegex = /v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)/;
if (!tagRegex.test(tag)) {
console.error(`Release tag should has format v1.2.3[-meta], got ${tag}`);
process.exit(1);
}
const releaseVersion = tag.slice(1);
let releaseId;
try {
const latestRelease = await github.client.get(`releases/tags/v${releaseVersion}`);
releaseId = latestRelease.data.id;
} catch (reason) {
if (reason.response.status !== 404) {
// 404 just means no release found. Not an error. Anything else though, re throw the error
throw reason;
} else {
console.error(`No release found`);
process.exit(1);
}
}
try {
const updateReleaseResponse = await github.client.put(`releases/${releaseId}`, {
tag_name: `v${releaseVersion}`,
name: `${releaseVersion}`,
body: `Grafana-Zabbix ${releaseVersion}`,
draft: false,
prerelease: false,
});
await publishAssets(
`./grafana-zabbix-${releaseVersion}.zip`,
`https://uploads.github.com/repos/${GRAFANA_ZABBIX_OWNER}/${GRAFANA_ZABBIX_REPO}/releases/${updateReleaseResponse.data.id}/assets`
);
} catch (reason) {
console.error(reason.data || reason.response || reason);
// Rethrow the error so that we can trigger a non-zero exit code to circle-ci
throw reason;
}
}
async function publishAssets(fileName, destUrl) {
// Add the assets. Loop through files in the ci/dist folder and upload each asset.
const fileStat = fs.statSync(`${fileName}`);
const fileData = fs.readFileSync(`${fileName}`);
return await github.client.post(`${destUrl}?name=${fileName}`, fileData, {
headers: {
'Content-Type': resolveContentType(path.extname(fileName)),
'Content-Length': fileStat.size,
},
maxContentLength: fileStat.size * 2 * 1024 * 1024,
});
}
const resolveContentType = (extension) => {
if (extension.startsWith('.')) {
extension = extension.substr(1);
}
switch (extension) {
case 'zip':
return 'application/zip';
case 'json':
return 'application/json';
case 'sha1':
return 'text/plain';
default:
return 'application/octet-stream';
}
};
main();