-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-server.js
More file actions
197 lines (171 loc) · 5.87 KB
/
Copy pathnode-server.js
File metadata and controls
197 lines (171 loc) · 5.87 KB
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
const express = require('express');
const axios = require('axios');
const https = require('https');
const {spawn, exec} = require('child_process');
const bodyParser = require('body-parser');
require('dotenv').config();
const fs = require('fs');
const port = process.env.PORT || 5000;
const app = express();
app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const DEFAULT_REPO_DIR = './testRepo';
let settings = {
repoName: "",
buildCommand: "",
mainBranch: "",
period: 0,
};
// эта очередь будет с логикой в дз по инфраструктуре
const buildsQueue = [];
class Build {
constructor(buildNumber, commitMessage, commitHash, authorName, start, duration) {
this.buildNumber = buildNumber;
this.commitMessage = commitMessage;
this.commitHash = commitHash;
this.authorName = authorName;
this.start = start;
this.duration = duration;
}
}
const parseCommitData = (data, commitHash) => {
data = data.toString().split('\t');
return {
"commitMessage": data[1],
"commitHash": commitHash,
"branchName": data[2].split(',')[0],
"authorName": data[0]
}
};
const token = process.env.REACT_APP_API_KEY;
const api = axios.create({
baseURL: 'https://hw.shri.yandex/api/',
timeout: 1000,
headers: {
Authorization: 'Bearer ' + token,
},
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
// получение сохраненных настроек
app.get('/api/settings', (req, res) => {
try {
api.get('/conf', {}).then(({data}) => {
// const {repoName, buildCommand, mainBranch, period} = data;
// settings = {repoName, buildCommand, mainBranch, period};
res.send({data});
});
} catch (e) {
console.log(e);
res.send('Error');
}
},
);
// cохранение настроек
app.post('/api/settings', async (req, res) => {
let conf = req.body;
if (!fs.existsSync(DEFAULT_REPO_DIR)) {
console.log('repo does not exist');
try {
spawn('git', ['clone', conf.repoName, DEFAULT_REPO_DIR]).stdout.on('data', data => {
console.log('Repo cloned');
});
} catch (e) {
res.send(e);
}
}
let result = {};
try {
await api.post('/conf', conf);
// TODO: парсить реальные статусы ответа сервера
result = {status: 200};
} catch (e) {
result = {status: 400};
} finally {
res.send({result});
}
});
// получение списка сборок
app.get('/api/builds', (req, res) => {
api.get('/build/list', {params: {offset: 0, limit: 5}}).then(({data}) => {
return res.send(data.data);
}).catch(() => {
res.end('Error');
});
});
// добавление сборки в очередь
app.post('/api/builds', (req, res) => {
const commitHash = req.body.commitHash;
let result = {};
let commitData = '';
// 2e2e218201c5ef56f5a60909db02504a06060494 commit for testing
// try {
exec(`git -C testRepo/ log -1 --pretty=format:"%an\t%s\t%D" ${commitHash}\n`,
async function (error, stdout, stderr) {
const commitData = parseCommitData(stdout, commitHash);
console.log('Got commit data by hash', commitData);
let buildId = '';
let buildNumber = 0;
await api.post('/build/request', commitData)
.then(response => {
buildId = response.data.data.id;
buildNumber = response.data.data.buildNumber;
api.post('/build/start', {
"buildId": buildId,
"dateTime": new Date().toISOString(),
}).then(() => {
setTimeout(() => {
api.post('/build/finish', {
"buildId": buildId,
"duration": 3,
"success": true,
"buildLog": "log"
})
}, 3000);
}
);
});
result = {"buildNumber": buildNumber};
res.send({result});
// эта очередь будет с логикой в дз по инфраструктуре
// buildsQueue.push(commitData);
});
});
// получение информации о конкретной сборке
app.get('/api/build/details/:buildId', (req, res) => {
const buildId = req.body.buildId;
try {
api.get('/build/details', {params: {buildId}}).then(({data}) => {
console.log(data);
res.send(data);
});
// res.end('Success');
} catch (e) {
console.log(e);
// res.end('Error');
}
});
// получение логов билда (?)
app.get('/api/build/log/:buildId', (req, res) => {
const buildId = req.params.buildId;
console.log(req);
try {
api.get('/build/log', {params: {buildId}}).then(({data}) => {
console.log(data);
res.send(data);
});
} catch (e) {
console.log(e);
// res.end('Error');
}
});
app.listen(port, () => console.log(`Listening on port ${port}`));
module.exports = app;
/*
Testing with curl
curl -X POST "http://localhost:3000/settings" -H "Content-Type: application/json" -d "{\"repoName\":\"git@github.com:ElizabethSvit/shri-async-hw.git\"}"
curl -X POST "http://localhost:3000/builds" -H "Content-Type: application/json" -d "{\"commitHash\":\"fe9d127781ad8870631f8c2defffc5a61d0fe44d\"}"
curl http://localhost:3000/builds
* */