forked from release-drafter/release-drafter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (86 loc) · 2.8 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
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 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 })
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
})
if (!draftRelease) {
log({ app, context, message: 'Creating new draft release' })
await context.github.repos.createRelease(
context.repo({
name: releaseInfo.name,
tag_name: releaseInfo.tag,
body: releaseInfo.body,
draft: true
})
)
} else {
log({ app, context, message: 'Updating existing draft release' })
await context.github.repos.updateRelease(
context.repo({
release_id: draftRelease.id,
body: releaseInfo.body
})
)
}
})
}