1 | 'use strict'
|
2 |
|
3 | const assign = require('object-assign')
|
4 | const conventionalChangelog = require('conventional-changelog')
|
5 | const debug = require(`debug`)(`conventional-github-releaser`)
|
6 | const escape = require('querystring').escape
|
7 | const gitSemverTags = require('git-semver-tags')
|
8 | const glGot = require('gl-got')
|
9 | const merge = require('lodash.merge')
|
10 | const Q = require('q')
|
11 | const through = require('through2')
|
12 | const transform = require('./transform')
|
13 |
|
14 |
|
15 | function 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 |
|
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 |
|
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 |
|
116 | module.exports = conventionalGitlabReleaser
|