Skip to content

Commit 3f4298f

Browse files
committed
refactor(github): changes setup for GitHub auth token
Previously, we expected a `GITHUB_AUTH_TOKEN` environment variable to be set. To make things more flexible, this commit changes to require you to make an instance of a `GitHub` class, and pass in the auth token. This allows the end user to get the token however they want and no longer ties them to specific environment variables. BREAKING CHANGE: Previously, you had to have a `GITHUB_AUTH_TOKEN` environment variable that would be used in `GitHub` class methods. The API has changed to instead create in instance of `GitHub` by passing in your auth token however works for you in your code. Previously used class method `recentlyClosedPullRequests` is now an instance method on `GitHub`.
1 parent c77ee21 commit 3f4298f

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/github.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import fetchMock from "fetch-mock";
22
import { GitHub, IResponse, ISearchResponse } from "./github";
33

4+
function createGitHub() {
5+
const authToken = process.env.GITHUB_AUTH_TOKEN;
6+
if (!authToken) {
7+
throw new Error(
8+
"Must set `GITHUB_AUTH_TOKEN` environmental variable for tests to run."
9+
);
10+
}
11+
12+
return new GitHub({ authToken });
13+
}
414
test("fetching from a busy, public organization", async () => {
5-
const response = await GitHub.recentlyClosedPullRequests({
15+
const github = createGitHub();
16+
const response = await github.recentlyClosedPullRequests({
617
endDate: new Date("5/7/19"),
718
organization: "microsoft",
819
startDate: new Date("5/1/19")
@@ -41,7 +52,8 @@ test("responses are formatted to be groups by repo, and 'cleaned up'", async ()
4152
return { data: mockResponse };
4253
});
4354

44-
const resp = await GitHub.recentlyClosedPullRequests({
55+
const github = createGitHub();
56+
const resp = await github.recentlyClosedPullRequests({
4557
endDate: new Date(),
4658
organization: "my-org",
4759
request: { fetch },
@@ -74,7 +86,8 @@ test("search string include the specified org and date range", async () => {
7486
}
7587
);
7688

77-
const resp = await GitHub.recentlyClosedPullRequests({
89+
const github = createGitHub();
90+
const resp = await github.recentlyClosedPullRequests({
7891
endDate: new Date("1/7/2019"),
7992
organization: "my-org",
8093
request: { fetch },

src/github.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,18 @@ interface IGitHubGraphqlArgs {
7373
}
7474

7575
export class GitHub {
76-
public static async recentlyClosedPullRequests(
76+
token: string;
77+
78+
constructor({ authToken }: { authToken: string }) {
79+
this.token = authToken;
80+
}
81+
82+
public async recentlyClosedPullRequests(
7783
params: IRequestParams
7884
): Promise<IPullRequestsForRepos> {
7985
let requestArgs: IGitHubGraphqlArgs = {
8086
headers: {
81-
authorization: `token ${process.env.GITHUB_AUTH_TOKEN}`
87+
authorization: `token ${this.token}`
8288
},
8389
query: closedPullRequestsQuery,
8490
searchString: this.__searchQueryString(params)
@@ -112,7 +118,7 @@ export class GitHub {
112118
}, pullRequestsGroupsByRepo);
113119
}
114120

115-
public static __searchQueryString({
121+
public __searchQueryString({
116122
organization,
117123
startDate,
118124
endDate

0 commit comments

Comments
 (0)