-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.js
170 lines (161 loc) · 4.51 KB
/
github.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
jsPsych.github = (function () {
var module = {};
var owner = "";
var repo = "";
var path = "";
var token = "";
var header = {};
module.config = function (config) {
if (typeof (config) != "object") {
return;
}
if(Object.keys(config).length < 4) {
console.error("Error: There should be at least 4 parameters")
}
owner = config["owner"] ? config["owner"] : "";
repo = config["repo"] ? config["repo"] : "";
path = config["path"] ? config["path"] : "";
token = config["token"] ? config["token"] : "";
if(token.length < 1) {
header = {
"Content-Type": "application/json"
}
} else {
header = {
"Content-Type": "application/json",
"Authorization": `token ${token}`
}
}
}
// Simplify uploading files
module.upload = function(fileName, message, content) {
if (module.isFileExist(fileName)) {
return module.update(fileName, message, content);
} else {
return module.push(fileName, message, content);
}
}
// new file upload
module.push = function (fileName, message, content) {
let formd = {
message: message,
content: btoa(content)
};
let res = new XMLHttpRequest();
res.open(
"PUT",
`https://api.github.com/repos/${owner}/${repo}/contents${path}/${fileName}`,
false
);
for (k in header) {
res.setRequestHeader(k, header[k]);
}
res.send(JSON.stringify(formd));
if(res.status >= 400) {
return false;
} else {
return true;
}
}
// update file
module.update = function (fileName, message, content) {
let formd = {
message: message,
content: btoa(content),
sha: module.getFileSha(fileName)
};
let res = new XMLHttpRequest();
res.open(
"PUT",
`https://api.github.com/repos/${owner}/${repo}/contents${path}/${fileName}`,
false
)
for (k in header) {
res.setRequestHeader(k, header[k]);
}
res.send(JSON.stringify(formd));
if(res.status >= 400) {
return false;
} else {
return true;
}
}
// get last commit sha
module.getLastSha = function () {
let res = new XMLHttpRequest();
res.open(
"GET",
`https://api.github.com/repos/${owner}/${repo}/commits`,
false
);
for (k in header) {
res.setRequestHeader(k, header[k]);
}
res.send();
return JSON.parse(res.responseText)[0].sha;
}
// get file id by the sha
module.getFileSha = function(fileName) {
let res = new XMLHttpRequest();
res.open(
"GET",
`https://api.github.com/repos/${owner}/${repo}/contents${path}/${fileName}`,
false
);
for (k in header) {
res.setRequestHeader(k, header[k]);
}
res.send();
// console.log(res);
return JSON.parse(res.responseText)["sha"]
}
// delete file
module.delete = function(fileName, message) {
let formd = {
message: message,
sha: module.getFileSha(fileName)
};
let res = new XMLHttpRequest();
res.open(
"DELETE",
`https://api.github.com/repos/${owner}/${repo}/contents${path}/${fileName}`,
false
)
for (k in header) {
res.setRequestHeader(k, header[k]);
}
res.send(JSON.stringify(formd));
if(res.status >= 400) {
return false;
} else {
return true;
}
}
// Determine whether the file exists
module.isFileExist = function(fileName) {
let res = new XMLHttpRequest();
res.open(
"GET",
`https://api.github.com/repos/${owner}/${repo}/contents${path}/${fileName}`,
false
)
for (k in header) {
res.setRequestHeader(k, header[k]);
}
res.send();
if(res.status == 200) {
return true;
} else {
return false;
}
}
module.getID = function(experID = "", length = 4, suffix = "") {
let name = `${experID ? experID : ""}`;
let i = 1;
while(module.isFileExist(name + i.toString().padStart(length, "0") + suffix + ".csv")) {
i++
}
return i;
}
return module;
})();