1 | 'use strict'
|
2 |
|
3 | const assign = require('object-assign')
|
4 | const conventionalChangelog = require('conventional-changelog')
|
5 | const escape = require('querystring').escape
|
6 | const gitSemverTags = require('git-semver-tags')
|
7 | const glGot = require('gl-got')
|
8 | const merge = require('lodash.merge')
|
9 | const Q = require('q')
|
10 | const through = require('through2')
|
11 | const transform = require('./transform')
|
12 |
|
13 |
|
14 | function conventionalGitlabReleaser (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 |
|
47 | writerOpts.headerPartial = writerOpts.headerPartial || ''
|
48 |
|
49 | let endpoint = `${auth.url}/api/v4/`
|
50 |
|
51 | Q.nfcall(gitSemverTags)
|
52 | .then(function (tags) {
|
53 | if (!tags || !tags.length) {
|
54 | setImmediate(userCb, new Error('No semver tags found'))
|
55 | return
|
56 | }
|
57 |
|
58 | const releaseCount = changelogOpts.releaseCount
|
59 | if (releaseCount !== 0) {
|
60 | gitRawCommitsOpts = assign({
|
61 | from: tags[releaseCount]
|
62 | }, gitRawCommitsOpts)
|
63 | }
|
64 |
|
65 | gitRawCommitsOpts.to = gitRawCommitsOpts.to || tags[0]
|
66 | })
|
67 | .then(() => glGot(`version`, {token: auth.token, endpoint}))
|
68 | .catch(() => {
|
69 | endpoint = `${auth.url}/api/v3/`
|
70 | })
|
71 | .then(() => {
|
72 | conventionalChangelog(changelogOpts, context, gitRawCommitsOpts, parserOpts, writerOpts)
|
73 | .on('error', function (err) {
|
74 | userCb(err)
|
75 | })
|
76 | .pipe(through.obj(function (chunk, enc, cb) {
|
77 | if (!chunk.keyCommit || !chunk.keyCommit.version) {
|
78 | cb()
|
79 | return
|
80 | }
|
81 |
|
82 | const promise = glGot('projects/' + escape(context.owner + '/' + context.repository) + '/repository/tags', {
|
83 | token: auth.token,
|
84 | endpoint,
|
85 | body: {
|
86 | tag_name: chunk.keyCommit.version,
|
87 | ref: chunk.keyCommit.hash,
|
88 | message: 'Release ' + chunk.keyCommit.version,
|
89 | release_description: chunk.log
|
90 | }
|
91 | })
|
92 |
|
93 | promises.push(promise)
|
94 |
|
95 | cb()
|
96 | }, function () {
|
97 | Q.all(promises)
|
98 | .then(function (responses) {
|
99 | userCb(null, responses)
|
100 | })
|
101 | .catch(function (err) {
|
102 | userCb(err)
|
103 | })
|
104 | }))
|
105 | })
|
106 | .catch(function (err) {
|
107 | userCb(err)
|
108 | })
|
109 | }
|
110 |
|
111 | module.exports = conventionalGitlabReleaser
|