|
| 1 | +import { getApiClient } from "../api-client"; |
| 2 | +import { Logger } from "../logging"; |
| 3 | +import { RepositoryNwo } from "../repository"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Enumerates repository property names that have some meaning to us. |
| 7 | + */ |
| 8 | +export enum RepositoryPropertyName {} |
| 9 | + |
| 10 | +/** |
| 11 | + * A repository property has a name and a value. |
| 12 | + */ |
| 13 | +export interface RepositoryProperty { |
| 14 | + property_name: string; |
| 15 | + value: string; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * The API returns a list of `RepositoryProperty` objects. |
| 20 | + */ |
| 21 | +type GitHubPropertiesResponse = RepositoryProperty[]; |
| 22 | + |
| 23 | +/** |
| 24 | + * A partial mapping from `RepositoryPropertyName` to values. |
| 25 | + */ |
| 26 | +export type RepositoryProperties = Partial< |
| 27 | + Record<RepositoryPropertyName, string> |
| 28 | +>; |
| 29 | + |
| 30 | +/** |
| 31 | + * Retrieves all known repository properties from the API. |
| 32 | + * |
| 33 | + * @param logger The logger to use. |
| 34 | + * @param repositoryNwo Information about the repository for which to load properties. |
| 35 | + * @returns Returns a partial mapping from `RepositoryPropertyName` to values. |
| 36 | + */ |
| 37 | +export async function loadPropertiesFromApi( |
| 38 | + logger: Logger, |
| 39 | + repositoryNwo: RepositoryNwo, |
| 40 | +): Promise<RepositoryProperties> { |
| 41 | + try { |
| 42 | + const response = await getApiClient().request( |
| 43 | + "GET /repos/:owner/:repo/properties/values", |
| 44 | + { |
| 45 | + owner: repositoryNwo.owner, |
| 46 | + repo: repositoryNwo.repo, |
| 47 | + }, |
| 48 | + ); |
| 49 | + const remoteProperties = response.data as GitHubPropertiesResponse; |
| 50 | + |
| 51 | + if (!Array.isArray(remoteProperties)) { |
| 52 | + throw new Error( |
| 53 | + `Expected repository properties API to return an array, but got: ${JSON.stringify(response.data)}`, |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + const knownProperties = new Set(Object.keys(RepositoryPropertyName)); |
| 58 | + const properties: RepositoryProperties = {}; |
| 59 | + for (const property of remoteProperties) { |
| 60 | + if (property.property_name === undefined) { |
| 61 | + throw new Error( |
| 62 | + `Expected property object to have a 'property_name', but got: ${JSON.stringify(property)}`, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if (knownProperties.has(property.property_name)) { |
| 67 | + properties[property.property_name] = property.value; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + logger.debug("Loaded the following values for the repository properties:"); |
| 72 | + for (const [property, value] of Object.entries(properties).sort( |
| 73 | + ([nameA], [nameB]) => nameA.localeCompare(nameB), |
| 74 | + )) { |
| 75 | + logger.debug(` ${property}: ${value}`); |
| 76 | + } |
| 77 | + |
| 78 | + return properties; |
| 79 | + } catch (e) { |
| 80 | + throw new Error( |
| 81 | + `Encountered an error while trying to determine repository properties: ${e}`, |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments