UNPKG

3.27 kBJavaScriptView Raw
1'use strict';
2
3const assign = require('object-assign');
4const conventionalChangelog = require('conventional-changelog');
5const gitSemverTags = require('git-semver-tags');
6const ghGot = require('gh-got');
7const merge = require('lodash.merge');
8const Q = require('q');
9const semver = require('semver');
10const through = require('through2');
11const transform = require('./transform');
12
13/* eslint max-params: ["error", 7] */
14function conventionalGithubReleaser(auth, changelogOpts, context, gitRawCommitsOpts, parserOpts, writerOpts, userCb) {
15 if (!auth) {
16 throw new Error('Expected an auth object');
17 }
18
19 const promises = [];
20
21 const changelogArgs = [changelogOpts, context, gitRawCommitsOpts, parserOpts, writerOpts].map(function (arg) {
22 if (typeof arg === 'function') {
23 userCb = arg;
24 return {};
25 }
26 return arg || {};
27 });
28
29 if (!userCb) {
30 throw new Error('Expected an callback');
31 }
32
33 changelogOpts = changelogArgs[0];
34 context = changelogArgs[1];
35 gitRawCommitsOpts = changelogArgs[2];
36 parserOpts = changelogArgs[3];
37 writerOpts = changelogArgs[4];
38
39 changelogOpts = merge({
40 transform: transform,
41 releaseCount: 1,
42 }, changelogOpts);
43
44 writerOpts.includeDetails = true;
45
46 // ignore the default header partial
47 writerOpts.headerPartial = writerOpts.headerPartial || '';
48
49 Q.nfcall(gitSemverTags)
50 .then(function (tags) {
51 if (!tags || !tags.length) {
52 setImmediate(userCb, new Error('No semver tags found'));
53 return;
54 }
55
56 const releaseCount = changelogOpts.releaseCount;
57 if (releaseCount !== 0) {
58 gitRawCommitsOpts = assign({
59 from: tags[releaseCount],
60 }, gitRawCommitsOpts);
61 }
62
63 gitRawCommitsOpts.to = gitRawCommitsOpts.to || tags[0];
64
65 conventionalChangelog(changelogOpts, context, gitRawCommitsOpts, parserOpts, writerOpts)
66 .on('error', function (err) {
67 userCb(err);
68 })
69 .pipe(through.obj(function (chunk, enc, cb) {
70 if (!chunk.keyCommit || !chunk.keyCommit.version) {
71 cb();
72 return;
73 }
74
75 const version = chunk.keyCommit.version;
76 const prerelease = semver.parse(version).prerelease.length > 0;
77 const draft = changelogOpts.draft || false;
78
79 const options = {
80 body: {
81 body: chunk.log,
82 draft: draft,
83 name: changelogOpts.name || version,
84 prerelease: prerelease,
85 tag_name: version,
86 target_commitish: changelogOpts.targetCommitish,
87 },
88 };
89
90 if (auth.token) {
91 options.token = auth.token;
92 }
93
94 if (auth.url) {
95 options.endpoint = auth.url;
96 }
97
98 const promise = ghGot('repos/' + context.owner + '/' + context.repository + '/releases', options);
99
100 promises.push(promise);
101
102 cb();
103 }, function () {
104 Q.all(promises)
105 .then(function (responses) {
106 userCb(null, responses);
107 })
108 .catch(function (err) {
109 userCb(err);
110 });
111 }));
112 })
113 .catch(function (err) {
114 userCb(err);
115 });
116}
117
118module.exports = conventionalGithubReleaser;