forked from johnfoconnor/changelog-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (101 loc) · 2.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { getInput, setFailed, setOutput } from '@actions/core'
import { exec as _exec } from '@actions/exec'
import { getOctokit, context } from '@actions/github'
const src = __dirname
async function run() {
try {
var headRef = getInput('head-ref')
var baseRef = getInput('base-ref')
const myToken = getInput('myToken')
const reverse = getInput('reverse').toLowerCase() === 'true'
const fetch = getInput('fetch').toLowerCase() === 'true'
const headRefCommitsOnly = getInput('headRefCommitsOnly').toLowerCase() === 'true'
const octokit = new getOctokit(myToken)
const { owner, repo } = context.repo
const regexp = /^[.A-Za-z0-9_/-]*$/
if (!headRef) {
headRef = context.sha
}
if (!baseRef) {
const latestRelease = await octokit.rest.repos.getLatestRelease({
owner: owner,
repo: repo
})
if (latestRelease) {
baseRef = latestRelease.data.tag_name
} else {
setFailed(
`There are no releases on ${owner}/${repo}. Tags are not releases.`
)
}
}
console.log(`head-ref: ${headRef}`)
console.log(`base-ref: ${baseRef}`)
if (
!!headRef &&
!!baseRef &&
regexp.test(headRef) &&
regexp.test(baseRef)
) {
getChangelog(headRef, baseRef, owner + '/' + repo, reverse, fetch, headRefCommitsOnly)
} else {
setFailed(
'Branch names must contain only numbers, strings, underscores, periods, forward slashes, and dashes.'
)
}
} catch (error) {
setFailed(error.message)
}
}
async function getChangelog(headRef, baseRef, repoName, reverse, fetch, headRefCommitsOnly) {
try {
let output = ''
let err = ''
// These are option configurations for the @actions/exec lib`
const options = {}
options.listeners = {
stdout: data => {
output += data.toString()
},
stderr: data => {
err += data.toString()
}
}
options.cwd = './'
var args = [headRef, baseRef, repoName];
if (reverse) {
args.push("--reverse");
}
if (fetch) {
args.push("--fetch");
}
if (headRefCommitsOnly) {
args.push("--head_ref_only");
}
await _exec(
`${src}/changelog.sh`,
args,
options
)
if (output) {
console.log(
'\x1b[32m%s\x1b[0m',
`Changelog between ${baseRef} and ${headRef}:\n${output}`
)
setOutput('changelog', output)
} else {
setFailed(err)
process.exit(1)
}
} catch (err) {
setFailed(
`Could not generate changelog between references because: ${err.message}`
)
process.exit(0)
}
}
try {
run()
} catch (error) {
setFailed(error.message)
}