-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
95 lines (83 loc) · 2.64 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
const { getConfig } = require('./lib/config')
const { isTriggerableBranch } = require('./lib/triggerable-branch')
const {
findReleases,
generateReleaseInfo,
createRelease,
updateRelease
} = require('./lib/releases')
const { findCommitsWithAssociatedPullRequests } = require('./lib/commits')
const { sortPullRequests } = require('./lib/sort-pull-requests')
const log = require('./lib/log')
const core = require('@actions/core')
module.exports = app => {
app.on('push', async context => {
const config = await getConfig({
app,
context,
configName: core.getInput('config-name')
})
if (config === null) return
// GitHub Actions merge payloads slightly differ, in that their ref points
// to the PR branch instead of refs/heads/master
const ref = process.env['GITHUB_REF'] || context.payload.ref
const branch = ref.replace(/^refs\/heads\//, '')
if (!isTriggerableBranch({ branch, app, context, config })) {
return
}
const { draftRelease, lastRelease } = await findReleases({ app, context })
const {
commits,
pullRequests: mergedPullRequests
} = await findCommitsWithAssociatedPullRequests({
app,
context,
branch,
lastRelease
})
const sortedMergedPullRequests = sortPullRequests(
mergedPullRequests,
config['sort-by'],
config['sort-direction']
)
const releaseInfo = generateReleaseInfo({
commits,
config,
lastRelease,
mergedPullRequests: sortedMergedPullRequests,
version: core.getInput('version') || undefined,
tag: core.getInput('tag') || undefined,
name: core.getInput('name') || undefined
})
const shouldDraft = core.getInput('publish').toLowerCase() !== 'true'
let createOrUpdateReleaseResponse
if (!draftRelease) {
log({ app, context, message: 'Creating new release' })
createOrUpdateReleaseResponse = await createRelease({
context,
releaseInfo,
shouldDraft,
config
})
} else {
log({ app, context, message: 'Updating existing release' })
createOrUpdateReleaseResponse = await updateRelease({
context,
draftRelease,
releaseInfo,
shouldDraft,
config
})
}
setActionOutput(createOrUpdateReleaseResponse)
})
}
function setActionOutput(releaseResponse) {
const {
data: { id: releaseId, html_url: htmlUrl, upload_url: uploadUrl }
} = releaseResponse
if (releaseId && Number.isInteger(releaseId))
core.setOutput('id', releaseId.toString())
if (htmlUrl) core.setOutput('html_url', htmlUrl)
if (uploadUrl) core.setOutput('upload_url', uploadUrl)
}