-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathrepository.ts
More file actions
48 lines (44 loc) · 1.35 KB
/
repository.ts
File metadata and controls
48 lines (44 loc) · 1.35 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
import { ConfigurationError, getRequiredEnvParam } from "./util";
// A repository name with owner, parsed into its two parts
export interface RepositoryNwo {
owner: string;
repo: string;
}
/**
* Get the repository name with owner from the environment variable
* `GITHUB_REPOSITORY`.
*
* @returns The repository name with owner.
*/
export function getRepositoryNwo(): RepositoryNwo {
return getRepositoryNwoFromEnv("GITHUB_REPOSITORY");
}
/**
* Get the repository name with owner from the first environment variable that
* is set and non-empty.
*
* @param envVarNames The names of the environment variables to check.
* @returns The repository name with owner.
* @throws ConfigurationError if none of the environment variables are set.
*/
export function getRepositoryNwoFromEnv(
...envVarNames: string[]
): RepositoryNwo {
const envVarName = envVarNames.find((name) => process.env[name]);
if (!envVarName) {
throw new ConfigurationError(
`None of the env vars ${envVarNames.join(", ")} are set`,
);
}
return parseRepositoryNwo(getRequiredEnvParam(envVarName));
}
export function parseRepositoryNwo(input: string): RepositoryNwo {
const parts = input.split("/");
if (parts.length !== 2) {
throw new ConfigurationError(`"${input}" is not a valid repository name`);
}
return {
owner: parts[0],
repo: parts[1],
};
}