send auth token to get container registry url endpoint
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
|||||||
} from '../src/api-client'
|
} from '../src/api-client'
|
||||||
|
|
||||||
const url = 'https://registry.example.com'
|
const url = 'https://registry.example.com'
|
||||||
|
const test_token = 'test_token'
|
||||||
|
|
||||||
let fetchMock: jest.SpyInstance
|
let fetchMock: jest.SpyInstance
|
||||||
|
|
||||||
@@ -26,12 +27,23 @@ describe('getRepositoryMetadata', () => {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
const result = await getRepositoryMetadata(url, 'repository', 'token')
|
const result = await getRepositoryMetadata(url, 'repository', test_token)
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
repoId: '123',
|
repoId: '123',
|
||||||
ownerId: '456',
|
ownerId: '456',
|
||||||
visibility: 'public'
|
visibility: 'public'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
expect(fetchMock).toHaveBeenCalledWith(
|
||||||
|
'https://registry.example.com/repos/repository',
|
||||||
|
{
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${test_token}`,
|
||||||
|
Accept: 'application/vnd.github.v3+json'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws an error when the fetch errors', async () => {
|
it('throws an error when the fetch errors', async () => {
|
||||||
@@ -67,18 +79,32 @@ describe('getContainerRegistryURL', () => {
|
|||||||
fetchMock.mockResolvedValueOnce(
|
fetchMock.mockResolvedValueOnce(
|
||||||
new Response(JSON.stringify({ url: 'https://registry.example.com' }))
|
new Response(JSON.stringify({ url: 'https://registry.example.com' }))
|
||||||
)
|
)
|
||||||
const result = await getContainerRegistryURL(url)
|
const result = await getContainerRegistryURL(url, test_token)
|
||||||
|
|
||||||
expect(result).toEqual(new URL('https://registry.example.com'))
|
expect(result).toEqual(new URL('https://registry.example.com'))
|
||||||
|
|
||||||
|
expect(fetchMock).toHaveBeenCalledWith(
|
||||||
|
'https://registry.example.com/packages/container-registry-url',
|
||||||
|
{
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${test_token}`,
|
||||||
|
Accept: 'application/vnd.github.v3+json'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws an error when the fetch errors', async () => {
|
it('throws an error when the fetch errors', async () => {
|
||||||
fetchMock.mockRejectedValueOnce(new Error('API is down'))
|
fetchMock.mockRejectedValueOnce(new Error('API is down'))
|
||||||
await expect(getContainerRegistryURL(url)).rejects.toThrow('API is down')
|
await expect(getContainerRegistryURL(url, test_token)).rejects.toThrow(
|
||||||
|
'API is down'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws an error when the response status is not ok', async () => {
|
it('throws an error when the response status is not ok', async () => {
|
||||||
fetchMock.mockResolvedValueOnce(new Response(null, { status: 500 }))
|
fetchMock.mockResolvedValueOnce(new Response(null, { status: 500 }))
|
||||||
await expect(getContainerRegistryURL(url)).rejects.toThrow(
|
await expect(getContainerRegistryURL(url, test_token)).rejects.toThrow(
|
||||||
'Failed to fetch container registry url due to bad status code: 500'
|
'Failed to fetch container registry url due to bad status code: 500'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -87,7 +113,7 @@ describe('getContainerRegistryURL', () => {
|
|||||||
fetchMock.mockResolvedValueOnce(
|
fetchMock.mockResolvedValueOnce(
|
||||||
new Response(JSON.stringify({ wrong: 'format' }))
|
new Response(JSON.stringify({ wrong: 'format' }))
|
||||||
)
|
)
|
||||||
await expect(getContainerRegistryURL(url)).rejects.toThrow(
|
await expect(getContainerRegistryURL(url, test_token)).rejects.toThrow(
|
||||||
'Failed to fetch repository metadata: unexpected response format'
|
'Failed to fetch repository metadata: unexpected response format'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
+9
-3
@@ -104365,8 +104365,14 @@ async function getRepositoryMetadata(githubAPIURL, repository, token) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
exports.getRepositoryMetadata = getRepositoryMetadata;
|
exports.getRepositoryMetadata = getRepositoryMetadata;
|
||||||
async function getContainerRegistryURL(githubAPIURL) {
|
async function getContainerRegistryURL(githubAPIURL, token) {
|
||||||
const response = await fetch(`${githubAPIURL}/packages/container-registry-url`);
|
const response = await fetch(`${githubAPIURL}/packages/container-registry-url`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
Accept: 'application/vnd.github.v3+json'
|
||||||
|
}
|
||||||
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Failed to fetch container registry url due to bad status code: ${response.status}`);
|
throw new Error(`Failed to fetch container registry url due to bad status code: ${response.status}`);
|
||||||
}
|
}
|
||||||
@@ -104464,7 +104470,7 @@ async function resolvePublishActionOptions() {
|
|||||||
throw new Error(`Could not find GITHUB_REPOSITORY_OWNER_ID.`);
|
throw new Error(`Could not find GITHUB_REPOSITORY_OWNER_ID.`);
|
||||||
}
|
}
|
||||||
// Required Values fetched from the GitHub API
|
// Required Values fetched from the GitHub API
|
||||||
const containerRegistryUrl = await apiClient.getContainerRegistryURL(apiBaseUrl);
|
const containerRegistryUrl = await apiClient.getContainerRegistryURL(apiBaseUrl, token);
|
||||||
const isEnterprise = !githubServerUrl.includes('https://github.com') &&
|
const isEnterprise = !githubServerUrl.includes('https://github.com') &&
|
||||||
!githubServerUrl.endsWith('.ghe.com');
|
!githubServerUrl.endsWith('.ghe.com');
|
||||||
const repoMetadata = await apiClient.getRepositoryMetadata(apiBaseUrl, nameWithOwner, token);
|
const repoMetadata = await apiClient.getRepositoryMetadata(apiBaseUrl, nameWithOwner, token);
|
||||||
|
|||||||
+10
-2
@@ -34,10 +34,18 @@ export async function getRepositoryMetadata(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getContainerRegistryURL(
|
export async function getContainerRegistryURL(
|
||||||
githubAPIURL: string
|
githubAPIURL: string,
|
||||||
|
token: string
|
||||||
): Promise<URL> {
|
): Promise<URL> {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${githubAPIURL}/packages/container-registry-url`
|
`${githubAPIURL}/packages/container-registry-url`,
|
||||||
|
{
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
Accept: 'application/vnd.github.v3+json'
|
||||||
|
}
|
||||||
|
}
|
||||||
)
|
)
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|||||||
+4
-2
@@ -92,8 +92,10 @@ export async function resolvePublishActionOptions(): Promise<PublishActionOption
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Required Values fetched from the GitHub API
|
// Required Values fetched from the GitHub API
|
||||||
const containerRegistryUrl: URL =
|
const containerRegistryUrl: URL = await apiClient.getContainerRegistryURL(
|
||||||
await apiClient.getContainerRegistryURL(apiBaseUrl)
|
apiBaseUrl,
|
||||||
|
token
|
||||||
|
)
|
||||||
|
|
||||||
const isEnterprise =
|
const isEnterprise =
|
||||||
!githubServerUrl.includes('https://github.com') &&
|
!githubServerUrl.includes('https://github.com') &&
|
||||||
|
|||||||
Reference in New Issue
Block a user