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

Add support for Bitbucket repositories #1100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions binderhub/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from .registry import DockerRegistry
from .main import MainHandler, ParameterizedMainHandler, LegacyRedirectHandler
from .repoproviders import (GitHubRepoProvider, GitRepoProvider,
BitbucketRepoProvider,
GitLabRepoProvider, GistRepoProvider,
ZenodoProvider, FigshareProvider, HydroshareProvider,
DataverseProvider)
Expand Down Expand Up @@ -399,6 +400,7 @@ def _add_slash(self, proposal):
repo_providers = Dict(
{
'gh': GitHubRepoProvider,
'bb': BitbucketRepoProvider,
'gist': GistRepoProvider,
'git': GitRepoProvider,
'gl': GitLabRepoProvider,
Expand Down
1 change: 1 addition & 0 deletions binderhub/event-schemas/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"provider": {
"enum": [
"GitHub",
"Bitbucket",
"Gist",
"GitLab",
"Git",
Expand Down
1 change: 1 addition & 0 deletions binderhub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

SPEC_NAMES = {
"gh": "GitHub",
"bb": "Bitbucket",
"gist": "Gist",
"gl": "GitLab",
"git": "Git repo",
Expand Down
73 changes: 73 additions & 0 deletions binderhub/repoproviders.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,76 @@ async def get_resolved_spec(self):

def get_build_slug(self):
return self.gist_id



# For Bitbucket

class BitbucketRepoProvider(RepoProvider):
"""Repo provider for Bitbucket sevices.

Users must provide a spec that matches the following form.

[https://bitbucket.org/]<repository>[/<ref>]

<url-escaped-namespace>/<unresolved_ref>
<url-escaped-namespace>/<resolved_ref>

The ref is optional, valid values are
- a full sha1 of a ref in the history
- master

If master or no ref is specified the latest revision will be used.
"""

name = Unicode("Bitbucket")

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.url, unresolved_ref = self.spec.split('/', 1)
self.repo = urllib.parse.unquote(self.url)
self.unresolved_ref = urllib.parse.unquote(unresolved_ref)
if not self.unresolved_ref:
raise ValueError("`unresolved_ref` must be specified as a query parameter for Bitbucket provider")

@gen.coroutine
def get_resolved_ref(self):
if hasattr(self, 'resolved_ref'):
return self.resolved_ref

try:
# Check if the reference is a valid SHA hash
self.sha1_validate(self.unresolved_ref)
except ValueError:
# The ref is a head/tag and we resolve it using `git ls-remote`
command = ["git", "ls-remote", self.repo, self.unresolved_ref]
result = subprocess.run(command, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode:
raise RuntimeError("Unable to run git ls-remote to get the `resolved_ref`: {}".format(result.stderr))
if not result.stdout:
raise ValueError("The specified branch, tag or commit SHA ('{}') was not found on the remote repository."
.format(self.unresolved_ref))
resolved_ref = result.stdout.split(None, 1)[0]
self.sha1_validate(resolved_ref)
self.resolved_ref = resolved_ref
else:
# The ref already was a valid SHA hash
self.resolved_ref = self.unresolved_ref

return self.resolved_ref

async def get_resolved_spec(self):
if not hasattr(self, 'resolved_ref'):
self.resolved_ref = await self.get_resolved_ref()
return f"{self.url}/{self.resolved_ref}"

def get_repo_url(self):
return self.repo

async def get_resolved_ref_url(self):
# not possible to construct ref url
return self.get_repo_url()

def get_build_slug(self):
return self.repo

9 changes: 8 additions & 1 deletion binderhub/static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ function updateRepoText() {
$("label[for=ref]").prop("disabled", false);
if (provider === "gh") {
text = "GitHub repository name or URL";
} else if (provider === "gl") {
}

else if (provider === "bb") {
text = "Bitbucket repository name or URL";
tag_text = "Bitbucket branch, tag, or commit SHA";
}

else if (provider === "gl") {
text = "GitLab.com repository or URL";
}
else if (provider === "gist") {
Expand Down
1 change: 1 addition & 0 deletions binderhub/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ <h4 id="form-header" class='row'>Build and launch a repository</h4>
</button>
<ul class="dropdown-menu" id="provider_prefix_sel">
<li class="dropdown-item" value="gh"><a href="#">GitHub</a></li>
<li class="dropdown-item" value="bb"><a href="#">Bitbucket</a></li>
<li class="dropdown-item" value="gist"><a href="#">Gist</a></li>
<li class="dropdown-item" value="gl"><a href="#">GitLab.com</a></li>
<li class="dropdown-item" value="git"><a href="#">Git repository</a></li>
Expand Down