forked from jupyterlite/litegitpuller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitpuller.ts
More file actions
335 lines (299 loc) · 9.09 KB
/
gitpuller.ts
File metadata and controls
335 lines (299 loc) · 9.09 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { PathExt } from '@jupyterlab/coreutils';
import { IDefaultFileBrowser } from '@jupyterlab/filebrowser';
import { Contents } from '@jupyterlab/services';
/**
* The abstract class for GitPuller using API.
*/
export abstract class GitPuller {
/**
* The constructor for the GitPuller abstract class.
*/
constructor(options: GitPuller.IOptions) {
this._defaultFileBrowser = options.defaultFileBrowser;
this._contents = options.contents;
}
/**
* The function to clone a repository.
*
* @param url - base URL of the repository using the API.
* @param branch - the targeted branch.
* @param basePath - the base directory where to clone the repo.
* @returns the path of the created directory.
*/
async clone(url: string, branch: string, basePath: string): Promise<string> {
const basePathComponents = basePath.split('/');
const basePathPrefixes = [];
for (let i = 0; i < basePathComponents.length; i++) {
basePathPrefixes.push(basePathComponents.slice(0, i + 1).join('/'));
}
// For a basePath 'a/b/c', create ['a', 'a/b', 'a/b/c']
await this.createTree(basePathPrefixes);
const fileList = await this.getFileList(url, branch);
await this.createTree(fileList.directories, basePath).then(async () => {
for (const file of fileList.files) {
const filePath = basePath ? PathExt.join(basePath, file) : file;
if (await this.fileExists(filePath)) {
this.addUploadError('File already exist', filePath);
continue;
}
// Upload missing files.
const fileContent = await this.getFile(url, file, branch);
await this.createFile(filePath, fileContent.blob, fileContent.type);
}
});
this._errors.forEach((value, key) => {
console.warn(
`The following files have not been uploaded.\nCAUSE: ${key}\nFILES: `,
value
);
});
return basePath;
}
/**
* Get files and directories list.
* This function must be defined by the classes that extends this one.
*
* @param url - base URL of the repository using the API.
* @param branch - the targeted branch.
*/
abstract getFileList(
url: string,
branch: string
): Promise<GitPuller.IFileList>;
/**
* Get the content of a file.
* This function must be defined by the classes that extends this one.
*
* @param url - base URL of the repository using the API.
* @param path - path of the file from the root of the repository.
* @param branch - the targeted branch.
*/
abstract getFile(
url: string,
path: string,
branch: string
): Promise<GitPuller.IFile>;
/**
* Create empty directories in content manager.
*
* @param directories - A list of directories.
* @param basePath - The root of the directories path.
*/
protected async createTree(
directories: string[],
basePath: string | null = null
): Promise<void> {
directories.sort();
for (let directory of directories) {
directory = basePath ? PathExt.join(basePath, directory) : directory;
const options = {
type: 'directory' as Contents.ContentType,
path: PathExt.dirname(directory)
};
// Create directory if it does not exist.
await this._contents
.get(directory, { content: false })
.catch(async () => {
const newDirectory = await this._contents.newUntitled(options);
await this._contents.rename(newDirectory.path, directory);
});
}
}
/**
* Check whether a file exists or not in the content manager.
*
* @param filePath - the file to check.
*/
protected async fileExists(filePath: string): Promise<boolean> {
return this._contents
.get(filePath, { content: false })
.then(() => true)
.catch(() => false);
}
/**
* Create a new file in the content manager.
*
* @param filePath - the path to the file.
* @param blob - the file content.
* @param type - the file type.
*/
protected async createFile(
filePath: string,
blob: Blob,
type: string
): Promise<void> {
let filename = PathExt.basename(filePath);
let inc = 0;
let uniqueFilename = false;
// The file must be first created at root path and then moved to its final path.
// Let's ensure an other file with the same name does not exists at root.
while (!uniqueFilename) {
await this._contents
.get(filename, { content: false })
.then(() => {
filename = `${inc}_${filename}`;
inc++;
})
.catch(e => {
uniqueFilename = true;
});
}
const file = new File([blob], filename, { type });
await this._defaultFileBrowser.model.upload(file).then(async model => {
if (!(model.path === filePath)) {
await this._contents.rename(model.path, filePath);
}
});
}
/**
* Add upload error in the map.
*
* @param error - the error.
* @param path - the path of the file in error.
*/
protected addUploadError(error: string, path: string) {
const errorFiles = this._errors.get(error) ?? [];
this._errors.set(error, [...errorFiles, path]);
}
protected _errors = new Map<string, string[]>();
protected _defaultFileBrowser: IDefaultFileBrowser;
protected _contents: Contents.IManager;
}
/**
* The GitPuller namespace.
*/
export namespace GitPuller {
/**
* The constructor options for the constructor.
*/
export interface IOptions {
defaultFileBrowser: IDefaultFileBrowser;
contents: Contents.IManager;
}
/**
* The files and directories list.
*/
export interface IFileList {
directories: string[];
files: string[];
}
/**
* The file content.
*/
export interface IFile {
blob: Blob;
type: string;
}
/**
* The error on file upload.
*/
export interface IUploadError {
type: string;
file: string;
}
}
/**
* The class to clone a repository from Github.
*/
export class GithubPuller extends GitPuller {
/**
* Get files and directories list.
*
* @param url - base URL of the repository using the API.
* @param branch - the targeted branch.
*/
async getFileList(url: string, branch: string): Promise<GitPuller.IFileList> {
const fetchUrl = `${url}/git/trees/${branch}?recursive=true`;
const fileList = await fetch(fetchUrl, {
method: 'GET',
headers: {
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
'User-Agent': 'request'
}
})
.then(resp => resp.json())
.then(data => data.tree as any[]);
const directories = Object.values(fileList)
.filter(fileDesc => fileDesc.type === 'tree')
.map(directory => directory.path as string);
const files = Object.values(fileList)
.filter(fileDesc => fileDesc.type === 'blob')
.map(file => file.path);
return { directories, files };
}
/**
* Get the content of a file.
*
* @param url - base URL of the repository using the API.
* @param path - path of the file from the root of the repository.
* @param branch - the targeted branch.
*/
async getFile(
url: string,
path: string,
branch: string
): Promise<GitPuller.IFile> {
const fetchUrl = `${url}/contents/${path}?ref=${branch}`;
const downloadUrl = await fetch(fetchUrl, {
method: 'GET',
headers: {
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
'User-Agent': 'request'
}
})
.then(resp => resp.json())
.then(data => data.download_url);
const resp = await fetch(downloadUrl);
const blob = await resp.blob();
const type = resp.headers.get('Content-Type') ?? '';
return { blob, type };
}
}
/**
* The class to clone a repository from a Gitlab server.
*/
export class GitlabPuller extends GitPuller {
/**
* Get files and directories list.
*
* @param url - base URL of the repository using the API.
* @param branch - the targeted branch.
*/
async getFileList(url: string, branch: string): Promise<GitPuller.IFileList> {
const fetchUrl = `${url}/repository/tree?ref=${branch}&recursive=true`;
const fileList = await fetch(fetchUrl, {
method: 'GET'
})
.then(resp => resp.json())
.then(data => data as any[]);
const directories = Object.values(fileList)
.filter(fileDesc => fileDesc.type === 'tree')
.map(directory => directory.path as string);
const files = Object.values(fileList)
.filter(fileDesc => fileDesc.type === 'blob')
.map(file => file.path);
return { directories, files };
}
/**
* Get the content of a file.
*
* @param url - base URL of the repository using the API.
* @param path - path of the file from the root of the repository.
* @param branch - the targeted branch.
*/
async getFile(
url: string,
path: string,
branch: string
): Promise<GitPuller.IFile> {
const fetchUrl = `${url}/repository/files/${encodeURIComponent(
path
)}/raw?ref=${branch}`;
const resp = await fetch(fetchUrl);
const blob = await resp.blob();
const type = resp.headers.get('Content-Type') ?? '';
return { blob, type };
}
}