-
Notifications
You must be signed in to change notification settings - Fork 68.4k
Expand file tree
/
Copy pathlabeler.ts
More file actions
163 lines (139 loc) · 4.48 KB
/
Copy pathlabeler.ts
File metadata and controls
163 lines (139 loc) · 4.48 KB
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
/* See function main in this file for documentation */
import * as coreLib from '@actions/core'
import { type Octokit } from '@octokit/rest'
import { CoreInject } from '@/links/scripts/action-injections'
import github from '@/workflows/github'
import { getActionContext } from '@/workflows/action-context'
import { boolEnvVar } from '@/workflows/get-env-inputs'
type Options = {
addLabels?: string[]
removeLabels?: string[]
ignoreIfAssigned?: boolean
ignoreIfLabeled?: boolean
issue_number?: number
owner?: string
repo?: string
}
// When this file is invoked directly from action as opposed to being imported
if (import.meta.url.endsWith(process.argv[1])) {
if (!process.env.GITHUB_TOKEN) {
throw new Error('You must set the GITHUB_TOKEN environment variable.')
}
const { ADD_LABELS, REMOVE_LABELS } = process.env
const octokit = github()
const opts: Options = {
ignoreIfAssigned: boolEnvVar('IGNORE_IF_ASSIGNED'),
ignoreIfLabeled: boolEnvVar('IGNORE_IF_LABELED'),
}
// labels come in comma separated from actions
if (typeof ADD_LABELS === 'string') {
opts.addLabels = [...ADD_LABELS.split(',')].map((l) => l.trim())
} else {
opts.addLabels = []
}
if (typeof REMOVE_LABELS === 'string') {
opts.removeLabels = [...REMOVE_LABELS.split(',')].map((l) => l.trim())
} else {
opts.removeLabels = []
}
const actionContext = getActionContext()
const { owner, repo } = actionContext
let issueOrPrNumber = actionContext?.pull_request?.number
if (!issueOrPrNumber) {
issueOrPrNumber = actionContext?.issue?.number
}
opts.issue_number = issueOrPrNumber
opts.owner = owner
opts.repo = repo
main(coreLib, octokit, opts)
}
/*
* Applies labels to an issue or pull request.
*
* opts:
* issue_number {number} id of the issue or pull request to label
* owner {string} owner of the repository
* repo {string} repository name
* addLabels {Array<string>} array of labels to apply
* removeLabels {Array<string>} array of labels to remove
* ignoreIfAssigned {boolean} don't apply labels if there are assignees
* ignoreIfLabeled {boolean} don't apply labels if there are already labels added
*/
export default async function main(
core: typeof coreLib | CoreInject,
octokit: Octokit,
opts: Options = {},
) {
if (opts.addLabels?.length === 0 && opts.removeLabels?.length === 0) {
core.info('No labels to add or remove specified, nothing to do.')
return
}
if (!opts.issue_number || !opts.owner || !opts.repo) {
throw new Error(`Missing required parameters ${JSON.stringify(opts)}`)
}
const issueOpts = {
issue_number: opts.issue_number,
owner: opts.owner,
repo: opts.repo,
}
if (opts.ignoreIfAssigned || opts.ignoreIfLabeled) {
try {
const { data } = await octokit.issues.get(issueOpts)
if (opts.ignoreIfAssigned) {
if (data.assignees?.length) {
core.info(
`ignore-if-assigned is true: not applying labels since there's ${data.assignees.length} assignees`,
)
return 0
}
}
if (opts.ignoreIfLabeled) {
if (data.labels.length > 0) {
core.info(
`ignore-if-labeled is true: not applying labels since there's ${data.labels.length} labels applied`,
)
return 0
}
}
} catch (err) {
throw new Error(`Error getting issue: ${err}`)
}
}
if (opts.removeLabels?.length) {
// removing a label fails if the label isn't already applied
let appliedLabels = []
try {
const { data } = await octokit.issues.get(issueOpts)
appliedLabels = data.labels.map((l) => (typeof l === 'string' ? l : l.name))
} catch (err) {
throw new Error(`Error getting issue: ${err}`)
}
opts.removeLabels = opts.removeLabels?.filter((l) => appliedLabels.includes(l))
await Promise.all(
opts.removeLabels.map(async (label) => {
try {
await octokit.issues.removeLabel({
...issueOpts,
name: label,
})
} catch (err) {
throw new Error(`Error removing label: ${err}`)
}
}),
)
if (opts.removeLabels?.length) {
core.info(`Removed labels: ${opts.removeLabels.join(', ')}`)
}
}
if (opts.addLabels?.length) {
try {
await octokit.issues.addLabels({
...issueOpts,
labels: opts.addLabels,
})
core.info(`Added labels: ${opts.addLabels.join(', ')}`)
} catch (err) {
throw new Error(`Error adding label: ${err}`)
}
}
}