-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (76 loc) · 2.28 KB
/
index.js
File metadata and controls
80 lines (76 loc) · 2.28 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
require("dotenv").config();
const Github = require("./src/github");
exports.mcrc_repo_github = function(req, resp) {
const github = new Github(req.body.access_token);
const respJson = JSON.parse(JSON.stringify(message_format));
const method = req.method;
try {
switch (method) {
case "POST":
if (req.body.repo_name && req.body.access_token) {
console.log("CREATION START");
github
.create_repo(req.body.repo_name)
.then(data => {
console.log("MAIN THEN CALLING.");
respJson.body = createSuccessResp("Repo created", data);
resp.send(respJson);
})
.catch(err => {
console.log("CATCH CALLLING");
respJson.body = createErrResp("Error", err.errors[0]);
resp.send(respJson);
});
} else {
throw new Error("repo_name and access_token required");
}
break;
case "DELETE":
if (req.body.user_name && req.body.repo_name) {
github
.delete_repo(req.body.user_name, req.body.repo_name)
.then(data => {
respJson.body = createSuccessResp("Repo deleted", data);
resp.send(respJson);
})
.catch(err => {
respJson.body = createErrResp("Error", err.errors[0]);
resp.send(respJson);
});
} else {
throw new Error("user_name and repo_name required");
}
break;
default:
throw new Error("Method not supported");
}
} catch (error) {
console.log("#############################################");
console.log(error);
respJson.body = createErrResp("Error", error);
console.log(respJson.body);
resp.send(respJson);
}
};
const message_format = {
statusCode: 200,
isBase64Encoded: false,
body: {
status: "",
message: "",
data: {},
error: null
}
};
function createSuccessResp(message, data) {
let resp_body = {};
(resp_body.status = "OK"), (resp_body.message = message);
resp_body.data = data;
return resp_body;
}
function createErrResp(message, err) {
let resp_body = {};
(resp_body.status = "FAIL"), (resp_body.message = message);
resp_body.err = err;
return resp_body;
}