Skip to content

Commit 4c0961e

Browse files
committed
Add tests for GitHub License API fallback
1 parent d1e9a12 commit 4c0961e

6 files changed

Lines changed: 98 additions & 20 deletions

File tree

__tests__/licenses.test.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expect, test} from '@jest/globals'
1+
import {expect, jest, test} from '@jest/globals'
22
import {Change, Changes} from '../src/schemas'
33
import {getDeniedLicenseChanges} from '../src/licenses'
44

@@ -48,6 +48,28 @@ let rubyChange: Change = {
4848
]
4949
}
5050

51+
jest.mock('@actions/core')
52+
53+
const mockOctokit = {
54+
rest: {
55+
licenses: {
56+
getForRepo: jest
57+
.fn()
58+
.mockReturnValue({data: {license: {spdx_id: 'AGPL'}}})
59+
}
60+
}
61+
}
62+
63+
jest.mock('octokit', () => {
64+
return {
65+
Octokit: class {
66+
constructor() {
67+
return mockOctokit
68+
}
69+
}
70+
}
71+
})
72+
5173
test('it fails if a license outside the allow list is found', async () => {
5274
const changes: Changes = [npmChange, rubyChange]
5375
const [invalidChanges, _] = await getDeniedLicenseChanges(changes, {
@@ -108,3 +130,46 @@ test('it fails if a license outside the allow list is found in both of added and
108130
})
109131
expect(invalidChanges).toStrictEqual([npmChange])
110132
})
133+
134+
describe('GH License API fallback', () => {
135+
beforeEach(() => {
136+
jest.clearAllMocks()
137+
})
138+
139+
test('it calls licenses endpoint if atleast one of the changes has null license and valid source_repository_url', async () => {
140+
const nullLicenseChange = {
141+
...npmChange,
142+
license: null,
143+
source_repository_url: 'http://github.com/some-owner/some-repo'
144+
}
145+
const [_, unknownChanges] = await getDeniedLicenseChanges(
146+
[nullLicenseChange, rubyChange],
147+
{}
148+
)
149+
150+
expect(mockOctokit.rest.licenses.getForRepo).toHaveBeenNthCalledWith(1, {
151+
owner: 'some-owner',
152+
repo: 'some-repo'
153+
})
154+
expect(unknownChanges.length).toEqual(0)
155+
})
156+
157+
test('it does not call licenses API endpoint for change with null license and invalid source_repository_url ', async () => {
158+
const [_, unknownChanges] = await getDeniedLicenseChanges(
159+
[{...npmChange, license: null}],
160+
{}
161+
)
162+
expect(mockOctokit.rest.licenses.getForRepo).not.toHaveBeenCalled()
163+
expect(unknownChanges.length).toEqual(1)
164+
})
165+
166+
test('it does not call licenses API endpoint if licenses for all changes are present', async () => {
167+
const [_, unknownChanges] = await getDeniedLicenseChanges(
168+
[npmChange, rubyChange],
169+
{}
170+
)
171+
172+
expect(mockOctokit.rest.licenses.getForRepo).not.toHaveBeenCalled()
173+
expect(unknownChanges.length).toEqual(0)
174+
})
175+
})

dist/index.js

Lines changed: 8 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"zod": "^3.19.1"
3838
},
3939
"devDependencies": {
40+
"@types/jest": "^27.5.2",
4041
"@types/node": "^16.11.65",
4142
"@typescript-eslint/eslint-plugin": "^5.40.0",
4243
"@typescript-eslint/parser": "^5.40.0",

src/licenses.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@ const fetchGHLicense = async (
6565
})
6666

6767
try {
68-
const response = await octokit.request(
69-
'GET /repos/{owner}/{repo}/license',
70-
{
71-
owner,
72-
repo
73-
}
74-
)
68+
const response = await octokit.rest.licenses.getForRepo({owner, repo})
7569
return response.data.license?.spdx_id ?? null
7670
} catch (_) {
7771
return null

0 commit comments

Comments
 (0)