-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathproperties.ts
More file actions
94 lines (82 loc) · 2.84 KB
/
properties.ts
File metadata and controls
94 lines (82 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { getRepositoryProperties } from "../api-client";
import { Logger } from "../logging";
import { RepositoryNwo } from "../repository";
import { GitHubVariant, GitHubVersion } from "../util";
/**
* Enumerates repository property names that have some meaning to us.
*/
export enum RepositoryPropertyName {
EXTRA_QUERIES = "github-codeql-extra-queries",
}
/**
* A repository property has a name and a value.
*/
export interface RepositoryProperty {
property_name: string;
value: string;
}
/**
* The API returns a list of `RepositoryProperty` objects.
*/
type GitHubPropertiesResponse = RepositoryProperty[];
/**
* A partial mapping from `RepositoryPropertyName` to values.
*/
export type RepositoryProperties = Partial<
Record<RepositoryPropertyName, string>
>;
/**
* Retrieves all known repository properties from the API.
*
* @param logger The logger to use.
* @param repositoryNwo Information about the repository for which to load properties.
* @returns Returns a partial mapping from `RepositoryPropertyName` to values.
*/
export async function loadPropertiesFromApi(
gitHubVersion: GitHubVersion,
logger: Logger,
repositoryNwo: RepositoryNwo,
): Promise<RepositoryProperties> {
// TODO: To be safe for now; later we should replace this with a version check once we know
// which version of GHES we expect this to be supported by.
if (gitHubVersion.type === GitHubVariant.GHES) {
return {};
}
try {
const response = await getRepositoryProperties(repositoryNwo);
const remoteProperties = response.data as GitHubPropertiesResponse;
if (!Array.isArray(remoteProperties)) {
throw new Error(
`Expected repository properties API to return an array, but got: ${JSON.stringify(response.data)}`,
);
}
logger.debug(
`Retrieved ${remoteProperties.length} repository properties: ${remoteProperties.map((p) => p.property_name).join(", ")}`,
);
const knownProperties = new Set(Object.values(RepositoryPropertyName));
const properties: RepositoryProperties = {};
for (const property of remoteProperties) {
if (property.property_name === undefined) {
throw new Error(
`Expected property object to have a 'property_name', but got: ${JSON.stringify(property)}`,
);
}
if (
knownProperties.has(property.property_name as RepositoryPropertyName)
) {
properties[property.property_name] = property.value;
}
}
logger.debug("Loaded the following values for the repository properties:");
for (const [property, value] of Object.entries(properties).sort(
([nameA], [nameB]) => nameA.localeCompare(nameB),
)) {
logger.debug(` ${property}: ${value}`);
}
return properties;
} catch (e) {
throw new Error(
`Encountered an error while trying to determine repository properties: ${e}`,
);
}
}