Files
publish-immutable-action/src/api-client.ts
T

54 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-01-29 20:31:00 +00:00
export async function getRepositoryMetadata(
repository: string,
token: string
): Promise<{ repoId: string; ownerId: string }> {
const response = await fetch(
2024-01-29 21:05:30 -05:00
`${process.env.GITHUB_API_URL}/repos/${repository}`,
{
2024-01-29 18:35:12 -05:00
method: 'GET',
headers: {
2024-01-29 21:05:30 -05:00
Authorization: `Bearer ${token}`,
Accept: 'application/vnd.github.v3+json'
2024-01-29 18:35:12 -05:00
}
2024-01-29 21:05:30 -05:00
}
)
2024-01-29 20:31:00 +00:00
if (!response.ok) {
throw new Error(
`Failed to fetch repository metadata due to bad status code: ${response.status}`
)
}
const data = await response.json()
// Check that the response contains the expected data
if (!data.id || !data.owner.id) {
throw new Error(
`Failed to fetch repository metadata: unexpected response format`
)
}
return { repoId: String(data.id), ownerId: String(data.owner.id) }
2024-01-29 20:31:00 +00:00
}
export async function getContainerRegistryURL(): Promise<URL> {
const response = await fetch(
`${process.env.GITHUB_API_URL}/packages/container-registry-url`
)
if (!response.ok) {
throw new Error(
`Failed to fetch container registry url due to bad status code: ${response.status}`
)
}
const data = await response.json()
if (!data.url) {
throw new Error(
`Failed to fetch repository metadata: unexpected response format`
)
}
const registryURL: URL = new URL(data.url)
return registryURL
}