-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.ts
131 lines (112 loc) · 2.77 KB
/
github.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import graphql from "@octokit/graphql";
import { format } from "date-fns";
const closedPullRequestsQuery = `
query recentPullRequests($searchString: String!) {
search(
query: $searchString
type: ISSUE
first: 100
) {
edges {
node {
... on PullRequest {
repository {
name
}
title
createdAt
closedAt
url
merged
}
}
}
}
}
`;
export interface IPullRequestsForRepos {
[repo: string]: IPullRequestInformation[];
}
interface IPullRequestInformation {
closedAt: Date;
createdAt: Date;
merged: boolean;
repository: { name: string };
title: string;
url: string;
}
interface IRequestParams {
endDate: Date;
organization: string;
startDate: Date;
request?: { fetch: () => object };
}
export interface ISearchResponse {
search: {
edges: IResponse[];
};
}
export interface IResponse {
node: {
repository: { name: string };
title: string;
createdAt: Date;
closedAt: Date;
url: string;
merged: boolean;
};
}
interface IGitHubGraphqlArgs {
headers: {
authorization: string;
};
query: string;
request?: { fetch: () => object };
searchString: string;
}
export class GitHub {
private token: string;
constructor({ authToken }: { authToken: string }) {
this.token = authToken;
}
public async recentlyClosedPullRequests(
params: IRequestParams
): Promise<IPullRequestsForRepos> {
let requestArgs: IGitHubGraphqlArgs = {
headers: {
authorization: `token ${this.token}`
},
query: closedPullRequestsQuery,
searchString: this.__searchQueryString(params)
};
if (params.request) {
requestArgs = { ...requestArgs, request: params.request };
}
const response: ISearchResponse = await graphql(requestArgs).catch(
(e: Error) => {
console.error(
`Failed to make request to GitHub. Received: ${e.message}`
);
throw e;
}
);
const pullRequestsGroupsByRepo: IPullRequestsForRepos = {};
return response.search.edges.reduce((accumulator, pullRequestResponse) => {
const repoName = pullRequestResponse.node.repository.name;
if (!accumulator[repoName]) {
accumulator[repoName] = [];
}
accumulator[repoName].push(pullRequestResponse.node);
return accumulator;
}, pullRequestsGroupsByRepo);
}
public __searchQueryString({
organization,
startDate,
endDate
}: IRequestParams) {
const formattedStartDate = format(startDate, "YYYY-MM-DD");
const formattedEndDate = format(endDate, "YYYY-MM-DD");
return `org:${organization} is:pr is:closed is:merged closed:${formattedStartDate}..${formattedEndDate}`;
}
}