-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-server.js
149 lines (140 loc) · 3.91 KB
/
local-server.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
import express from 'express';
import fetch from 'node-fetch';
import * as dotenv from 'dotenv';
dotenv.config();
const app = express();
app.use(express.json());
const baseUrl = '/githubOauth';
/**
* get user's access token
*/
app.get(`${baseUrl}/getAccessToken`, async function (req, res) {
const response = await fetch(
`https://github.com/login/oauth/access_token?client_id=${process.env.CLIENT_ID}&client_secret=${process.env.CLIENT_SECRET}&code=${req.query.code}`,
{ method: 'POST', headers: { Accept: 'application/json' } }
);
const data = await response.json();
res.json(data.access_token);
});
/**
* get user information
*/
app.get(`${baseUrl}/getUser`, async (req, res) => {
const response = await fetch(' https://api.github.com/user', {
method: 'GET',
headers: {
Authorization: req.get('Authorization'),
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
},
});
const data = await response.json();
res.json(data);
});
/**
* get user's issues
*/
app.get(`${baseUrl}/getUserIssues/:page`, async function (req, res) {
const response = await fetch(
`https://api.github.com/issues?state=open&filter=all&per_page=6&page=${req.params.page}`,
{
method: 'GET',
headers: {
Authorization: req.get('Authorization'),
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
},
}
);
const data = await response.json();
res.json(data);
});
/**
* update issue
*/
app.patch(
`${baseUrl}/updateIssue/:owner/:repo/:issueNumber`,
async function (req, res) {
const response = await fetch(
`https://api.github.com/repos/${req.params.owner}/${req.params.repo}/issues/${req.params.issueNumber}`,
{
method: 'PATCH',
headers: {
Authorization: req.get('Authorization'),
Accept: ' application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
},
body: JSON.stringify(req.body),
}
);
const data = await response.json();
res.json(data);
}
);
/**
* search issues
*/
app.get(`${baseUrl}/search`, async function (req, res) {
const userParams = req.query.user ? `user:${req.query.user}` : '';
const repoParams = req.query.repo
? `repo:${req.query.user}/${req.query.repo}`
: '';
const queryParams = req.query.query || '';
const params = `${queryParams} ${userParams} ${repoParams}`;
const endcodeUrl = encodeURIComponent(params);
const response = await fetch(
`https://api.github.com/search/issues?q=${endcodeUrl}`,
{
method: 'GET',
headers: {
Authorization: req.get('Authorization'),
Accept: ' application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
},
}
);
const data = await response.json();
res.json(data);
});
/**
* create issue
*/
app.post(`${baseUrl}/create/:owner/:repo`, async function (req, res) {
const response = await fetch(
`https://api.github.com/repos/${req.params.owner}/${req.params.repo}/issues`,
{
method: 'POST',
headers: {
Authorization: req.get('Authorization'),
Accept: ' application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
},
body: JSON.stringify(req.body),
}
);
const data = await response.json();
res.json(data);
});
/**
* set issue labels
*/
app.put(
`${baseUrl}/setLabels/:owner/:repo/:issueNumber`,
async function (req, res) {
const response = await fetch(
`https://api.github.com/repos/${req.params.owner}/${req.params.repo}/issues/${req.params.issueNumber}/labels`,
{
method: 'PUT',
headers: {
Authorization: req.get('Authorization'),
Accept: ' application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
},
body: JSON.stringify(req.body),
}
);
const data = await response.json();
res.json(data);
}
);
app.listen(3000);