Skip to content

Commit

Permalink
Test nbgitpuller API to make sure it is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
brichet committed Jul 23, 2024
1 parent 70d6240 commit 08fec45
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,58 @@ import {
} from '@jupyterlab/application';
import { PathExt } from '@jupyterlab/coreutils';
import { IDefaultFileBrowser } from '@jupyterlab/filebrowser';
import { ServerConnection } from '@jupyterlab/services';
import { GitPuller, GithubPuller, GitlabPuller } from './gitpuller';

/**
* Test if nbgitpuller extension is also installed by requesting its Rest API.
* This test avoid fetching the same repository two times.
*/
export async function testNbGitPuller(): Promise<boolean> {
// Make request to Jupyter API
const settings = ServerConnection.makeSettings();
const params = {
repo: 'https://github.com/jupyterlite/litegitpuller',
branch: 'main'
};
const searchParams = new URLSearchParams(params);
const requestUrl = `${settings.baseUrl}git-pull/api?${searchParams}`;
let response: Response;
try {
response = await ServerConnection.makeRequest(
requestUrl,
{ method: 'GET' },
settings
);
} catch (error) {
return false;
}

if (!response.ok) {
return false;
}

return true;
}

const gitPullerExtension: JupyterFrontEndPlugin<void> = {
id: '@jupyterlite/litegitpuller:plugin',
autoStart: true,
requires: [IDefaultFileBrowser],
activate: (app: JupyterFrontEnd, defaultFileBrowser: IDefaultFileBrowser) => {
activate: async (
app: JupyterFrontEnd,
defaultFileBrowser: IDefaultFileBrowser
) => {
if (await testNbGitPuller()) {
console.log(
'@jupyterlite/litegitpuller is not activated to avoid conflict with nbgitpuller'
);
return;
}

console.log(
'JupyterLab extension @jupyterlite/litegitpuller is activated!'
);
if (!(app.name === 'JupyterLite')) {
return;
}

const urlParams = new URLSearchParams(window.location.search);
const repo = urlParams.get('repo');
Expand Down

0 comments on commit 08fec45

Please sign in to comment.