Skip to content

Commit cdb2530

Browse files
committed
Add functions to get the current and dotcom instance
1 parent 0b0ac95 commit cdb2530

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/github.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import test from "ava";
22

3+
import { ActionsEnvVars } from "./environment";
34
import * as github from "./github";
4-
import { setupTests } from "./testing-utils";
5+
import { getTestEnv, setupTests } from "./testing-utils";
56

67
setupTests(test);
78

@@ -67,3 +68,44 @@ test("parseGitHubUrl", (t) => {
6768
message: '"http:///::::433" is not a valid URL',
6869
});
6970
});
71+
72+
test("getCurrentInstance throws if required env vars aren't set", (t) => {
73+
t.throws(() => github.getCurrentInstance(getTestEnv()));
74+
t.throws(() => {
75+
const env = getTestEnv({
76+
[ActionsEnvVars.GITHUB_SERVER_URL]: "https://localhost",
77+
});
78+
github.getCurrentInstance(env);
79+
});
80+
t.throws(() => {
81+
const env = getTestEnv({
82+
[ActionsEnvVars.GITHUB_API_URL]: "https://api.localhost",
83+
});
84+
github.getCurrentInstance(env);
85+
});
86+
});
87+
88+
test("getCurrentInstance constructs instance from environment", (t) => {
89+
const url = "https://localhost";
90+
const apiUrl = "https://api.localhost";
91+
92+
const env = getTestEnv({
93+
[ActionsEnvVars.GITHUB_SERVER_URL]: url,
94+
[ActionsEnvVars.GITHUB_API_URL]: apiUrl,
95+
});
96+
const instance = github.getCurrentInstance(env);
97+
98+
t.is(instance.url, url);
99+
t.is(instance.apiURL, apiUrl);
100+
});
101+
102+
test("getCurrentInstance constructs dotcom instance with dotcom env vars", (t) => {
103+
const env = getTestEnv({
104+
[ActionsEnvVars.GITHUB_SERVER_URL]: github.GITHUB_DOTCOM_URL,
105+
[ActionsEnvVars.GITHUB_API_URL]: github.GITHUB_DOTCOM_API_URL,
106+
});
107+
const instance = github.getCurrentInstance(env);
108+
const dotcomInstance = github.getDotcomInstance();
109+
110+
t.deepEqual(dotcomInstance, instance);
111+
});

‎src/github.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,19 @@ export function parseGitHubUrl(inputUrl: string): string {
6060

6161
return url.toString();
6262
}
63+
64+
/** Retrieves a {@link GitHubInstance} describing the instance we are running on. */
65+
export function getCurrentInstance(env: ReadOnlyEnv): GitHubInstance {
66+
return {
67+
url: env.getRequired(ActionsEnvVars.GITHUB_SERVER_URL),
68+
apiURL: env.getRequired(ActionsEnvVars.GITHUB_API_URL),
69+
};
70+
}
71+
72+
/** Retrieves a {@link GitHubInstance} describing the GitHub.com instance. */
73+
export function getDotcomInstance(): GitHubInstance {
74+
return {
75+
url: GITHUB_DOTCOM_URL,
76+
apiURL: GITHUB_DOTCOM_API_URL,
77+
};
78+
}

0 commit comments

Comments
 (0)