-
Notifications
You must be signed in to change notification settings - Fork 10
/
github.service.ts
199 lines (182 loc) · 5.25 KB
/
github.service.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import moment from 'moment';
import {graphql} from "@octokit/graphql";
import fetch from "node-fetch";
import {getOwnerAndName} from "../../runners/queues/score.queue";
const runQuery = async (query: string, token: string): Promise<any> => {
try {
const data = await graphql(query, {
request: {
fetch
},
headers: {
authorization: `token ${token}`,
}
});
return {data};
} catch (err) {
const data = await graphql(query, {
request: {
fetch
},
headers: {
authorization: `Basic ${process.env.GITHUB_AUTH}`,
}
});
console.log(data);
return {data};
}
}
const year = moment().format('YYYY');
interface PullsById {
"data": {
"nodes": Array<{
"id": string, "createdAt": string, "title": string, "url": string
}>
}
}
interface GraphQLResponse {
data: {
rateLimit: {
remaining: number
}, search: {
issueCount: number, edges: Array<{
cursor: string,
node: {
id: string; createdAt: string, title: string, url: string
}
}>
}
}
}
interface GraphQLPResponse {
data: {
search: {
edges: Array<{
cursor: string, node: {
author: {
login: string
}
}
}>
}
}
}
export class GithubService {
static async loadPrDetails(ids: string[], token: string): Promise<PullsById> {
try {
const {data}: { data: PullsById } = await runQuery(`
query {
nodes(ids: ${JSON.stringify(ids)}) {
... on PullRequest {
id
createdAt
title
url
}
}
}
`, token);
return data;
} catch (err) {
return {
data: {
nodes: []
}
}
}
}
static async loadUserPRs(name: string, token: string, after = ''): Promise<{ total: number, issues: Array<{ id: string, createdAt: string, title: string, url: string }> }> {
console.log('Calculating ' + name);
if (after) {
console.log('getting after');
}
try {
const data: GraphQLResponse = await runQuery(`
{
rateLimit{
remaining
}
search (
first: 100
type: ISSUE
${after ? `after: "${after}"` : ''}
query: "-label:spam,invalid is:closed author:${name} is:pr sort:created-desc merged:${year}-10-01..${year}-10-31T23:59:00"
) {
issueCount
edges {
cursor
node {
... on PullRequest {
id
createdAt
title
url
}
}
}
}
}
`, token);
return {
total: data.data.search.issueCount || 0,
issues: [...data?.data?.search?.edges.map(p => p.node), ...(data?.data?.search?.edges?.length || 0) < 100 ? [] : (await this.loadUserPRs(name, token, data.data.search.edges[data.data.search.edges.length - 1].cursor)).issues]
};
} catch (err) {
console.log(err);
console.log(`There was a problem getting ${name}`);
return {total: 0, issues: []};
}
}
static async totalRepositoryStars(name: string, token: string): Promise<number> {
try {
const props = getOwnerAndName(name);
const {data}: { data: any } = await runQuery(`
{
repository(owner:"${props.owner}", name:"${props.name}") {
stargazers {
totalCount
}
}
}
`, token);
return data.repository.stargazers.totalCount;
}
catch (err) {
console.log('err', name.startsWith('/') ? name.slice(1) : name);
return 0;
}
}
static async loadAllMembersMergedPr(after = '', token: string): Promise<string[]> {
console.log(`taking after ${after}`);
const query = `
query {
search(
first: 100
type: ISSUE,
${after ? `after: "${after}"` : ''}
query: "org:novuhq is:pr is:merged merged:${year}-10-01..${year}-10-31T23:59:00"
) {
issueCount,
edges {
cursor
node {
... on PullRequest {
author {
login
}
}
}
}
}
}
`;
try {
const {data}: { data: GraphQLPResponse } = await runQuery(query, token);
return [
...data.data.search.edges.map(e => e.node.author.login),
...data.data.search.edges.length === 100 ? await this.loadAllMembersMergedPr(data.data.search.edges[data.data.search.edges.length - 1].cursor, token) : []]
} catch (err) {
return []
}
}
}