Skip to content

Commit 6d32604

Browse files
authored
Upload to a user-provided uploadpath (#26)
* Create the full upload basepath If a nested basepath `'a/b/c'` is given and does not exist yet, all directories in the basepath will be created, similar to using `mkdir -p` * Add a `uploadpath` query param to upload the repo to * Document the new `uploadpath` query parameter * Fix code formatting * Fix uploadpath default * Remove extraneous async * Add missing awaits * Fix prettier
1 parent fd07ac8 commit 6d32604

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Currently the allowed parameters are:
2525
- `repo`: (**required**) the github repository to fetch.
2626
- `branch`: the branch of the repository to fetch (default to _main_).
2727
- `urlpath`: the path to a notebook file to open (relative to the root of the repository).
28-
- `provider`: The provider of the API. Currently it supports _Github_ and _Gitlab_ API.
28+
- `provider`: the provider of the API. Currently it supports _Github_ and _Gitlab_ API.
29+
- `uploadpath`: the path to the directory in which the repository directory is created.
2930

3031
## Limitations
3132

src/gitpuller.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ export abstract class GitPuller {
2323
* @returns the path of the created directory.
2424
*/
2525
async clone(url: string, branch: string, basePath: string): Promise<string> {
26-
await this.createTree([basePath]);
26+
const basePathComponents = basePath.split('/');
27+
const basePathPrefixes = [];
28+
for (let i = 0; i < basePathComponents.length; i++) {
29+
basePathPrefixes.push(basePathComponents.slice(0, i + 1).join('/'));
30+
}
31+
32+
// For a basePath 'a/b/c', create ['a', 'a/b', 'a/b/c']
33+
await this.createTree(basePathPrefixes);
2734

2835
const fileList = await this.getFileList(url, branch);
2936

@@ -96,11 +103,12 @@ export abstract class GitPuller {
96103
path: PathExt.dirname(directory)
97104
};
98105
// Create directory if it does not exist.
99-
await this._contents.get(directory, { content: false }).catch(() => {
100-
this._contents.newUntitled(options).then(async newDirectory => {
106+
await this._contents
107+
.get(directory, { content: false })
108+
.catch(async () => {
109+
const newDirectory = await this._contents.newUntitled(options);
101110
await this._contents.rename(newDirectory.path, directory);
102111
});
103-
});
104112
}
105113
}
106114

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ const gitPullerExtension: JupyterFrontEndPlugin<void> = {
6161

6262
let puller: GitPuller | null = null;
6363

64-
const basePath = PathExt.basename(repo);
6564
const branch = urlParams.get('branch') || 'main';
6665
const provider = urlParams.get('provider') || 'github';
6766
const filePath = urlParams.get('urlpath');
67+
const uploadPath = urlParams.get('uploadpath') || '/';
68+
69+
const basePath = PathExt.join(uploadPath, PathExt.basename(repo));
6870

6971
const repoUrl = new URL(repo);
7072
if (provider === 'github') {
@@ -95,10 +97,10 @@ const gitPullerExtension: JupyterFrontEndPlugin<void> = {
9597
return;
9698
}
9799

98-
puller.clone(repoUrl.href, branch, basePath).then(async basePath => {
100+
puller.clone(repoUrl.href, branch, basePath).then(repoPath => {
99101
if (filePath) {
100102
app.commands.execute('filebrowser:open-path', {
101-
path: PathExt.join(basePath, filePath)
103+
path: PathExt.join(repoPath, filePath)
102104
});
103105
}
104106
});

0 commit comments

Comments
 (0)