Skip to content
Snippets Groups Projects
Commit 2290aab7 authored by Antoine Lambert's avatar Antoine Lambert
Browse files

browse: Support resolution of SWH persistent identifiers

Closes T926
parent 0ed611fb
No related branches found
No related tags found
No related merge requests found
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
......@@ -91,4 +91,6 @@ SWH Browse Urls
.. 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
......@@ -8,6 +8,7 @@ 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
......
# 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'(?P<swh_id>swh:[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)
# 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment