1 | 'use strict'
|
2 |
|
3 | const bluebird = require('bluebird')
|
4 | const url = require('url')
|
5 | const GitHubApi = require('github')
|
6 | const parseGithubUrl = require('parse-github-repo-url')
|
7 | const debug = require('debug')('github-post-release')
|
8 | const newPublicCommits = require('new-public-commits').newPublicCommits
|
9 | const R = require('ramda')
|
10 | const pluralize = require('pluralize')
|
11 | const join = require('path').join
|
12 | const utils = require('./utils')
|
13 | const formChangelog = require('./changelog').formChangelog
|
14 |
|
15 |
|
16 | function getClosedIssues () {
|
17 | return newPublicCommits().then(utils.commitsToIssues)
|
18 | }
|
19 |
|
20 | function getGitHub (githubUrl, token) {
|
21 | if (!token) {
|
22 | throw new Error('Missing gh token')
|
23 | }
|
24 | const githubConfig = githubUrl ? url.parse(githubUrl) : {}
|
25 |
|
26 | let protocol = (githubConfig.protocol || '').split(':')[0] || null
|
27 | if (protocol === 'git+https') {
|
28 | debug('switching github protocol from %s to https', protocol)
|
29 | protocol = 'https'
|
30 | }
|
31 |
|
32 | let host = githubConfig.hostname
|
33 | if (host === 'github.com') {
|
34 |
|
35 | debug('using api.github.com host')
|
36 | host = 'api.github.com'
|
37 | }
|
38 |
|
39 | const config = {
|
40 | version: '3.0.0',
|
41 | port: githubConfig.port,
|
42 | protocol,
|
43 | host
|
44 | }
|
45 | debug('github config')
|
46 | debug(config)
|
47 |
|
48 | const github = new GitHubApi(config)
|
49 |
|
50 | github.authenticate({
|
51 | type: 'token',
|
52 | token: token
|
53 | })
|
54 |
|
55 | const createComment = bluebird.promisify(github.issues.createComment)
|
56 | return createComment
|
57 | }
|
58 |
|
59 | function getGitHubToken () {
|
60 | return process.env.GH_TOKEN
|
61 | }
|
62 |
|
63 | function commentOnIssues (repoUrl, message, debugMode, issues) {
|
64 | if (!issues) {
|
65 | return Promise.resolve()
|
66 | }
|
67 | if (R.isEmpty(issues)) {
|
68 | debug('no issues to comment on')
|
69 | return Promise.resolve()
|
70 | }
|
71 |
|
72 | const createComment = debugMode
|
73 | ? R.identity
|
74 | : getGitHub(repoUrl, getGitHubToken())
|
75 | const parsed = parseGithubUrl(repoUrl)
|
76 | const owner = parsed[0]
|
77 | const repo = parsed[1]
|
78 | debug(
|
79 | 'commenting on %d %s: %j',
|
80 | issues.length,
|
81 | pluralize('issues', issues.length),
|
82 | issues
|
83 | )
|
84 |
|
85 | const onPosted = number => () => {
|
86 | console.log('posted comment for issue', number)
|
87 | }
|
88 |
|
89 | const onFailed = number => err => {
|
90 | console.error('Could not comment on issue', number)
|
91 | console.error(err)
|
92 | }
|
93 |
|
94 | const commentPromises = issues.map(number =>
|
95 | createComment({ owner, repo, number, body: message })
|
96 | .then(onPosted(number))
|
97 | .catch(onFailed(number))
|
98 | )
|
99 |
|
100 | return bluebird.all(commentPromises)
|
101 | }
|
102 |
|
103 |
|
104 | function githubPostRelease (pluginConfig, config, callback) {
|
105 |
|
106 |
|
107 | const pkg = config.pkg
|
108 | const repoUrl = pkg.repository.url
|
109 | const parsedRepo = parseGithubUrl(repoUrl)
|
110 |
|
111 | debug('published version %s', pkg.version)
|
112 | debug('repo url %s', repoUrl)
|
113 |
|
114 | const onSuccess = changelog => {
|
115 | debug('✅ all done, with message: %s', message)
|
116 | debug('changelog:')
|
117 | debug(changelog)
|
118 | callback(null, changelog)
|
119 | }
|
120 |
|
121 | const onFailure = err => {
|
122 | console.error('🔥 failed with error')
|
123 | console.error(err)
|
124 | callback(err)
|
125 | }
|
126 |
|
127 | const commentingFailed = err => {
|
128 | console.error('😟 commenting on related issues failed')
|
129 | console.error(err)
|
130 | }
|
131 |
|
132 | const generateChangeLog = () => {
|
133 | debug('generate changelog for release version %s', pkg.version)
|
134 | return formChangelog(pkg.version)
|
135 | }
|
136 |
|
137 | const owner = parsedRepo[0]
|
138 | const repo = parsedRepo[1]
|
139 | const message = utils.formMessage(owner, repo, pkg.name, pkg.version)
|
140 |
|
141 | getClosedIssues()
|
142 | .then(R.partial(commentOnIssues, [repoUrl, message, config.debug]))
|
143 | .catch(commentingFailed)
|
144 | .then(generateChangeLog)
|
145 | .then(onSuccess, onFailure)
|
146 | }
|
147 |
|
148 | module.exports = githubPostRelease
|
149 |
|
150 | if (!module.parent) {
|
151 | console.log('demo run')
|
152 | const pkg = require(join(__dirname, '..', 'package.json'))
|
153 | const generateNotes = R.path(['release', 'generateNotes'])(pkg)
|
154 | const config = R.is(Object, generateNotes) ? generateNotes : {}
|
155 | githubPostRelease(config, { pkg: pkg }, (err, changelog) => {
|
156 | console.log('finished with')
|
157 | console.log('err?', err)
|
158 | console.log('changelog\n' + changelog)
|
159 | })
|
160 | }
|