Skip to content

Commit b22f334

Browse files
committed
Add getRepositoryNwo() helper functions
1 parent 486ab5a commit b22f334

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/repository.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
1-
import { ConfigurationError } from "./util";
1+
import { ConfigurationError, getRequiredEnvParam } from "./util";
22

33
// A repository name with owner, parsed into its two parts
44
export interface RepositoryNwo {
55
owner: string;
66
repo: string;
77
}
88

9+
/**
10+
* Get the repository name with owner from the environment variable
11+
* `GITHUB_REPOSITORY`.
12+
*
13+
* @returns The repository name with owner.
14+
*/
15+
export function getRepositoryNwo(): RepositoryNwo {
16+
return getRepositoryNwoFromEnv("GITHUB_REPOSITORY");
17+
}
18+
19+
/**
20+
* Get the repository name with owner from the first environment variable that
21+
* is set and non-empty.
22+
*
23+
* @param envVarNames The names of the environment variables to check.
24+
* @returns The repository name with owner.
25+
* @throws ConfigurationError if none of the environment variables are set.
26+
*/
27+
export function getRepositoryNwoFromEnv(
28+
...envVarNames: string[]
29+
): RepositoryNwo {
30+
const envVarName = envVarNames.find((name) => process.env[name]);
31+
if (!envVarName) {
32+
throw new ConfigurationError(
33+
`None of the env vars ${envVarNames.join(", ")} are set`,
34+
);
35+
}
36+
return parseRepositoryNwo(getRequiredEnvParam(envVarName));
37+
}
38+
939
export function parseRepositoryNwo(input: string): RepositoryNwo {
1040
const parts = input.split("/");
1141
if (parts.length !== 2) {

0 commit comments

Comments
 (0)