-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (154 loc) · 5.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const core = require('@actions/core')
const github = require('@actions/github')
const semverRegExp = /(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/
const dateRegExp = /(0|\d{4})-(0|\d{2})-(0|\d{2})/
const headerRegExp = new RegExp(`^## \\[${semverRegExp.source}\\] - ${dateRegExp.source}$`)
async function run() {
try {
core.info('Creating release...')
const path = core.getInput('path')
const draft = core.getInput('draft') === 'true'
const mutable = core.getInput('mutable') === 'true'
const token = core.getInput('token')
core.info(`Path: ${path}`)
core.info(`Draft: ${draft}`)
core.info(`Mutable: ${mutable}`)
core.info(`Token: ${token != null ? '***' : null}`)
const octokit = new github.getOctokit(token)
core.info('Getting changelog...')
let owner, repo, ref
if (github.context.eventName == 'pull_request') {
[owner, repo] = github.context.payload.pull_request.head.repo.full_name.split('/')
ref = github.context.payload.pull_request.head.sha
} else {
owner = github.context.repo.owner
repo = github.context.repo.repo
ref = github.context.sha
}
core.info(`Owner: ${owner}`)
core.info(`Repo: ${repo}`)
core.info(`Ref: ${ref}`)
const content = await octokit.rest.repos.getContent({
owner,
repo,
path,
ref
})
const changelog = Buffer.from(content.data.content, 'base64').toString()
core.debug(`Changelog: ${changelog}`)
core.info('Parsing changelog...')
let version, body
for (const line of changelog.split('\n')) {
if (line.match(headerRegExp)) {
if (version != null) {
break
}
version = line.match(semverRegExp)
} else if (version != null) {
if (body == null) {
body = line
} else {
body += '\n' + line
}
}
}
if (version == null) {
core.info('No release found')
return
}
const tag = `v${version[0]}`
core.info(`Version: ${version[0]}`)
core.info(`Body: ${body}`)
core.info(`Tag: ${tag}`)
core.info('Listing releases...')
let releases = await octokit.paginate(octokit.rest.repos.listReleases, github.context.repo)
let release = releases.find(release => release.tag_name === tag)
if (release != null && release.published_at != null) {
core.info('Release is already published')
return
}
let target
if (github.context.eventName == 'pull_request') {
target = github.context.payload.pull_request.base.ref
} else {
target = github.context.sha
}
if (release == null) {
core.info('Creating release...')
const options = {
...github.context.repo,
tag_name: tag,
target_commitish: target,
name: tag,
body,
draft,
prerelease: version[4] != null
}
if (body.trim() !== '') {
options.body = body
} else {
options.generate_release_notes = true
}
const response = (await octokit.rest.repos.createRelease(options)).data
core.info(JSON.stringify(response, null, 2))
} else {
core.info('Updating release...')
const response = await octokit.rest.repos.updateRelease({
...github.context.repo,
release_id: release.id,
target_commitish: target,
draft
})
core.info(JSON.stringify(response, null, 2))
}
core.info('Waiting for 10 seconds...')
await new Promise(resolve => setTimeout(resolve, 10000))
core.info('Listing releases...')
releases = await octokit.paginate(octokit.rest.repos.listReleases, github.context.repo)
release = releases.find(release => release.tag_name === tag)
if (release == null) {
throw new Error('Release not found')
}
core.info(`Release: ${release.html_url}`)
const tags = []
if (mutable && release.published_at != null) {
core.info('Updating mutable tags...')
const suffix = `${version[4] != null ? '-' + version[4] : ''}${version[5] != null ? '+' + version[5] : ''}`
tags.push(`v${version[1]}.${version[2]}${suffix}`)
tags.push(`v${version[1]}${suffix}`)
for (const tag of tags) {
core.info(`Tag: ${tag}`)
core.info('Listing refs...')
const refs = await octokit.rest.git.listMatchingRefs({
...github.context.repo,
ref: `tags/${tag}`
})
const ref = refs.data.find(ref => ref.ref === `refs/tags/${tag}`)
if (ref == null) {
core.info('Creating ref...')
await octokit.rest.git.createRef({
...github.context.repo,
ref: `refs/tags/${tag}`,
sha: github.context.sha
})
} else {
core.info('Updating ref...')
await octokit.rest.git.updateRef({
...github.context.repo,
ref: `tags/${tag}`,
sha: github.context.sha,
force: true
})
}
}
}
tags.push(tag)
core.setOutput("url", release.html_url)
core.setOutput("tag", tag)
core.setOutput("tags", tags.join(','))
core.setOutput("body", body)
} catch (error) {
core.setFailed(error.message)
}
}
run()