get visibility when grabbing repo information

This commit is contained in:
Conor Sloan
2024-04-15 12:03:02 +01:00
parent 96609b599a
commit 6dc0f68595
8 changed files with 125 additions and 12 deletions
+12 -2
View File
@@ -18,10 +18,20 @@ afterEach(() => {
describe('getRepositoryMetadata', () => {
it('returns repository metadata when the fetch response is ok', async () => {
fetchMock.mockResolvedValueOnce(
new Response(JSON.stringify({ id: '123', owner: { id: '456' } }))
new Response(
JSON.stringify({
id: '123',
owner: { id: '456' },
visibility: 'public'
})
)
)
const result = await getRepositoryMetadata(url, 'repository', 'token')
expect(result).toEqual({ repoId: '123', ownerId: '456' })
expect(result).toEqual({
repoId: '123',
ownerId: '456',
visibility: 'public'
})
})
it('throws an error when the fetch errors', async () => {
+41 -2
View File
@@ -4,6 +4,7 @@ import * as cfg from '../src/config'
import * as apiClient from '../src/api-client'
let getContainerRegistryURLMock: jest.SpyInstance
let getRepositoryMetadataMock: jest.SpyInstance
let getInputMock: jest.SpyInstance
const ghcrUrl = new URL('https://ghcr.io')
@@ -14,6 +15,10 @@ describe('config.resolvePublishActionOptions', () => {
.spyOn(apiClient, 'getContainerRegistryURL')
.mockImplementation()
getRepositoryMetadataMock = jest
.spyOn(apiClient, 'getRepositoryMetadata')
.mockImplementation()
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
configureEventContext()
@@ -133,6 +138,30 @@ describe('config.resolvePublishActionOptions', () => {
)
})
it('throws an error when getting the repository metadata fails', async () => {
getInputMock.mockReturnValueOnce('token')
getContainerRegistryURLMock.mockResolvedValue(ghcrUrl)
getRepositoryMetadataMock.mockRejectedValue(
new Error('Failed to get repository metadata')
)
await expect(cfg.resolvePublishActionOptions()).rejects.toThrow(
'Failed to get repository metadata'
)
})
it('throws an error when returned repository visibility is empty', async () => {
getInputMock.mockReturnValueOnce('token')
getContainerRegistryURLMock.mockResolvedValue(ghcrUrl)
getRepositoryMetadataMock.mockResolvedValue({
visibility: ''
})
await expect(cfg.resolvePublishActionOptions()).rejects.toThrow(
'Could not find repository visibility.'
)
})
it('returns options when all values are present', async () => {
getInputMock.mockImplementation((name: string) => {
expect(name).toBe('github-token')
@@ -140,6 +169,10 @@ describe('config.resolvePublishActionOptions', () => {
})
getContainerRegistryURLMock.mockResolvedValue(ghcrUrl)
getRepositoryMetadataMock.mockResolvedValue({
visibility: 'public'
})
const options = await cfg.resolvePublishActionOptions()
expect(options).toEqual({
@@ -150,6 +183,7 @@ describe('config.resolvePublishActionOptions', () => {
apiBaseUrl: 'apiBaseUrl',
runnerTempDir: 'runnerTempDir',
sha: 'sha',
repositoryVisibility: 'public',
repositoryId: 'repositoryId',
repositoryOwnerId: 'repositoryOwnerId',
isEnterprise: false,
@@ -164,6 +198,9 @@ describe('config.resolvePublishActionOptions', () => {
return 'token'
})
getContainerRegistryURLMock.mockResolvedValue(ghcrUrl)
getRepositoryMetadataMock.mockResolvedValue({
visibility: 'public'
})
process.env.GITHUB_SERVER_URL = 'https://github-enterprise.com'
@@ -181,7 +218,8 @@ describe('config.resolvePublishActionOptions', () => {
repositoryOwnerId: 'repositoryOwnerId',
isEnterprise: true,
containerRegistryUrl: ghcrUrl,
token: 'token'
token: 'token',
repositoryVisibility: 'public'
})
})
})
@@ -200,7 +238,8 @@ describe('config.serializeOptions', () => {
repositoryOwnerId: 'repositoryOwnerId',
isEnterprise: false,
containerRegistryUrl: ghcrUrl,
token: 'token'
token: 'token',
repositoryVisibility: 'public'
}
const serialized = cfg.serializeOptions(options)
+2 -1
View File
@@ -389,6 +389,7 @@ function baseOptions(): cfg.PublishActionOptions {
isEnterprise: false,
containerRegistryUrl: ghcrUrl,
token: 'token',
ref: 'refs/tags/v1.2.3'
ref: 'refs/tags/v1.2.3',
repositoryVisibility: 'public'
}
}