-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (65 loc) · 1.57 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
'use strict';
const graphqlGot = require('graphql-got');
const controlAccess = require('control-access');
const ONE_DAY = 60 * 60 * 24;
const {
GITHUB_TOKEN,
GITHUB_USERNAME,
ACCESS_ALLOW_ORIGIN,
ACCESS_MAX_COUNT = 5
} = process.env;
if (!GITHUB_TOKEN) {
throw new Error('Please set your GitHub token in the `GITHUB_TOKEN` environment variable');
}
if (!GITHUB_USERNAME) {
throw new Error('Please set your GitHub username in the `GITHUB_USERNAME` environment variable');
}
if (!ACCESS_ALLOW_ORIGIN) {
throw new Error('Please set the `access-control-allow-origin` you want in the `ACCESS_ALLOW_ORIGIN` environment variable');
}
const query = `
query {
user(login: "${GITHUB_USERNAME}") {
pullRequests(
last: ${ACCESS_MAX_COUNT},
orderBy: {
field: CREATED_AT,
direction: ASC
}
) {
edges {
node {
author {
login
}
url
state
publishedAt
mergedAt
}
}
}
}
}
`;
const fetchRepos = async () => {
const {body} = await graphqlGot('api.github.com/graphql', {
query,
token: GITHUB_TOKEN
});
return body.user.pullRequests.edges;
};
module.exports = async (request, response) => {
controlAccess()(request, response);
try {
const repos = await fetchRepos();
response.setHeader('content-type', 'application/json');
response.setHeader('cache-control', `s-maxage=${ONE_DAY}, max-age=0`);
response.end(JSON.stringify(repos));
} catch (error) {
console.error(error);
response.statusCode = 500;
response.setHeader('content-type', 'text/plain');
response.end('Internal server error');
}
};