@@ -31,6 +31,7 @@ describe('Invalid Usage', () => {
3131 . mockReturnValueOnce ( [ ] . join ( '\n' ) ) // labels
3232 . mockReturnValueOnce ( 'MyAwesomeIssue' ) // issue_number
3333 . mockReturnValueOnce ( 'issue-ops/invalid-repo' ) // repository
34+ . mockReturnValueOnce ( '' ) // api_url
3435 } )
3536
3637 afterEach ( ( ) => {
@@ -45,6 +46,7 @@ describe('Invalid Usage', () => {
4546 . mockReturnValueOnce ( [ ] . join ( '\n' ) ) // labels
4647 . mockReturnValueOnce ( 'MyAwesomeIssue' ) // issue_number
4748 . mockReturnValueOnce ( 'issue-ops/invalid-repo' ) // repository
49+ . mockReturnValueOnce ( '' ) // api_url
4850
4951 await main . run ( )
5052
@@ -53,6 +55,104 @@ describe('Invalid Usage', () => {
5355 } )
5456} )
5557
58+ describe ( 'Input Validation' , ( ) => {
59+ afterEach ( ( ) => {
60+ jest . resetAllMocks ( )
61+ } )
62+
63+ it ( 'Uses default API URL when api_url is empty' , async ( ) => {
64+ // Mock the environment variable
65+ const originalGitHubApiUrl = process . env . GITHUB_API_URL
66+ process . env . GITHUB_API_URL = 'https://api.github.com'
67+
68+ core . getInput
69+ . mockReturnValueOnce ( 'add' ) // action
70+ . mockReturnValueOnce ( 'false' ) // create
71+ . mockReturnValueOnce ( 'token' ) // github_token
72+ . mockReturnValueOnce ( [ 'test' ] . join ( '\n' ) ) // labels
73+ . mockReturnValueOnce ( '1' ) // issue_number
74+ . mockReturnValueOnce ( 'issue-ops/labeler' ) // repository
75+ . mockReturnValueOnce ( '' ) // api_url (empty)
76+
77+ await main . run ( )
78+
79+ expect ( core . getInput ) . toHaveBeenCalledWith ( 'api_url' , { required : false } )
80+ expect ( core . info ) . toHaveBeenCalledWith (
81+ ' - API URL: https://api.github.com'
82+ )
83+
84+ // Restore the original environment variable
85+ if ( originalGitHubApiUrl !== undefined ) {
86+ process . env . GITHUB_API_URL = originalGitHubApiUrl
87+ } else {
88+ delete process . env . GITHUB_API_URL
89+ }
90+ } )
91+
92+ it ( 'Uses GITHUB_API_URL environment variable when api_url is empty' , async ( ) => {
93+ // Mock the environment variable to a different value
94+ const originalGitHubApiUrl = process . env . GITHUB_API_URL
95+ process . env . GITHUB_API_URL = 'https://github.enterprise.internal/api/v3'
96+
97+ core . getInput
98+ . mockReturnValueOnce ( 'add' ) // action
99+ . mockReturnValueOnce ( 'false' ) // create
100+ . mockReturnValueOnce ( 'token' ) // github_token
101+ . mockReturnValueOnce ( [ 'test' ] . join ( '\n' ) ) // labels
102+ . mockReturnValueOnce ( '1' ) // issue_number
103+ . mockReturnValueOnce ( 'issue-ops/labeler' ) // repository
104+ . mockReturnValueOnce ( '' ) // api_url (empty)
105+
106+ await main . run ( )
107+
108+ expect ( core . getInput ) . toHaveBeenCalledWith ( 'api_url' , { required : false } )
109+ expect ( core . info ) . toHaveBeenCalledWith (
110+ ' - API URL: https://github.enterprise.internal/api/v3'
111+ )
112+
113+ // Restore the original environment variable
114+ if ( originalGitHubApiUrl !== undefined ) {
115+ process . env . GITHUB_API_URL = originalGitHubApiUrl
116+ } else {
117+ delete process . env . GITHUB_API_URL
118+ }
119+ } )
120+
121+ it ( 'Uses custom API URL when provided' , async ( ) => {
122+ const customApiUrl = 'https://github.enterprise.com/api/v3'
123+
124+ core . getInput
125+ . mockReturnValueOnce ( 'add' ) // action
126+ . mockReturnValueOnce ( 'false' ) // create
127+ . mockReturnValueOnce ( 'token' ) // github_token
128+ . mockReturnValueOnce ( [ 'test' ] . join ( '\n' ) ) // labels
129+ . mockReturnValueOnce ( '1' ) // issue_number
130+ . mockReturnValueOnce ( 'issue-ops/labeler' ) // repository
131+ . mockReturnValueOnce ( customApiUrl ) // api_url
132+
133+ await main . run ( )
134+
135+ expect ( core . getInput ) . toHaveBeenCalledWith ( 'api_url' , { required : false } )
136+ expect ( core . info ) . toHaveBeenCalledWith ( ` - API URL: ${ customApiUrl } ` )
137+ } )
138+
139+ it ( 'Handles invalid issue number' , async ( ) => {
140+ core . getInput
141+ . mockReturnValueOnce ( 'add' ) // action
142+ . mockReturnValueOnce ( 'false' ) // create
143+ . mockReturnValueOnce ( 'token' ) // github_token
144+ . mockReturnValueOnce ( [ 'test' ] . join ( '\n' ) ) // labels
145+ . mockReturnValueOnce ( 'not-a-number' ) // issue_number
146+ . mockReturnValueOnce ( 'issue-ops/labeler' ) // repository
147+ . mockReturnValueOnce ( '' ) // api_url
148+
149+ await main . run ( )
150+
151+ // The parseInt will result in NaN, which should still be handled
152+ expect ( core . info ) . toHaveBeenCalledWith ( ' - Issue Number: NaN' )
153+ } )
154+ } )
155+
56156describe ( 'Add Labels' , ( ) => {
57157 beforeEach ( ( ) => {
58158 // Set the action's inputs as return values from core.getInput()
@@ -63,6 +163,7 @@ describe('Add Labels', () => {
63163 . mockReturnValueOnce ( [ 'bug' , 'enhancement' ] . join ( '\n' ) ) // labels
64164 . mockReturnValueOnce ( '1' ) // issue_number
65165 . mockReturnValueOnce ( 'issue-ops/labeler' ) // repository
166+ . mockReturnValueOnce ( '' ) // api_url
66167 } )
67168
68169 afterEach ( ( ) => {
@@ -88,6 +189,29 @@ describe('Add Labels', () => {
88189 expect ( core . getInput ) . toHaveReturnedWith ( '1' )
89190 expect ( core . getInput ) . toHaveBeenCalledWith ( 'repository' , { required : true } )
90191 expect ( core . getInput ) . toHaveReturnedWith ( 'issue-ops/labeler' )
192+ expect ( core . getInput ) . toHaveBeenCalledWith ( 'api_url' , { required : false } )
193+ expect ( core . getInput ) . toHaveReturnedWith ( '' )
194+ } )
195+
196+ it ( 'Uses custom API URL when provided' , async ( ) => {
197+ // Reset mocks and set up new inputs
198+ jest . resetAllMocks ( )
199+
200+ core . getInput
201+ . mockReturnValueOnce ( 'add' ) // action
202+ . mockReturnValueOnce ( 'true' ) // create
203+ . mockReturnValueOnce ( 'token' ) // github_token
204+ . mockReturnValueOnce ( [ 'bug' ] . join ( '\n' ) ) // labels
205+ . mockReturnValueOnce ( '1' ) // issue_number
206+ . mockReturnValueOnce ( 'issue-ops/labeler' ) // repository
207+ . mockReturnValueOnce ( 'https://github.enterprise.com/api/v3' ) // api_url
208+
209+ await main . run ( )
210+
211+ expect ( core . getInput ) . toHaveBeenCalledWith ( 'api_url' , { required : false } )
212+ expect ( core . info ) . toHaveBeenCalledWith (
213+ ' - API URL: https://github.enterprise.com/api/v3'
214+ )
91215 } )
92216
93217 it ( 'Fails on GitHub API error' , async ( ) => {
@@ -130,6 +254,35 @@ describe('Add Labels', () => {
130254 repo : 'labeler'
131255 } )
132256 } )
257+
258+ it ( 'Does not create labels when create is false' , async ( ) => {
259+ // Reset mocks and set up new inputs
260+ jest . resetAllMocks ( )
261+
262+ core . getInput
263+ . mockReturnValueOnce ( 'add' ) // action
264+ . mockReturnValueOnce ( 'false' ) // create
265+ . mockReturnValueOnce ( 'token' ) // github_token
266+ . mockReturnValueOnce ( [ 'nonexistent-label' ] . join ( '\n' ) ) // labels
267+ . mockReturnValueOnce ( '1' ) // issue_number
268+ . mockReturnValueOnce ( 'issue-ops/labeler' ) // repository
269+ . mockReturnValueOnce ( '' ) // api_url
270+
271+ mocktokit . rest . issues . getLabel . mockRejectedValue ( {
272+ status : 404 ,
273+ message : 'Not found'
274+ } )
275+
276+ await main . run ( )
277+
278+ expect ( mocktokit . rest . issues . createLabel ) . not . toHaveBeenCalled ( )
279+ expect ( mocktokit . rest . issues . addLabels ) . toHaveBeenCalledWith ( {
280+ issue_number : 1 ,
281+ labels : [ 'nonexistent-label' ] ,
282+ owner : 'issue-ops' ,
283+ repo : 'labeler'
284+ } )
285+ } )
133286} )
134287
135288describe ( 'Remove Labels' , ( ) => {
@@ -142,6 +295,7 @@ describe('Remove Labels', () => {
142295 . mockReturnValueOnce ( [ 'bug' , 'enhancement' ] . join ( '\n' ) ) // labels
143296 . mockReturnValueOnce ( '1' ) // issue_number
144297 . mockReturnValueOnce ( 'issue-ops/labeler' ) // repository
298+ . mockReturnValueOnce ( '' ) // api_url
145299 } )
146300
147301 afterEach ( ( ) => {
0 commit comments