-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
102 lines (85 loc) · 2.97 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
const actionscore = require('@actions/core')
const { setFailed, warning, notice, debug, setOutput } = actionscore
const actionsgithub = require('@actions/github')
const { GitHub, context } = actionsgithub
async function run () {
try {
// Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage
const github = new GitHub(process.env.GITHUB_TOKEN)
// Get owner and repo from context of payload that triggered the action
const { owner, repo } = context.repo
// Get the inputs from the workflow file: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs
const id = process.env.RELEASE_ID || process.env.INPUT_RELEASE_ID || '' // getInput('release_id', { required: false })
const tag = process.env.TAG || process.env.INPUT_TAG || '' // getInput('tag', { required: false })
const deleteOrphan = (process.env.INPUT_DELETE_ORPHAN_TAG || '').trim().toLowerCase() === 'true'
if (!id && !tag) {
setFailed('At least one of the following inputs must be defined: release_id or tag.')
return
}
// Retrieve the release ID
let data
if (!id) {
try {
data = await github.repos.getReleaseByTag({
owner,
repo,
tag
})
} catch (e) {
warning(`Could not retrieve release for ${tag}: ${e.message}`)
}
if (!data) {
if (deleteOrphan) {
const deleteTagResponse = await github.git.deleteRef({
owner,
repo,
ref: `tags/${tag}`
})
if (deleteTagResponse) {
notice(`Removed ${tag}, even though there was no associated release.`)
return
}
}
setFailed(`Tag "${tag}" was not found or a release ID is not associated with it.`)
return
}
data = data.data
} else {
data = await github.repos.getRelease({
owner,
repo,
release_id: id
})
if (!data) {
debug(JSON.stringify(data, null, 2))
setFailed(`Release "${id}" was not found.`)
return
}
data = data.data
}
debug(JSON.stringify(data, null, 2))
// API Documentation: https://developer.github.com/v3/repos/releases/#delete-a-release
// Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-delete-release
debug(`Removing release ${data.id}`)
const response = await github.repos.deleteRelease({
owner,
repo,
release_id: data.id
})
debug(JSON.stringify(response, null, 2))
// Delete tag reference
debug(`Removing reference: tags/${data.tag_name}`)
const tagresponse = await github.git.deleteRef({
owner,
repo,
ref: `tags/${data.tag_name}`
})
debug(JSON.stringify(tagresponse, null, 2))
setOutput('release_id', data.id.toString())
setOutput('tag', data.tag_name.toString())
} catch (e) {
warning(e.stack)
setFailed(e.message)
}
}
run()