Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload to a user-provided uploadpath #26

Merged
merged 8 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Currently the allowed parameters are:
- `repo`: (**required**) the github repository to fetch.
- `branch`: the branch of the repository to fetch (default to _main_).
- `urlpath`: the path to a notebook file to open (relative to the root of the repository).
- `provider`: The provider of the API. Currently it supports _Github_ and _Gitlab_ API.
- `provider`: the provider of the API. Currently it supports _Github_ and _Gitlab_ API.
- `uploadpath`: the path to the directory in which the repository directory is created.

## Limitations

Expand Down
16 changes: 12 additions & 4 deletions src/gitpuller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ export abstract class GitPuller {
* @returns the path of the created directory.
*/
async clone(url: string, branch: string, basePath: string): Promise<string> {
await this.createTree([basePath]);
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);

Expand Down Expand Up @@ -96,11 +103,12 @@ export abstract class GitPuller {
path: PathExt.dirname(directory)
};
// Create directory if it does not exist.
await this._contents.get(directory, { content: false }).catch(() => {
this._contents.newUntitled(options).then(async newDirectory => {
await this._contents
.get(directory, { content: false })
.catch(async () => {
const newDirectory = await this._contents.newUntitled(options);
await this._contents.rename(newDirectory.path, directory);
});
});
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ const gitPullerExtension: JupyterFrontEndPlugin<void> = {

let puller: GitPuller | null = null;

const basePath = PathExt.basename(repo);
const branch = urlParams.get('branch') || 'main';
const provider = urlParams.get('provider') || 'github';
const filePath = urlParams.get('urlpath');
const uploadPath = urlParams.get('uploadpath') || '/';

const basePath = PathExt.join(uploadPath, PathExt.basename(repo));

const repoUrl = new URL(repo);
if (provider === 'github') {
Expand Down Expand Up @@ -95,10 +97,10 @@ const gitPullerExtension: JupyterFrontEndPlugin<void> = {
return;
}

puller.clone(repoUrl.href, branch, basePath).then(async basePath => {
puller.clone(repoUrl.href, branch, basePath).then(repoPath => {
if (filePath) {
app.commands.execute('filebrowser:open-path', {
path: PathExt.join(basePath, filePath)
path: PathExt.join(repoPath, filePath)
});
}
});
Expand Down
Loading