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 a Software Heritage provider #1256

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion binderhub/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from .repoproviders import (GitHubRepoProvider, GitRepoProvider,
GitLabRepoProvider, GistRepoProvider,
ZenodoProvider, FigshareProvider, HydroshareProvider,
DataverseProvider)
DataverseProvider, SWHIDProvider)
from .metrics import MetricsHandler

from .utils import ByteSpecification, url_path_join
Expand Down Expand Up @@ -465,6 +465,7 @@ def _add_slash(self, proposal):
'figshare': FigshareProvider,
'hydroshare': HydroshareProvider,
'dataverse': DataverseProvider,
'swh': SWHIDProvider,
},
config=True,
help="""
Expand Down
3 changes: 2 additions & 1 deletion binderhub/event-schemas/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"Zenodo",
"Figshare",
"Hydroshare",
"Dataverse"
"Dataverse",
"SWHID"
],
"description": "Provider for the repository being launched"
},
Expand Down
1 change: 1 addition & 0 deletions binderhub/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"figshare": "Figshare",
"hydroshare": "Hydroshare",
"dataverse": "Dataverse",
"swh": "Software Heritage",
}


Expand Down
38 changes: 38 additions & 0 deletions binderhub/repoproviders.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,41 @@ async def get_resolved_spec(self):

def get_build_slug(self):
return self.gist_id


class SWHIDProvider(RepoProvider):
"""Provide contents of a SWHID record

Users must provide a spec consisting of the SWHID of a directory or revision.
"""
name = Unicode("SWHID")

async def get_resolved_ref(self):
client = AsyncHTTPClient()
req = HTTPRequest("https://archive.softwareheritage.org/api/1/known/",
method="POST",
headers={'content-type': 'application/json'},
body=json.dumps([self.spec]),
user_agent="BinderHub")
r = await client.fetch(req)
r.rethrow()

response = json.loads(r.body)
if response[self.spec]["known"]:
return self.spec
raise RuntimeError(f"Unknown SWHID {self.spec}")


async def get_resolved_spec(self):
return self.spec

def get_repo_url(self):
# While called repo URL, the return value of this function is passed
# as argument to repo2docker, hence we return the spec as is.
return self.spec

async def get_resolved_ref_url(self):
return self.spec

def get_build_slug(self):
return "swh-{}".format(self.spec)
2 changes: 1 addition & 1 deletion binderhub/static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function getBuildFormValues() {

var ref = $('#ref').val().trim() || $("#ref").attr("placeholder");
if (providerPrefix === 'zenodo' || providerPrefix === 'figshare' || providerPrefix === 'dataverse' ||
providerPrefix === 'hydroshare') {
providerPrefix === 'hydroshare' || providerPrefix === 'swh') {
ref = "";
}
var path = $('#filepath').val().trim();
Expand Down
25 changes: 25 additions & 0 deletions binderhub/tests/test_repoproviders.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
GitLabRepoProvider,
GitRepoProvider,
HydroshareProvider,
SWHIDProvider,
ZenodoProvider,
strip_suffix,
tokenize_spec,
Expand Down Expand Up @@ -490,3 +491,27 @@ def test_gist_secret():

provider = GistRepoProvider(spec=spec, allow_secret_gist=True)
assert IOLoop().run_sync(provider.get_resolved_ref) is not None


@pytest.mark.parametrize('spec,resolved_spec,resolved_ref,resolved_ref_url,build_slug', [
['swh:1:rev:c30614ec4587418fb264efb466cba58991029f16',
'swh:1:rev:c30614ec4587418fb264efb466cba58991029f16',
'swh:1:rev:c30614ec4587418fb264efb466cba58991029f16',
'swh:1:rev:c30614ec4587418fb264efb466cba58991029f16',
'swh-swh:1:rev:c30614ec4587418fb264efb466cba58991029f16'],
])
async def test_swh(spec, resolved_spec, resolved_ref, resolved_ref_url, build_slug):
provider = SWHIDProvider(spec=spec)

# have to resolve the ref first
ref = await provider.get_resolved_ref()
assert ref == resolved_ref

slug = provider.get_build_slug()
assert slug == build_slug
repo_url = provider.get_repo_url()
assert repo_url == spec
ref_url = await provider.get_resolved_ref_url()
assert ref_url == resolved_ref_url
spec = await provider.get_resolved_spec()
assert spec == resolved_spec
6 changes: 6 additions & 0 deletions doc/developer/repoproviders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ Currently supported providers, their prefixes and specs are:
+------------+--------------------+-------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Git | ``git`` | ``<url-escaped-url>/<commit-sha>`` | A generic repository provider for URLs that point directly to a git repository. |
+------------+--------------------+-------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| SWHID | ``swh`` | ``<SWHID>`` | A `Software Heritage persistent identifier`_. |
+------------+--------------------+-------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------+


.. _`Software Heritage persistent identifier`: https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html#persistent-identifiers


Adding a new repository provider
================================
Expand Down
7 changes: 7 additions & 0 deletions doc/reference/repoproviders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@ Module: :mod:`binderhub.repoproviders`

.. autoconfigurable:: GitRepoProvider
:members:


:class:`SWHIDProvider`
---------------------------

.. autoconfigurable:: SWHIDProvider
:members: