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

Link generator handles non-git sources #195

Open
wants to merge 13 commits into
base: gh-pages
Choose a base branch
from
12 changes: 12 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2.1
jobs:
build:
docker:
- image: circleci/python:3.6
steps:
- run: echo "no operation build"

workflows:
build:
jobs:
- build
156 changes: 123 additions & 33 deletions _static/link_gen/link.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
// Pure function that generates an nbgitpuller URL
function generateRegularUrl(hubUrl, urlPath, repoUrl, branch) {
function generateRegularUrl(hubUrl, urlPath, repoUrl, branch, compressed, source) {

// assume hubUrl is a valid URL
var url = new URL(hubUrl);

url.searchParams.set('repo', repoUrl);

if(compressed) {
url.searchParams.set('content_provider', source);
}
if (urlPath) {
url.searchParams.set('urlpath', urlPath);
}

if (branch) {
url.searchParams.set('branch', branch);
} else if(source == "git"){
url.searchParams.set('branch', "main");
}

if (!url.pathname.endsWith('/')) {
Expand All @@ -22,20 +26,24 @@ function generateRegularUrl(hubUrl, urlPath, repoUrl, branch) {
return url.toString();
}

function generateCanvasUrl(hubUrl, urlPath, repoUrl, branch) {
function generateCanvasUrl(hubUrl, urlPath, repoUrl, branch, compressed, source) {
// assume hubUrl is a valid URL
var url = new URL(hubUrl);

var nextUrlParams = new URLSearchParams();

nextUrlParams.append('repo', repoUrl);

if(compressed) {
nextUrlParams.append('content_provider', source);
}
if (urlPath) {
nextUrlParams.append('urlpath', urlPath);
}

if (branch) {
nextUrlParams.append('branch', branch);
} else if(source == "git"){
nextUrlParams.append('branch', "main");
}

var nextUrl = '/hub/user-redirect/git-pull?' + nextUrlParams.toString();
Expand All @@ -50,20 +58,25 @@ function generateCanvasUrl(hubUrl, urlPath, repoUrl, branch) {
}

function generateBinderUrl(hubUrl, userName, repoName, branch, urlPath,
contentRepoUrl, contentRepoBranch) {
contentRepoUrl, contentRepoBranch, compressed, source) {

var url = new URL(hubUrl);

var nextUrlParams = new URLSearchParams();

nextUrlParams.append('repo', contentRepoUrl);

if(compressed) {
nextUrlParams.append('content_provider', source);
}
if (urlPath) {
nextUrlParams.append('urlpath', urlPath);
}

if (contentRepoBranch) {
nextUrlParams.append('branch', contentRepoBranch);
} else if(source == "git"){
nextUrlParams.append('branch', "main");
}

var nextUrl = 'git-pull?' + nextUrlParams.toString();
Expand Down Expand Up @@ -100,37 +113,37 @@ var apps = {
}
}

function clearLinks(){
document.getElementById('default-link').value = "";
document.getElementById('binder-link').value = "";
document.getElementById('canvas-link').value = "";
}

function changeTab(div) {
var hub = document.getElementById("hub");
var hub_help_text = document.getElementById("hub-help-text");
var env_repo = document.getElementById("repo");
var env_repo_branch = document.getElementById("branch");
var env_repo_help_text = document.getElementById("env-repo-help-text");
var content_repo = document.getElementById("content-repo-group");
var content_branch = document.getElementById("content-branch-group");
var env_repo_group = document.getElementById("env-repo-group");
var env_repo = document.getElementById("env-repo");
var id = div.id;

var form = document.getElementById('linkgenerator');
clearLinks();
if (id.includes("binder")) {
hub.placeholder = "https://mybinder.org";
hub.value = "https://mybinder.org";
hub_help_text.hidden = true;
hub.labels[0].innerHTML = "BinderHub URL";
env_repo.labels[0].innerHTML = "Git Environment Repository URL";
env_repo_help_text.hidden = false;
env_repo_branch.required = true;
env_repo_branch.pattern = ".+";
content_repo.hidden = false;
content_branch.hidden = false;
env_repo_group.style.display = '';
env_repo.disabled = false;
} else {
hub.placeholder = "https://hub.example.com";
hub.value = "";
hub_help_text.hidden = false;
hub.labels[0].innerHTML = "JupyterHub URL";
env_repo.labels[0].innerHTML = "Git Repository URL";
env_repo_help_text.hidden = true;
env_repo_branch.required = false;
content_repo.hidden = true;
content_branch.hidden = true;

env_repo_group.style.display = 'none';
env_repo.disabled = true;
}
displaySource();
}

/**
Expand All @@ -141,28 +154,58 @@ function changeTab(div) {
* See https://github.com/git/git/blob/1c52ecf4ba0f4f7af72775695fee653f50737c71/builtin/clone.c#L276
*/
function generateCloneDirectoryName(gitCloneUrl) {
if(gitCloneUrl.slice(-1) == "/")
gitCloneUrl = gitCloneUrl.slice(0,-1);
var lastPart = gitCloneUrl.split('/').slice(-1)[0];
return lastPart.split(':').slice(-1)[0].replace(/(\.git|\.bundle)?/, '');
}

function handleSource(args){
source = args["source"];
branch = "";
compressed = true;
sourceUrl ="";
if(source == "git"){
sourceUrl = args["contentRepoUrl"];
branch = args["contentRepoBranch"];
compressed = false;
} else if(source == "googledrive"){
sourceUrl = args["driveUrl"];
} else if(source == "dropbox"){
sourceUrl = args["dropUrl"];
} else if(source == "generic_web"){
sourceUrl = args["webUrl"];
}
return {
"branch": branch,
"sourceUrl": sourceUrl,
"compressed": compressed
}
}

function displayLink() {
var form = document.getElementById('linkgenerator');

form.classList.add('was-validated');
if (form.checkValidity()) {
var hubUrl = document.getElementById('hub').value;
var repoUrl = document.getElementById('repo').value;
var branch = document.getElementById('branch').value;
var driveUrl = document.getElementById('drive-url').value;
var dropUrl = document.getElementById('drop-url').value;
var webUrl = document.getElementById('generic-web-url').value;
var envRepoUrl = document.getElementById('env-repo').value;
var envGitBranch = document.getElementById('env-branch').value;
var contentRepoUrl = document.getElementById('content-repo').value;
var contentRepoBranch = document.getElementById('content-branch').value;
var filePath = document.getElementById('filepath').value;
var appName = form.querySelector('input[name="app"]:checked').value;
var activeTab = document.querySelector(".nav-link.active").id;

var source = form.querySelector('input[name="source"]:checked').value;
if (appName === 'custom') {
var urlPath = document.getElementById('urlpath').value;
} else {
var repoName = generateCloneDirectoryName(repoUrl);
var repoName = generateCloneDirectoryName(contentRepoUrl);
if(source !== "git"){
repoName = ""
}
var urlPath;
if (activeTab === "tab-auth-binder") {
var contentRepoName = new URL(contentRepoUrl).pathname.split('/').pop().replace(/\.git$/, '');
Expand All @@ -171,26 +214,37 @@ function displayLink() {
urlPath = apps[appName].generateUrlPath(repoName + '/' + filePath);
}
}

args = {
"source": source,
"contentRepoUrl": contentRepoUrl,
"contentRepoBranch": contentRepoBranch,
"driveUrl": driveUrl,
"dropUrl": dropUrl,
"webUrl": webUrl
}
config = handleSource(args)
if (activeTab === "tab-auth-default") {
document.getElementById('default-link').value = generateRegularUrl(
hubUrl, urlPath, repoUrl, branch
hubUrl, urlPath, config["sourceUrl"], config["branch"], config["compressed"], source
);
} else if (activeTab === "tab-auth-canvas"){
document.getElementById('canvas-link').value = generateCanvasUrl(
hubUrl, urlPath, repoUrl, branch
hubUrl, urlPath, config["sourceUrl"], config["branch"], config["compressed"], source
);
} else if (activeTab === "tab-auth-binder"){
// FIXME: userName parsing using new URL(...) assumes a
// HTTP based repoUrl. Does it make sense to create a
// BinderHub link for SSH URLs? Then let's fix this parsing.
var userName = new URL(repoUrl).pathname.split('/')[1];
var userName = new URL(envRepoUrl).pathname.split('/')[1];
document.getElementById('binder-link').value = generateBinderUrl(
hubUrl, userName, repoName, branch, urlPath, contentRepoUrl, contentRepoBranch
hubUrl, userName, repoName, envGitBranch, urlPath, config["sourceUrl"], config["branch"], config["compressed"], source
);
}
} else {
clearLinks();
}
}

function populateFromQueryString() {
// preseed values if specified in the url
var params = new URLSearchParams(window.location.search);
Expand All @@ -213,6 +267,33 @@ function populateFromQueryString() {
}
}

function hideShowByClassName(cls, hideShow){
[].forEach.call(document.querySelectorAll(cls), function (el) {
el.style.display = hideShow;
setDisabled = (hideShow == 'none')
$(el).find("input").each(function(){
$(this).prop("disabled", setDisabled);
});
});
}


function displaySource(){
var form = document.getElementById('linkgenerator');
var source = form.querySelector('input[name="source"]:checked').value;
hideShowByClassName(".source", 'none');

if(source == 'git'){
hideShowByClassName(".source-git", '');
} else if(source == 'googledrive'){
hideShowByClassName(".source-googledrive", '');
} else if(source == 'dropbox'){
hideShowByClassName(".source-dropbox", '');
} else if(source =="generic_web"){
hideShowByClassName(".source-generic-web", '');
}
}

/**
* Main loop of the program.
*
Expand Down Expand Up @@ -246,11 +327,19 @@ function render() {
*/
function main() {
// Hook up any changes in form elements to call render()
document.querySelectorAll('#linkgenerator input[type="radio"]').forEach(
document.querySelectorAll('#linkgenerator input[name="app"]').forEach(
function (element) {
element.addEventListener('change', render);
}
)
document.querySelectorAll('#linkgenerator input[name="source"]').forEach(
function (element) {
element.addEventListener('change', function(){
displaySource();
render();
});
}
)
document.querySelectorAll('#linkgenerator input[type="text"], #linkgenerator input[type="url"]').forEach(
function (element) {
element.addEventListener('input', render);
Expand All @@ -270,6 +359,7 @@ function main() {
}

// Do an initial render, to make sure our disabled / enabled properties are correctly set
displaySource();
render();
}

Expand Down
Loading