-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.js
executable file
·234 lines (173 loc) · 4.92 KB
/
util.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Form parent ids
function formParents_GL({baseInfo, mergeInfo}){
//TODO: what if pr has two CAs
let caHead = mergeInfo.commits[0].parent_ids[0];
let parents = {
baseHead: baseInfo.commit.id,
prHead: mergeInfo.commit.id
}
if (parents.baseHead !== caHead)
parents["caHead"] = caHead
return ({ parents })
}
// Form parent ids
function formParents_GH({ mergeInfo }){
//TODO: what if pr has two CAs
let caHead = mergeInfo.merge_base_commit.sha;
let parents = {
baseHead: mergeInfo.base_commit.sha,
prHead: mergeInfo.commits.pop().sha
};
if (parents.baseHead !== caHead)
parents["caHead"] = caHead
return ({ parents })
}
// Form urls for all trees need to be fetched
function formTreeUrls_GL(baseBranch, prBranch, dirs){
/**
* To create the merge commit, we need only trees of base and pr
* No need to get the tree of common ancestor
* In case of GitLab, we get trees for changed dirs
**/
let trees = {
base: treeUrls_GL(baseBranch, dirs),
pr: treeUrls_GL(prBranch, dirs),
}
return {
urls: [...trees.base, ...trees.pr]
}
}
// Form urls for all trees need to be fetched
function formTreeUrls_GH(prInfo, mergeInfo){
/**
* To create the merge commit, we need only trees of base and pr,
* no need to get the tree of common ancestor.
* In case of GitHub, we get all trees based on two hashes.
* Commits on mergeInfo are not always included on Github response,
* so we get tree info from pr branch
**/
let trees = {
base: mergeInfo.base_commit.commit.tree.sha,
pr: prInfo.commit.tree.sha,
//pr: mergeInfo.commits.pop().commit.tree.sha
}
return {
treeIds: trees,
urls: treeUrls_GH([trees.base, trees.pr])
}
}
function treeUrls_GL(branch, dirs){
let urls = [];
let endpoint = `${REPO_API}/tree?ref=${branch}&path=`;
for (dir of dirs){
urls.push(`${endpoint}${trimPath(dir)}`);
}
return urls;
}
function treeUrls_GH(trees){
// TODO: Handle truncated trees
let urls = [];
let endpoint = `${REPO_API}/git/trees/`;
for (tree of trees){
urls.push(`${endpoint}${tree}?recursive=1`);
}
return urls;
}
// Form urls for all files need to be fetched
function formBlobUrls(parents, blobs){
// If baseHead and caHead are not the same, fetch files for caHead
let caRefs = []
if (parents.hasOwnProperty("caHead"))
caRefs = fileUrls (parents.caHead, blobs)
let baseRefs = fileUrls (parents.baseHead, blobs)
let prRefs = fileUrls (parents.prHead, blobs)
return [...baseRefs, ...prRefs, ...caRefs]
}
// Form file Endpoints for a remote ref
function fileUrls(ref, files){
let urls = [];
if (SERVER == SERVER_GL){
let endpoint = `${REPO_API}/files/`;
for (file of files){
urls.push(`${endpoint}${trimPath(file)}/raw?ref=${ref}`);
}
}
if (SERVER == SERVER_GH){
let endpoint = `${REPO_API}/contents/`;
for (file of files){
urls.push(`${endpoint}${file}?ref=${ref}`);
}
}
return urls;
}
// Extract the fpath and ref name of fetched blobs
function blobParser({ item, info, data }){
let result;
if (SERVER == SERVER_GL)
result = blob_GL({ item, info, data });
if (SERVER == SERVER_GH)
result = blob_GH({ item, info, data });
let {key, ref, content} = result;
// Initialize sub-object
if(key in data == false){
data[key] = {};
}
data [key][ref] = content; //TODO: Make a return
}
// Extract the fpath and ref name of fetched blobs
function blob_GL({ item, info }){
let key = untrimPath(extractBetween(item, "/files/", "/raw?"));
let ref = extractAfter(item, "?ref=");
return {key, ref, content: info}
}
// Extract the fpath and ref name of fetched blobs
function blob_GH({ item, info}){
let key = info.path;
let ref = extractAfter(info.url, "?ref=");
return {key, ref, content: atob(info.content)}
}
// Extract the fpath and ref name of fetched trees
function treeParser_GL({ item, info, data }){
let key = extractBetween(item, "?ref=", "&path=");
let ref = untrimPath(extractAfter(item, "&path="));
// Initialize sub-object, otherwise you'll get errors
if(key in data == false){
data[key] = {};
}
data [key][ref] = formTreeEntries_GL(info);
}
// Extract the fpath and ref name of fetched trees
function treeParser_GH({ info, data }){
data [info.sha] = info.tree;
}
//Form Trees
function formTreeEntries_GL(entries){
let trees = {};
for (let entry of entries) {
// Omit the first char of the tree mode
let mode = entry.mode;
entry.mode = mode == "040000"? "40000": mode;
trees[entry.name] = entry;
}
return trees;
}
//Form Trees
function formTreeEntries_GH(entries){
let trees = {}
for (let entry of entries) {
let path = entry.path;
let parent = getParentPath(path);
let name = removeParentPath (path);
if(parent in trees == false){
trees[parent] = {};
}
// Add entry name, rename sha with id
entry["name"] = name;
entry["id"] = entry.sha; delete entry["sha"];
// Omit the first char of the tree mode
let mode = entry.mode;
entry.mode = mode == "040000"? "40000": mode;
trees[parent][name] = entry;
}
return trees;
}