Ghost/ghost/release-utils/lib/releases.js

175 lines
4.6 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
const os = require('os');
const _ = require('lodash');
const Promise = require('bluebird');
const requestPromise = require('request-promise');
const request = require('request');
const localUtils = require('./utils');
function getFinalChangelog(options) {
let filterEmojiCommits = true;
let changelog = fs.readFileSync(options.changelogPath).toString('utf8').split(os.EOL);
let finalChangelog = [];
2020-03-10 20:07:36 +03:00
if (Object.prototype.hasOwnProperty.call(options, 'filterEmojiCommits')) {
filterEmojiCommits = options.filterEmojiCommits;
}
// @NOTE: optional array of string lines, which we pre-pend
2020-03-10 20:07:36 +03:00
if (Object.prototype.hasOwnProperty.call(options, 'content') && _.isArray(options.content)) {
finalChangelog = finalChangelog.concat(options.content);
}
if (filterEmojiCommits) {
changelog = localUtils.filterEmojiCommits(changelog);
localUtils.sortByEmoji(changelog);
}
if (_.isEmpty(changelog)) {
changelog = ['No user-visible changes in this release.'];
}
finalChangelog = finalChangelog.concat(changelog);
return finalChangelog;
}
module.exports.create = (options = {}) => {
let draft = true;
let prerelease = false;
localUtils.checkMissingOptions(options,
'changelogPath',
'github',
'github.token',
'userAgent',
'uri',
'tagName',
'releaseName'
);
2020-03-10 20:07:36 +03:00
if (Object.prototype.hasOwnProperty.call(options, 'draft')) {
draft = options.draft;
}
2020-03-10 20:07:36 +03:00
if (Object.prototype.hasOwnProperty.call(options, 'prerelease')) {
prerelease = options.prerelease;
}
let body = [];
// CASE: changelogPath can be array of paths with content
if (_.isArray(options.changelogPath)) {
options.changelogPath.forEach((opts) => {
body = body.concat(getFinalChangelog(opts));
});
} else {
// CASE: changelogPath can be a single path(For backward compatibility)
body = body.concat(getFinalChangelog(options));
}
// CASE: clean before upload
body = body.filter((item) => {
return item !== undefined;
});
if (options.gistUrl) {
body.push('');
body.push('You can see the [full change log](' + options.gistUrl + ') for the details of every change included in this release.');
}
2020-04-07 14:46:51 +03:00
if (options.extraText) {
body.push('');
body.push(options.extraText);
}
const auth = 'token ' + options.github.token;
const reqOptions = {
uri: options.uri,
headers: {
'User-Agent': options.userAgent,
Authorization: auth
},
method: 'POST',
body: {
tag_name: options.tagName,
target_commitish: 'master',
name: options.releaseName,
body: body.join(os.EOL),
draft: draft,
prerelease: prerelease
},
json: true,
resolveWithFullResponse: true
};
return requestPromise(reqOptions)
.then((response) => {
return {
id: response.body.id,
releaseUrl: response.body.html_url,
uploadUrl: response.body.upload_url
};
});
};
module.exports.uploadZip = (options = {}) => {
localUtils.checkMissingOptions(options,
'zipPath',
'github',
'github.token',
'userAgent',
'uri'
);
const auth = 'token ' + options.github.token;
const stats = fs.statSync(options.zipPath);
const reqOptions = {
uri: options.uri,
headers: {
'User-Agent': options.userAgent,
Authorization: auth,
'Content-Type': 'application/zip',
'Content-Length': stats.size
},
method: 'POST',
json: true,
resolveWithFullResponse: true
};
return new Promise((resolve, reject) => {
fs.createReadStream(options.zipPath)
.on('error', reject)
.pipe(request.post(reqOptions, (err, res) => {
if (err) {
return reject(err);
}
resolve({
downloadUrl: res.body.browser_download_url
});
}));
});
};
module.exports.get = (options = {}) => {
localUtils.checkMissingOptions(options,
'userAgent',
'uri'
);
const reqOptions = {
uri: options.uri,
headers: {
'User-Agent': options.userAgent
},
method: 'GET',
json: true
};
return requestPromise(reqOptions)
.then((response) => {
return response;
});
};