1
1
/* See function main in this file for documentation */
2
2
3
3
import coreLib from '@actions/core'
4
-
5
- import github from '#src/workflows/github.js'
6
- import { getActionContext } from '#src/workflows/action-context.js'
7
- import { boolEnvVar } from '#src/workflows/get-env-inputs.js'
4
+ import { type Octokit } from '@octokit/rest'
5
+ import { CoreInject } from '@/links/scripts/action-injections'
6
+
7
+ import github from '#src/workflows/github.ts'
8
+ import { getActionContext } from '#src/workflows/action-context.ts'
9
+ import { boolEnvVar } from '#src/workflows/get-env-inputs.ts'
10
+
11
+ type Options = {
12
+ addLabels ?: string [ ]
13
+ removeLabels ?: string [ ]
14
+ ignoreIfAssigned ?: boolean
15
+ ignoreIfLabeled ?: boolean
16
+ issue_number ?: number
17
+ owner ?: string
18
+ repo ?: string
19
+ }
8
20
9
21
// When this file is invoked directly from action as opposed to being imported
10
22
if ( import . meta. url . endsWith ( process . argv [ 1 ] ) ) {
@@ -16,28 +28,19 @@ if (import.meta.url.endsWith(process.argv[1])) {
16
28
17
29
const octokit = github ( )
18
30
19
- const opts = {
20
- addLabels : ADD_LABELS ,
21
- removeLabels : REMOVE_LABELS ,
31
+ const opts : Options = {
22
32
ignoreIfAssigned : boolEnvVar ( 'IGNORE_IF_ASSIGNED' ) ,
23
33
ignoreIfLabeled : boolEnvVar ( 'IGNORE_IF_LABELED' ) ,
24
34
}
25
35
26
36
// labels come in comma separated from actions
27
- let addLabels
28
-
29
- if ( opts . addLabels ) {
30
- addLabels = [ ...opts . addLabels . split ( ',' ) ]
31
- opts . addLabels = addLabels . map ( ( l ) => l . trim ( ) )
37
+ if ( typeof ADD_LABELS === 'string' ) {
38
+ opts . addLabels = [ ...ADD_LABELS . split ( ',' ) ] . map ( ( l ) => l . trim ( ) )
32
39
} else {
33
40
opts . addLabels = [ ]
34
41
}
35
-
36
- let removeLabels
37
-
38
- if ( opts . removeLabels ) {
39
- removeLabels = [ ...opts . removeLabels . split ( ',' ) ]
40
- opts . removeLabels = removeLabels . map ( ( l ) => l . trim ( ) )
42
+ if ( typeof REMOVE_LABELS === 'string' ) {
43
+ opts . removeLabels = [ ...REMOVE_LABELS . split ( ',' ) ] . map ( ( l ) => l . trim ( ) )
41
44
} else {
42
45
opts . removeLabels = [ ]
43
46
}
@@ -54,7 +57,7 @@ if (import.meta.url.endsWith(process.argv[1])) {
54
57
opts . owner = owner
55
58
opts . repo = repo
56
59
57
- main ( coreLib , octokit , opts , { } )
60
+ main ( coreLib , octokit , opts )
58
61
}
59
62
60
63
/*
@@ -69,22 +72,31 @@ if (import.meta.url.endsWith(process.argv[1])) {
69
72
* ignoreIfAssigned {boolean} don't apply labels if there are assignees
70
73
* ignoreIfLabeled {boolean} don't apply labels if there are already labels added
71
74
*/
72
- export default async function main ( core , octokit , opts = { } ) {
75
+ export default async function main (
76
+ core : typeof coreLib | CoreInject ,
77
+ octokit : Octokit ,
78
+ opts : Options = { } ,
79
+ ) {
73
80
if ( opts . addLabels ?. length === 0 && opts . removeLabels ?. length === 0 ) {
74
81
core . info ( 'No labels to add or remove specified, nothing to do.' )
75
82
return
76
83
}
77
84
85
+ if ( ! opts . issue_number || ! opts . owner || ! opts . repo ) {
86
+ throw new Error ( `Missing required parameters ${ JSON . stringify ( opts ) } ` )
87
+ }
88
+ const issueOpts = {
89
+ issue_number : opts . issue_number ,
90
+ owner : opts . owner ,
91
+ repo : opts . repo ,
92
+ }
93
+
78
94
if ( opts . ignoreIfAssigned || opts . ignoreIfLabeled ) {
79
95
try {
80
- const { data } = await octokit . issues . get ( {
81
- issue_number : opts . issue_number ,
82
- owner : opts . owner ,
83
- repo : opts . repo ,
84
- } )
96
+ const { data } = await octokit . issues . get ( issueOpts )
85
97
86
98
if ( opts . ignoreIfAssigned ) {
87
- if ( data . assignees . length > 0 ) {
99
+ if ( data . assignees ? .length ) {
88
100
core . info (
89
101
`ignore-if-assigned is true: not applying labels since there's ${ data . assignees . length } assignees` ,
90
102
)
@@ -105,31 +117,24 @@ export default async function main(core, octokit, opts = {}) {
105
117
}
106
118
}
107
119
108
- if ( opts . removeLabels ?. length > 0 ) {
120
+ if ( opts . removeLabels ?. length ) {
109
121
// removing a label fails if the label isn't already applied
110
122
let appliedLabels = [ ]
111
123
112
124
try {
113
- const { data } = await octokit . issues . get ( {
114
- issue_number : opts . issue_number ,
115
- owner : opts . owner ,
116
- repo : opts . repo ,
117
- } )
118
-
119
- appliedLabels = data . labels . map ( ( l ) => l . name )
125
+ const { data } = await octokit . issues . get ( issueOpts )
126
+ appliedLabels = data . labels . map ( ( l ) => ( typeof l === 'string' ? l : l . name ) )
120
127
} catch ( err ) {
121
128
throw new Error ( `Error getting issue: ${ err } ` )
122
129
}
123
130
124
- opts . removeLabels = opts . removeLabels . filter ( ( l ) => appliedLabels . includes ( l ) )
131
+ opts . removeLabels = opts . removeLabels ? .filter ( ( l ) => appliedLabels . includes ( l ) )
125
132
126
133
await Promise . all (
127
134
opts . removeLabels . map ( async ( label ) => {
128
135
try {
129
136
await octokit . issues . removeLabel ( {
130
- issue_number : opts . issue_number ,
131
- owner : opts . owner ,
132
- repo : opts . repo ,
137
+ ...issueOpts ,
133
138
name : label ,
134
139
} )
135
140
} catch ( err ) {
@@ -138,17 +143,15 @@ export default async function main(core, octokit, opts = {}) {
138
143
} ) ,
139
144
)
140
145
141
- if ( opts . removeLabels . length > 0 ) {
146
+ if ( opts . removeLabels ? .length ) {
142
147
core . info ( `Removed labels: ${ opts . removeLabels . join ( ', ' ) } ` )
143
148
}
144
149
}
145
150
146
- if ( opts . addLabels ?. length > 0 ) {
151
+ if ( opts . addLabels ?. length ) {
147
152
try {
148
153
await octokit . issues . addLabels ( {
149
- issue_number : opts . issue_number ,
150
- owner : opts . owner ,
151
- repo : opts . repo ,
154
+ ...issueOpts ,
152
155
labels : opts . addLabels ,
153
156
} )
154
157
0 commit comments