UNPKG

3.08 kBJavaScriptView Raw
1'use strict'
2
3const assign = require('object-assign')
4const conventionalChangelog = require('conventional-changelog')
5const escape = require('querystring').escape
6const gitSemverTags = require('git-semver-tags')
7const glGot = require('gl-got')
8const merge = require('lodash.merge')
9const Q = require('q')
10const through = require('through2')
11const transform = require('./transform')
12
13/* eslint max-params: ["error", 7] */
14function 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 // ignore the default header partial
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
111module.exports = conventionalGitlabReleaser