-
Notifications
You must be signed in to change notification settings - Fork 320
/
index.js
117 lines (106 loc) · 3.48 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
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 { incrementVersionBasedOnLabels } = require('./lib/versions')
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,
'auto-release': false
}
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
})
let releaseId
if (!draftRelease) {
log({ app, context, message: 'Creating new draft release' })
const response = await context.github.repos.createRelease(
context.repo({
name: releaseInfo.name,
tag_name: releaseInfo.tag,
body: releaseInfo.body,
draft: true
})
)
releaseId = response.data.id
} else {
log({ app, context, message: 'Updating existing draft release' })
await context.github.repos.updateRelease(
context.repo({
release_id: draftRelease.id,
body: releaseInfo.body
})
)
releaseId = draftRelease.id
}
if (config['auto-release']) {
log({ app, context, message: 'Autoreleasing!' })
const thisVersion = incrementVersionBasedOnLabels(
lastRelease,
mergedPullRequests
)
await context.github.repos.updateRelease(
context.repo({
release_id: releaseId,
draft: false,
name: thisVersion, // TODO - how should name and tag_name behave here?
tag_name: thisVersion
})
)
}
})
}