11/* See function main in this file for documentation */
22
33import 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+ }
820
921// When this file is invoked directly from action as opposed to being imported
1022if ( import . meta. url . endsWith ( process . argv [ 1 ] ) ) {
@@ -16,28 +28,19 @@ if (import.meta.url.endsWith(process.argv[1])) {
1628
1729 const octokit = github ( )
1830
19- const opts = {
20- addLabels : ADD_LABELS ,
21- removeLabels : REMOVE_LABELS ,
31+ const opts : Options = {
2232 ignoreIfAssigned : boolEnvVar ( 'IGNORE_IF_ASSIGNED' ) ,
2333 ignoreIfLabeled : boolEnvVar ( 'IGNORE_IF_LABELED' ) ,
2434 }
2535
2636 // 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 ( ) )
3239 } else {
3340 opts . addLabels = [ ]
3441 }
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 ( ) )
4144 } else {
4245 opts . removeLabels = [ ]
4346 }
@@ -54,7 +57,7 @@ if (import.meta.url.endsWith(process.argv[1])) {
5457 opts . owner = owner
5558 opts . repo = repo
5659
57- main ( coreLib , octokit , opts , { } )
60+ main ( coreLib , octokit , opts )
5861}
5962
6063/*
@@ -69,22 +72,31 @@ if (import.meta.url.endsWith(process.argv[1])) {
6972 * ignoreIfAssigned {boolean} don't apply labels if there are assignees
7073 * ignoreIfLabeled {boolean} don't apply labels if there are already labels added
7174 */
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+ ) {
7380 if ( opts . addLabels ?. length === 0 && opts . removeLabels ?. length === 0 ) {
7481 core . info ( 'No labels to add or remove specified, nothing to do.' )
7582 return
7683 }
7784
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+
7894 if ( opts . ignoreIfAssigned || opts . ignoreIfLabeled ) {
7995 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 )
8597
8698 if ( opts . ignoreIfAssigned ) {
87- if ( data . assignees . length > 0 ) {
99+ if ( data . assignees ? .length ) {
88100 core . info (
89101 `ignore-if-assigned is true: not applying labels since there's ${ data . assignees . length } assignees` ,
90102 )
@@ -105,31 +117,24 @@ export default async function main(core, octokit, opts = {}) {
105117 }
106118 }
107119
108- if ( opts . removeLabels ?. length > 0 ) {
120+ if ( opts . removeLabels ?. length ) {
109121 // removing a label fails if the label isn't already applied
110122 let appliedLabels = [ ]
111123
112124 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 ) )
120127 } catch ( err ) {
121128 throw new Error ( `Error getting issue: ${ err } ` )
122129 }
123130
124- opts . removeLabels = opts . removeLabels . filter ( ( l ) => appliedLabels . includes ( l ) )
131+ opts . removeLabels = opts . removeLabels ? .filter ( ( l ) => appliedLabels . includes ( l ) )
125132
126133 await Promise . all (
127134 opts . removeLabels . map ( async ( label ) => {
128135 try {
129136 await octokit . issues . removeLabel ( {
130- issue_number : opts . issue_number ,
131- owner : opts . owner ,
132- repo : opts . repo ,
137+ ...issueOpts ,
133138 name : label ,
134139 } )
135140 } catch ( err ) {
@@ -138,17 +143,15 @@ export default async function main(core, octokit, opts = {}) {
138143 } ) ,
139144 )
140145
141- if ( opts . removeLabels . length > 0 ) {
146+ if ( opts . removeLabels ? .length ) {
142147 core . info ( `Removed labels: ${ opts . removeLabels . join ( ', ' ) } ` )
143148 }
144149 }
145150
146- if ( opts . addLabels ?. length > 0 ) {
151+ if ( opts . addLabels ?. length ) {
147152 try {
148153 await octokit . issues . addLabels ( {
149- issue_number : opts . issue_number ,
150- owner : opts . owner ,
151- repo : opts . repo ,
154+ ...issueOpts ,
152155 labels : opts . addLabels ,
153156 } )
154157
0 commit comments