-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (169 loc) · 4.01 KB
/
index.js
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
const Table = require('cli-table');
const env = require('node-env-file');
const fetch = require('node-fetch');
try {
env(__dirname + '/.env');
} catch(e) {
console.error('.env file missing!');
}
const query = `
{
user(login: "${process.env.LOGIN}") {
pullRequests(last: 100, states: OPEN) {
edges {
node {
repository{
name
}
state
resourcePath
baseRefName
headRefName
reviews(last:10) {
edges{
node{
state
}
}
}
commits(last: 1) {
edges {
node {
commit {
status {
state,
contexts {
targetUrl,
state,
description,
context
}
}
}
}
}
}
}
}
}
}
}`;
const queryReviewRequests = `
query {
search(query: "type:pr state:open review-requested:${process.env.LOGIN}", type: ISSUE, first: 100) {
issueCount
pageInfo {
endCursor
startCursor
}
edges {
node {
... on PullRequest {
repository {
nameWithOwner
}
number
url
}
}
}
}
}
`;
fetch('https://api.github.com/graphql', {
method: 'POST',
body: JSON.stringify({query}),
headers: {
'Authorization': `Bearer ${process.env.API_KEY}`,
},
}).then(res => res.text())
.then(parseResult)
.catch(error => console.error(error));
fetch('https://api.github.com/graphql', {
method: 'POST',
body: JSON.stringify({"query": queryReviewRequests}),
headers: {
'Authorization': `Bearer ${process.env.API_KEY}`,
},
}).then(res => res.text())
.then(parseReviewRequests)
.catch(error => console.error(error));
function parseResult(body) {
const data = JSON.parse(body);
const results = new Table({
head: ['Repository', 'CI', 'Review', 'Branch', 'Base', 'Link']
});
data.data.user.pullRequests.edges.forEach(edge => {
const node = edge.node;
if (node.state !== 'OPEN') {
return;
}
let state = '';
let link = 'https://github.com' + node.resourcePath;
let failedJobs = [];
let errorUrl = '';
if (node.commits.edges[0].node.commit.status) {
switch (node.commits.edges[0].node.commit.status.state) {
case 'SUCCESS':
state = 'OK';
break;
case 'FAILURE':
state = 'ERR';
node.commits.edges[0].node.commit.status.contexts.forEach(function(context) {
if (context.state == 'ERROR') {
errorUrl = context.targetUrl;
} else if (context.state == 'FAILURE') {
failedJobs.push(context.context);
}
});
break;
case 'PENDING':
state = 'PEN';
break;
}
}
let approval = '';
node.reviews.edges.forEach(edge => {
switch (edge.node.state) {
case 'APPROVED':
case 'DISMISSED':
approval = 'Approved';
break;
case 'CHANGES_REQUESTED':
approval = 'Changes requested';
break;
}
});
if (errorUrl.length > 0) {
link += '\n' + errorUrl;
}
if (failedJobs.length > 0) {
link += '\n\nFailed:\n' + failedJobs.join('\n');
}
results.push([
node.repository.name,
state,
approval,
node.headRefName,
node.baseRefName,
link
]);
});
console.log(results.toString());
}
function parseReviewRequests(body) {
const data = JSON.parse(body);
const results = new Table({
head: ['Repository', 'Link']
});
data.data.search.edges.forEach(edge => {
const node = edge.node;
results.push([
node.repository.nameWithOwner,
node.url
]);
});
if (results.length > 0) {
console.log(results.toString());
}
}