diff --git a/docs/uri-scheme-browse-identifiers.rst b/docs/uri-scheme-browse-identifiers.rst new file mode 100644 index 00000000..a5f54dbb --- /dev/null +++ b/docs/uri-scheme-browse-identifiers.rst @@ -0,0 +1,29 @@ +SWH identifiers +^^^^^^^^^^^^^^^ + +A subset of Software Heritage objects (contents, directories, releases and revisions) +can be browsed using :ref:`persistent-identifiers`. Those identifiers +are guaranteed to remain stable (persistent) over time. + +.. http:get:: /browse/(swh_id)/ + + End point to browse SWH objects using their persistent identifiers. + A redirection to the adequate HTML view will be performed when + reaching it. + + :param string swh_id: a persistent identifier for a SWH object + (see the :ref:`persistent-identifiers` section to learn more + about its syntax) + :resheader Location: the redirection URL for browsing the SWH object + associated to the provided identifier + :statuscode 302: no error + :statuscode 400: the provided identifier is malformed + + **Examples:** + + .. parsed-literal:: + + :swh_web_browse:`swh:1:cnt:0ffd12d85cdec70c88e852fc3f5ea9fd342213cd` + :swh_web_browse:`swh:1:dir:db990da9af15427455ce7836ce2b8a34b9bf67f5` + :swh_web_browse:`swh:1:rel:a9b7e3f1eada90250a6b2ab2ef3e0a846cb16831` + :swh_web_browse:`swh:1:rev:f1b94134a4b879bc55c3dacdb496690c8ebdc03f` \ No newline at end of file diff --git a/docs/uri-scheme-browse.rst b/docs/uri-scheme-browse.rst index cb245f8c..7eefa3cb 100644 --- a/docs/uri-scheme-browse.rst +++ b/docs/uri-scheme-browse.rst @@ -1,94 +1,96 @@ URI scheme for SWH Web Browse application ========================================= This web application aims to provide HTML views to easily navigate in the SWH archive, thus it needs to be reached from a web browser. If you intend to query the SWH archive programmatically through any HTTP client, please refer to the :ref:`swh-web-api-urls` section instead. Context-independent browsing ---------------------------- Context-independent URLs provide information about SWH objects (e.g., revisions, directories, contents, person, ...), independently of the contexts where they have been found (e.g., specific repositories, branches, commits, ...). The following endpoints are the same of the API case (see below), and just render the corresponding information for user consumption. Where hyperlinks are created, they always point to other context-independent user URLs: * :http:get:`/browse/content/[(algo_hash):](hash)/`: Display a SWH content * :http:get:`/browse/content/[(algo_hash):](hash)/raw/`: Get / Download SWH content raw data * :http:get:`/browse/directory/(sha1_git)/[(path)/]`: Browse the content of a SWH directory * :http:get:`/browse/person/(person_id)/`: Information on a SWH person * :http:get:`/browse/revision/(sha1_git)/`: Browse a SWH revision * :http:get:`/browse/revision/(sha1_git)/log/`: Browse history log heading to a SWH revision Context-dependent browsing -------------------------- Context-dependent URLs provide information about SWH objects, limited to specific contexts where the objects have been found. For instance, instead of having to specify a (root) revision by *sha1_git*, users might want to specify a place and a time. In SWH a "place" is an origin, with an optional branch name; a "time" is a timestamp at which some place has been observed by SWH crawlers. Wherever a revision context is expected in a path (i.e., a **/browse/revision/(sha1_git)/** path fragment) we can put in its stead a path fragment of the form **/origin/(origin_type)/url/(origin_url)/[/visit/(timestamp)/][?branch=(branch)]**. Such a fragment is resolved, internally by the SWH archive, to a *sha1_git* as follows: - if *timestamp* is absent: look for the most recent crawl of origin *origin_id* - if *timestamp* is given: look for the closest crawl of origin *origin_id* from timestamp *timestamp* - if *branch* is given as a query parameter: look for the branch *branch* - if *branch* is absent: look for branch "HEAD" or "master" - return the *sha1_git* pointed by the chosen branch The already mentioned URLs for revision contexts can therefore be alternatively specified by users as: * :http:get:`/browse/origin/(origin_type)/url/(origin_url)/directory/[(path)/]` * :http:get:`/browse/origin/(origin_type)/url/(origin_url)/visit/(timestamp)/directory/[(path)/]` * :http:get:`/browse/origin/(origin_type)/url/(origin_url)/content/(path)/` * :http:get:`/browse/origin/(origin_type)/url/(origin_url)/visit/(timestamp)/content/(path)/` * :http:get:`/browse/origin/(origin_type)/url/(origin_url)/log/` * :http:get:`/browse/origin/(origin_type)/url/(origin_url)/visit/(timestamp)/log/` Typing: - *origin_id* is given as an integer identifier, pointing into the origin table. There will be separate mechanisms for finding origins by other means (e.g., URLs, metadata, etc). - *branch* name is given as per the corresponding VCS (e.g., Git) as a query parameter to the requestes URL. - *timestamp* is given in a format as liberal as possible, to uphold the principle of least surprise. At the very minimum it is possible to enter timestamps as: - Unix epoch timestamp (see for instance the output of `date +%s`) - ISO 8601 timestamps (see for instance the output of `date -I`, `date -Is`) - YYYY[MM[DD[HH[MM[SS]]]]] ad-hoc format - YYYY[-MM[-DD[ HH:[MM:[SS:]]]]] ad-hoc format SWH Browse Urls --------------- .. include:: uri-scheme-browse-content.rst .. include:: uri-scheme-browse-directory.rst .. include:: uri-scheme-browse-origin.rst .. include:: uri-scheme-browse-person.rst .. include:: uri-scheme-browse-revision.rst -.. include:: uri-scheme-browse-release.rst \ No newline at end of file +.. include:: uri-scheme-browse-release.rst + +.. include:: uri-scheme-browse-identifiers.rst diff --git a/swh/web/browse/urls.py b/swh/web/browse/urls.py index a7333c9a..376f3fa2 100644 --- a/swh/web/browse/urls.py +++ b/swh/web/browse/urls.py @@ -1,37 +1,38 @@ # Copyright (C) 2017-2018 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from django.conf.urls import url from django.shortcuts import render import swh.web.browse.views.directory # noqa import swh.web.browse.views.content # noqa +import swh.web.browse.views.identifiers # noqa import swh.web.browse.views.origin # noqa import swh.web.browse.views.person # noqa import swh.web.browse.views.release # noqa import swh.web.browse.views.revision # noqa from swh.web.browse.browseurls import BrowseUrls def default_browse_view(request): """Default django view used as an entry point for the swh browse ui web application. The url that point to it is /browse/. Args: request: input django http request """ return render(request, 'person.html', {'heading': 'Browse the Software Heritage archive', 'empty_browse': True}) urlpatterns = [ url(r'^$', default_browse_view, name='browse-homepage') ] urlpatterns += BrowseUrls.get_url_patterns() diff --git a/swh/web/browse/views/identifiers.py b/swh/web/browse/views/identifiers.py new file mode 100644 index 00000000..47a96617 --- /dev/null +++ b/swh/web/browse/views/identifiers.py @@ -0,0 +1,59 @@ +# Copyright (C) 2017-2018 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from django.shortcuts import redirect + +from swh.model.identifiers import parse_persistent_identifier + +from swh.web.browse.browseurls import browse_route +from swh.web.common.utils import reverse +from swh.web.common.exc import BadInputExc, handle_view_exception + + +@browse_route(r'(?Pswh:[0-9]+:[a-z]+:[0-9a-f]+)/', + view_name='browse-swh-id') +def swh_id_browse(request, swh_id): + """ + Django view enabling to browse the SWH archive using + :ref:`persistent-identifiers`. + + The url that points to it is :http:get:`/browse/(swh_id)/`. + + Args: + swh_id: A SWH persistent identifier + + Returns: + A redirection to the adequate view to browse the + requested SWH object + """ + try: + swh_id_parsed = parse_persistent_identifier(swh_id) + object_type = swh_id_parsed['object_type'] + object_id = swh_id_parsed['object_id'] + view_url = None + if object_type == 'cnt': + query_string = 'sha1_git:' + object_id + view_url = reverse('browse-content', + kwargs={'query_string': query_string}, + query_params=request.GET) + elif object_type == 'dir': + view_url = reverse('browse-directory', + kwargs={'sha1_git': object_id}, + query_params=request.GET) + elif object_type == 'rel': + view_url = reverse('browse-release', + kwargs={'sha1_git': object_id}, + query_params=request.GET) + elif object_type == 'rev': + view_url = reverse('browse-revision', + kwargs={'sha1_git': object_id}, + query_params=request.GET) + else: + msg = '\'%s\' is not a valid SWH persistent identifier!' % swh_id + raise BadInputExc(msg) + except Exception as exc: + return handle_view_exception(request, exc) + + return redirect(view_url) diff --git a/swh/web/tests/browse/views/test_identifiers.py b/swh/web/tests/browse/views/test_identifiers.py new file mode 100644 index 00000000..cde7b1f9 --- /dev/null +++ b/swh/web/tests/browse/views/test_identifiers.py @@ -0,0 +1,125 @@ +# Copyright (C) 2018 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +# flake8: noqa + +from unittest.mock import patch +from nose.tools import istest +from django.test import TestCase + +from swh.web.common.exc import BadInputExc +from swh.web.common.utils import reverse +from swh.web.tests.testbase import SWHWebTestBase + +from .data.content_test_data import stub_content_text_data + +from .data.directory_test_data import stub_root_directory_sha1 + +from .data.revision_test_data import revision_id_test + +from .data.release_test_data import stub_release + +swh_id_prefix = 'swh:1:' + + +class SwhBrowseIdTest(SWHWebTestBase, TestCase): + + @istest + def content_id_browse(self): + cnt_sha1_git = stub_content_text_data['checksums']['sha1_git'] + swh_id = swh_id_prefix + 'cnt:' + cnt_sha1_git + url = reverse('browse-swh-id', + kwargs={'swh_id': swh_id}) + + query_string = 'sha1_git:' + cnt_sha1_git + content_browse_url = reverse('browse-content', + kwargs={'query_string': query_string}) + + resp = self.client.get(url) + + self.assertEquals(resp.status_code, 302) + self.assertEqual(resp['location'], content_browse_url) + + @istest + def directory_id_browse(self): + swh_id = swh_id_prefix + 'dir:' + stub_root_directory_sha1 + url = reverse('browse-swh-id', + kwargs={'swh_id': swh_id}) + + directory_browse_url = reverse('browse-directory', + kwargs={'sha1_git': stub_root_directory_sha1}) + + resp = self.client.get(url) + + self.assertEquals(resp.status_code, 302) + self.assertEqual(resp['location'], directory_browse_url) + + @istest + def revision_id_browse(self): + swh_id = swh_id_prefix + 'rev:' + revision_id_test + url = reverse('browse-swh-id', + kwargs={'swh_id': swh_id}) + + revision_browse_url = reverse('browse-revision', + kwargs={'sha1_git': revision_id_test}) + + resp = self.client.get(url) + + self.assertEquals(resp.status_code, 302) + self.assertEqual(resp['location'], revision_browse_url) + + query_params = {'origin_type': 'git', + 'origin_url': 'https://github.com/webpack/webpack'} + + url = reverse('browse-swh-id', + kwargs={'swh_id': swh_id}, + query_params=query_params) + + revision_browse_url = reverse('browse-revision', + kwargs={'sha1_git': revision_id_test}, + query_params=query_params) + + resp = self.client.get(url) + self.assertEquals(resp.status_code, 302) + self.assertEqual(resp['location'], revision_browse_url) + + @istest + def release_id_browse(self): + swh_id = swh_id_prefix + 'rel:' + stub_release['id'] + url = reverse('browse-swh-id', + kwargs={'swh_id': swh_id}) + + release_browse_url = reverse('browse-release', + kwargs={'sha1_git': stub_release['id']}) + + resp = self.client.get(url) + + self.assertEquals(resp.status_code, 302) + self.assertEqual(resp['location'], release_browse_url) + + query_params = {'origin_type': 'git', + 'origin_url': 'https://github.com/python/cpython'} + + url = reverse('browse-swh-id', + kwargs={'swh_id': swh_id}, + query_params=query_params) + + release_browse_url = reverse('browse-release', + kwargs={'sha1_git': stub_release['id']}, + query_params=query_params) + + resp = self.client.get(url) + self.assertEquals(resp.status_code, 302) + self.assertEqual(resp['location'], release_browse_url) + + @istest + def bad_id_browse(self): + swh_id = swh_id_prefix + 'foo:' + stub_release['id'] + url = reverse('browse-swh-id', + kwargs={'swh_id': swh_id}) + + resp = self.client.get(url) + self.assertEquals(resp.status_code, 400) +