forked from release-drafter/release-drafter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (118 loc) · 3.7 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const { getConfig } = require('./lib/config')
const { isTriggerableReference } = require('./lib/triggerable-reference')
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 { shouldDraft, configName, version, tag, name } = getInput()
const config = await getConfig({
app,
context,
configName,
})
const { isPreRelease } = getInput({ config })
if (config === null) return
// GitHub Actions merge payloads slightly differ, in that their ref points
// to the PR branch instead of refs/heads/master
// context.payload.ref
const ref = context.payload.ref
const masterRef = 'refs/heads/master'
if (!isTriggerableReference({ ref:masterRef, app, context, config })) {
return
}
const { draftRelease, lastRelease, lastPreRelease } = await findReleases({ app, context })
let basisRelease = lastRelease;
if(ref !== masterRef) {
basisRelease = lastPreRelease;
log({ app, context, message: `basisRelease : ${basisRelease.created_at}` })
}
const {
commits,
pullRequests: mergedPullRequests,
} = await findCommitsWithAssociatedPullRequests({
app,
context,
ref: masterRef,
lastRelease: basisRelease,
config,
})
const sortedMergedPullRequests = sortPullRequests(
mergedPullRequests,
config['sort-by'],
config['sort-direction']
)
const releaseInfo = generateReleaseInfo({
commits,
config,
lastRelease: basisRelease,
mergedPullRequests: sortedMergedPullRequests,
version,
tag,
name,
isPreRelease,
shouldDraft,
})
let createOrUpdateReleaseResponse
if (!draftRelease) {
log({ app, context, message: 'Creating new release' })
createOrUpdateReleaseResponse = await createRelease({
context,
releaseInfo,
config,
})
} else {
log({ app, context, message: 'Updating existing release' })
createOrUpdateReleaseResponse = await updateRelease({
context,
draftRelease,
releaseInfo,
config,
})
}
setActionOutput(createOrUpdateReleaseResponse, releaseInfo)
})
}
function getInput({ config } = {}) {
// Returns all the inputs that doesn't need a merge with the config file
if (!config) {
return {
shouldDraft: core.getInput('publish').toLowerCase() !== 'true',
configName: core.getInput('config-name'),
version: core.getInput('version') || undefined,
tag: core.getInput('tag') || undefined,
name: core.getInput('name') || undefined,
}
}
// Merges the config file with the input
// the input takes precedence, because it's more easy to change at runtime
const preRelease = core.getInput('prerelease').toLowerCase()
return {
isPreRelease: preRelease === 'true' || (!preRelease && config.prerelease),
}
}
function setActionOutput(releaseResponse, { body }) {
const {
data: {
id: releaseId,
html_url: htmlUrl,
upload_url: uploadUrl,
tag_name: tagName,
name: name,
},
} = 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)
if (tagName) core.setOutput('tag_name', tagName)
if (name) core.setOutput('name', name)
core.setOutput('body', body)
}