forked from oleg-nenashev/jenkins-release-drafter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (126 loc) · 4.25 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
133
134
135
136
137
138
const getConfig = require('probot-config')
const { isTriggerableBranch } = require('./lib/triggerable-branch')
const { findReleases, generateReleaseInfo } = require('./lib/releases')
const { findCommitsWithAssociatedPullRequests } = require('./lib/commits')
const { validateReplacers } = require('./lib/template')
const {
validateSortDirection,
sortPullRequests,
SORT_DIRECTIONS
} = require('./lib/sort-pull-requests')
const log = require('./lib/log')
const configName = 'release-drafter.yml'
module.exports = app => {
app.on('push', async context => {
const defaults = {
branches: context.payload.repository.default_branch,
'change-template': `* $TITLE (#$NUMBER) @$AUTHOR`,
'no-changes-template': `* No changes`,
'version-template': `$MAJOR.$MINOR.$PATCH`,
categories: [],
'exclude-labels': [],
replacers: [],
'sort-direction': SORT_DIRECTIONS.descending
}
const config = Object.assign(
defaults,
(await getConfig(context, configName)) || {}
)
config.replacers = validateReplacers({
app,
context,
replacers: config.replacers
})
config['sort-direction'] = validateSortDirection(config['sort-direction'])
// 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 sha = 'untagged-' + context.payload.after.substring(0, 7)
const branch = ref.replace(/^refs\/heads\//, '')
if (!config.template) {
log({ app, context, message: 'No valid config found' })
return
}
if (!isTriggerableBranch({ branch, app, context, config })) {
return
}
const { draftRelease, lastRelease } = await findReleases({ app, context })
var assetFound = false
if (draftRelease) {
if (draftRelease.tag_name == sha) {
assetFound = true
}
}
if (!assetFound) {
let currentRelease
// Update the tag name of the current draft release or create a new one if no draft can be found
if (!draftRelease) {
log({ app, context, message: 'Creating new draft release' })
currentRelease = await context.github.repos.createRelease(
context.repo({
tag_name: sha,
draft: true
})
)
} else {
log({ app, context, message: 'Updating existing draft release' })
await context.github.repos.updateRelease(
context.repo({
release_id: draftRelease.id,
tag_name: sha
})
)
currentRelease = await context.github.repos.getRelease(
context.repo({
release_id: draftRelease.id
})
)
}
const {
commits,
pullRequests: mergedPullRequests
} = await findCommitsWithAssociatedPullRequests({
app,
context,
branch,
lastRelease
})
const sortedMergedPullRequests = sortPullRequests(
mergedPullRequests,
config['sort-direction']
)
const releaseInfo = generateReleaseInfo({
commits,
config,
lastRelease,
mergedPullRequests: sortedMergedPullRequests
})
log({ app, context, message: 'Updating draft release body' })
await context.github.repos.updateRelease(
context.repo({
release_id: currentRelease.data.id,
body: releaseInfo.body
})
)
for (var asset in currentRelease.data.assets) {
await context.github.repos.deleteReleaseAsset(
context.repo({
asset_id: currentRelease.data.assets[asset].id
})
)
}
console.log('::set-output name=tagname::' + currentRelease.data.tag_name)
console.log(
'::set-output name=uploadurl::' + currentRelease.data.upload_url
)
} else {
log({
app,
context,
message: 'Matching assets found. Change log already up-to-date.'
})
console.log('::set-output name=tagname::' + draftRelease.tag_name)
console.log('::set-output name=uploadurl::' + draftRelease.upload_url)
}
})
}