UNPKG

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