Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/revision.py
# Copyright (C) 2015-2019 The Software Heritage developers | # Copyright (C) 2015-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from typing import Optional | |||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from rest_framework.request import Request | |||||
from swh.web.api import utils | from swh.web.api import utils | ||||
from swh.web.api.apidoc import api_doc, format_docstring | from swh.web.api.apidoc import api_doc, format_docstring | ||||
from swh.web.api.apiurls import api_route | from swh.web.api.apiurls import api_route | ||||
from swh.web.api.views.utils import api_lookup | from swh.web.api.views.utils import api_lookup | ||||
from swh.web.common import archive | from swh.web.common import archive | ||||
DOC_RETURN_REVISION = """ | DOC_RETURN_REVISION = """ | ||||
Show All 21 Lines | |||||
DOC_RETURN_REVISION_ARRAY = DOC_RETURN_REVISION.replace(":>json", ":>jsonarr") | DOC_RETURN_REVISION_ARRAY = DOC_RETURN_REVISION.replace(":>json", ":>jsonarr") | ||||
@api_route( | @api_route( | ||||
r"/revision/(?P<sha1_git>[0-9a-f]+)/", "api-1-revision", checksum_args=["sha1_git"] | r"/revision/(?P<sha1_git>[0-9a-f]+)/", "api-1-revision", checksum_args=["sha1_git"] | ||||
) | ) | ||||
@api_doc("/revision/") | @api_doc("/revision/") | ||||
@format_docstring(return_revision=DOC_RETURN_REVISION) | @format_docstring(return_revision=DOC_RETURN_REVISION) | ||||
def api_revision(request, sha1_git): | def api_revision(request: Request, sha1_git: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/revision/(sha1_git)/ | .. http:get:: /api/1/revision/(sha1_git)/ | ||||
Get information about a revision in the archive. Revisions are | Get information about a revision in the archive. Revisions are | ||||
identified by **sha1** checksums, compatible with Git commit | identified by **sha1** checksums, compatible with Git commit | ||||
identifiers. | identifiers. | ||||
See :func:`swh.model.git_objects.revision_git_object` in our data model | See :func:`swh.model.git_objects.revision_git_object` in our data model | ||||
module for details about how they are computed. | module for details about how they are computed. | ||||
Show All 25 Lines | |||||
@api_route( | @api_route( | ||||
r"/revision/(?P<sha1_git>[0-9a-f]+)/raw/", | r"/revision/(?P<sha1_git>[0-9a-f]+)/raw/", | ||||
"api-1-revision-raw-message", | "api-1-revision-raw-message", | ||||
checksum_args=["sha1_git"], | checksum_args=["sha1_git"], | ||||
) | ) | ||||
@api_doc("/revision/raw/", tags=["hidden"]) | @api_doc("/revision/raw/", tags=["hidden"]) | ||||
def api_revision_raw_message(request, sha1_git): | def api_revision_raw_message(request: Request, sha1_git: str): | ||||
"""Return the raw data of the message of revision identified by sha1_git""" | """Return the raw data of the message of revision identified by sha1_git""" | ||||
raw = archive.lookup_revision_message(sha1_git) | raw = archive.lookup_revision_message(sha1_git) | ||||
response = HttpResponse(raw["message"], content_type="application/octet-stream") | response = HttpResponse(raw["message"], content_type="application/octet-stream") | ||||
response["Content-disposition"] = "attachment;filename=rev_%s_raw" % sha1_git | response["Content-disposition"] = "attachment;filename=rev_%s_raw" % sha1_git | ||||
return response | return response | ||||
@api_route( | @api_route( | ||||
r"/revision/(?P<sha1_git>[0-9a-f]+)/directory/", | r"/revision/(?P<sha1_git>[0-9a-f]+)/directory/", | ||||
"api-1-revision-directory", | "api-1-revision-directory", | ||||
checksum_args=["sha1_git"], | checksum_args=["sha1_git"], | ||||
) | ) | ||||
@api_route( | @api_route( | ||||
r"/revision/(?P<sha1_git>[0-9a-f]+)/directory/(?P<dir_path>.+)/", | r"/revision/(?P<sha1_git>[0-9a-f]+)/directory/(?P<dir_path>.+)/", | ||||
"api-1-revision-directory", | "api-1-revision-directory", | ||||
checksum_args=["sha1_git"], | checksum_args=["sha1_git"], | ||||
) | ) | ||||
@api_doc("/revision/directory/") | @api_doc("/revision/directory/") | ||||
@format_docstring() | @format_docstring() | ||||
def api_revision_directory(request, sha1_git, dir_path=None, with_data=False): | def api_revision_directory( | ||||
request: Request, sha1_git: str, dir_path: Optional[str] = None | |||||
): | |||||
""" | """ | ||||
.. http:get:: /api/1/revision/(sha1_git)/directory/[(path)/] | .. http:get:: /api/1/revision/(sha1_git)/directory/[(path)/] | ||||
Get information about directory (entry) objects associated to revisions. | Get information about directory (entry) objects associated to revisions. | ||||
Each revision is associated to a single "root" directory. | Each revision is associated to a single "root" directory. | ||||
This endpoint behaves like :http:get:`/api/1/directory/(sha1_git)/[(path)/]`, | This endpoint behaves like :http:get:`/api/1/directory/(sha1_git)/[(path)/]`, | ||||
but operates on the root directory associated to a given revision. | but operates on the root directory associated to a given revision. | ||||
Show All 16 Lines | .. http:get:: /api/1/revision/(sha1_git)/directory/[(path)/] | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`revision/f1b94134a4b879bc55c3dacdb496690c8ebdc03f/directory/` | :swh_web_api:`revision/f1b94134a4b879bc55c3dacdb496690c8ebdc03f/directory/` | ||||
""" | """ | ||||
rev_id, result = archive.lookup_directory_through_revision( | rev_id, result = archive.lookup_directory_through_revision( | ||||
{"sha1_git": sha1_git}, dir_path, with_data=with_data | {"sha1_git": sha1_git}, dir_path | ||||
) | ) | ||||
content = result["content"] | content = result["content"] | ||||
if result["type"] == "dir": # dir_entries | if result["type"] == "dir": # dir_entries | ||||
result["content"] = [ | result["content"] = [ | ||||
utils.enrich_directory_entry(entry, request=request) for entry in content | utils.enrich_directory_entry(entry, request=request) for entry in content | ||||
] | ] | ||||
elif result["type"] == "file": # content | elif result["type"] == "file": # content | ||||
result["content"] = utils.enrich_content(content, request=request) | result["content"] = utils.enrich_content(content, request=request) | ||||
elif result["type"] == "rev": # revision | elif result["type"] == "rev": # revision | ||||
result["content"] = utils.enrich_revision(content, request=request) | result["content"] = utils.enrich_revision(content, request=request) | ||||
return result | return result | ||||
@api_route( | @api_route( | ||||
r"/revision/(?P<sha1_git>[0-9a-f]+)/log/", | r"/revision/(?P<sha1_git>[0-9a-f]+)/log/", | ||||
"api-1-revision-log", | "api-1-revision-log", | ||||
checksum_args=["sha1_git"], | checksum_args=["sha1_git"], | ||||
) | ) | ||||
@api_doc("/revision/log/") | @api_doc("/revision/log/") | ||||
@format_docstring(return_revision_array=DOC_RETURN_REVISION_ARRAY) | @format_docstring(return_revision_array=DOC_RETURN_REVISION_ARRAY) | ||||
def api_revision_log(request, sha1_git): | def api_revision_log(request: Request, sha1_git: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/revision/(sha1_git)/log/ | .. http:get:: /api/1/revision/(sha1_git)/log/ | ||||
Get a list of all revisions heading to a given one, in other words show | Get a list of all revisions heading to a given one, in other words show | ||||
the commit log. | the commit log. | ||||
The revisions are returned in the breadth-first search order while | The revisions are returned in the breadth-first search order while | ||||
visiting the revision graph. The number of revisions to return is also | visiting the revision graph. The number of revisions to return is also | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |