-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitapi.js
109 lines (99 loc) · 2.65 KB
/
gitapi.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
const request = require('sync-request');
const logger = require('./logger');
const getHttpHeader = function (token) {
var headers = {};
if (token != null) {
var auth = 'token ' + token;
headers = { 'user-agent': 'node.js', 'Authorization': auth };
} else {
headers = { 'user-agent': 'node.js' };
}
return headers;
}
exports.isBranch = function (token, repo, branch) {
const url = "https://api.github.com/repos" + repo + "/branches/" + branch;
const res = request('GET', url, {
headers: getHttpHeader(token),
});
let status = {};
try {
if (JSON.parse(res.getBody().toString('utf8')).name) {
status.type = "success";
return status;
}
} catch (err) {
status.type = "error";
status.httpcode = err.statusCode;
status.body = err.body;
return status;
}
status.type = "error";
return status;
}
exports.isCommit = function (token, repo, commit) {
const url = "https://api.github.com/repos" + repo + "/commits/" + commit;
const res = request('GET', url, {
headers: getHttpHeader(token),
});
let status = {};
try {
if (JSON.parse(res.getBody().toString('utf8')).commit) {
status.type = "success";
return status;
}
} catch (err) {
status.type = "error";
status.httpcode = err.statusCode;
status.body = err.body;
return status;
}
status.type = "error";
return status;
}
exports.isCommitHasABranch = function (token, repo, commit) {
const url = "https://github.com" + repo + "/branch_commits/" + commit;
let status = {};
let body = {};
try {
const res = request('GET', url, {
headers: getHttpHeader(token),
});
body = res.getBody().toString('utf8');
if (body.search("This commit does not belong to any branch on this repository") == -1) {
status.type = "success";
return status;
}
}catch(err){
status.type = "error";
status.httpcode = err.statusCode;
status.body = err.body;
return status;
}
status.body = commit+ " does not have branch, ";
if(body.search("branches-tag-list") >=0){
status.body += commit+"but commit has tags";
}
status.httpcode = 404;
status.type = "error";
return status;
}
exports.isRepository = function (token, repo) {
const url = "https://api.github.com/repos" + repo;
const res = request('GET', url, {
headers: getHttpHeader(token),
});
let status = {};
try {
if (JSON.parse(res.getBody().toString('utf8')).name) {
status.type = "success";
return status;
}
} catch (err) {
status.type = "error";
status.httpcode = err.statusCode;
status.body = err.body;
return status;
}
status.type = "error";
return status;
}