예제 #1
0
def _revision_directory_by(revision,
                           path,
                           request_path,
                           limit=100,
                           with_data=False):
    """
    Compute the revision matching criterion's directory or content data.

    Args:
        revision: dictionary of criterions representing a revision to lookup
        path: directory's path to lookup
        request_path: request path which holds the original context to
        limit: optional query parameter to limit the revisions log
        (default to 100). For now, note that this limit could impede the
        transitivity conclusion about sha1_git not being an ancestor of
        with_data: indicate to retrieve the content's raw data if path resolves
        to a content.

    """
    def enrich_directory_local(dir, context_url=request_path):
        return utils.enrich_directory(dir, context_url)

    rev_id, result = service.lookup_directory_through_revision(
        revision, path, limit=limit, with_data=with_data)

    content = result['content']
    if result['type'] == 'dir':  # dir_entries
        result['content'] = list(map(enrich_directory_local, content))
    elif result['type'] == 'file':  # content
        result['content'] = utils.enrich_content(content)
    elif result['type'] == 'rev':  # revision
        result['content'] = utils.enrich_revision(content)

    return result
예제 #2
0
def test_enrich_content_with_hashes(api_request_factory, content):

    for algo in DEFAULT_ALGORITHMS:

        content_data = dict(content)

        query_string = "%s:%s" % (algo, content_data[algo])

        url = reverse("api-1-content", url_args={"q": query_string})
        request = api_request_factory.get(url)

        enriched_content = utils.enrich_content(content_data,
                                                query_string=query_string,
                                                request=request)

        content_data["data_url"] = reverse("api-1-content-raw",
                                           url_args={"q": query_string},
                                           request=request)

        content_data["filetype_url"] = reverse("api-1-content-filetype",
                                               url_args={"q": query_string},
                                               request=request)

        content_data["language_url"] = reverse("api-1-content-language",
                                               url_args={"q": query_string},
                                               request=request)

        content_data["license_url"] = reverse("api-1-content-license",
                                              url_args={"q": query_string},
                                              request=request)

        assert enriched_content == content_data
예제 #3
0
def api_revision_directory(request, sha1_git, dir_path=None, with_data=False):
    """
    .. http:get:: /api/1/revision/(sha1_git)/directory/[(path)/]

        Get information about directory (entry) objects associated to revisions.
        Each revision is associated to a single "root" directory.
        This endpoint behaves like :http:get:`/api/1/directory/(sha1_git)/[(path)/]`,
        but operates on the root directory associated to a given revision.

        :param string sha1_git: hexadecimal representation of the revision
            **sha1_git** identifier
        :param string path: optional parameter to get information about the
            directory entry pointed by that relative path

        {common_headers}

        :>json array content: directory entries as returned by
            :http:get:`/api/1/directory/(sha1_git)/[(path)/]`
        :>json string path: path of directory from the revision root one
        :>json string revision: the unique revision identifier
        :>json string type: the type of the directory

        :statuscode 200: no error
        :statuscode 400: an invalid **sha1_git** value has been provided
        :statuscode 404: requested revision can not be found in the archive

        **Example:**

        .. parsed-literal::

            :swh_web_api:`revision/f1b94134a4b879bc55c3dacdb496690c8ebdc03f/directory/`
    """
    rev_id, result = archive.lookup_directory_through_revision(
        {"sha1_git": sha1_git}, dir_path, with_data=with_data
    )

    content = result["content"]
    if result["type"] == "dir":  # dir_entries
        result["content"] = [
            utils.enrich_directory_entry(entry, request=request) for entry in content
        ]
    elif result["type"] == "file":  # content
        result["content"] = utils.enrich_content(content, request=request)
    elif result["type"] == "rev":  # revision
        result["content"] = utils.enrich_revision(content, request=request)

    return result
예제 #4
0
    def test_enrich_content_with_hashes_and_top_level_url(
            self, mock_django_reverse):

        for algo, hash in self.sample_content_hashes.items():

            query_string = '%s:%s' % (algo, hash)

            # given
            mock_django_reverse.side_effect = [
                '/api/content/%s/' % query_string,
                '/api/content/%s/raw/' % query_string,
                '/api/filetype/%s/' % query_string,
                '/api/language/%s/' % query_string,
                '/api/license/%s/' % query_string,
            ]

            # when
            enriched_content = utils.enrich_content({algo: hash},
                                                    top_url=True,
                                                    query_string=query_string)

            # then
            self.assertEqual(
                enriched_content, {
                    algo: hash,
                    'content_url': '/api/content/%s/' % query_string,
                    'data_url': '/api/content/%s/raw/' % query_string,
                    'filetype_url': '/api/filetype/%s/' % query_string,
                    'language_url': '/api/language/%s/' % query_string,
                    'license_url': '/api/license/%s/' % query_string,
                })

            mock_django_reverse.assert_has_calls([
                call('api-content', url_args={'q': query_string}),
                call('api-content-raw', url_args={'q': query_string}),
                call('api-content-filetype', url_args={'q': query_string}),
                call('api-content-language', url_args={'q': query_string}),
                call('api-content-license', url_args={'q': query_string}),
            ])

            mock_django_reverse.reset()
예제 #5
0
def test_api_revision_directory_ok_returns_content(api_client, archive_data,
                                                   content, person, date):
    content_path = "foo"
    _dir = Directory(entries=(DirectoryEntry(
        name=content_path.encode(),
        type="file",
        target=hash_to_bytes(content["sha1_git"]),
        perms=DentryPerms.content,
    ), ))
    archive_data.directory_add([_dir])

    revision = Revision(
        directory=_dir.id,
        author=person,
        committer=person,
        message=b"commit message",
        date=TimestampWithTimezone.from_datetime(date),
        committer_date=TimestampWithTimezone.from_datetime(date),
        synthetic=False,
        type=RevisionType.GIT,
    )
    archive_data.revision_add([revision])

    revision_id = hash_to_hex(revision.id)
    cnt_data = archive_data.content_get(content["sha1"])
    url = reverse(
        "api-1-revision-directory",
        {
            "sha1_git": revision_id,
            "dir_path": content_path
        },
    )
    rv = check_api_get_responses(api_client, url, status_code=200)

    assert rv.data == {
        "content": enrich_content(cnt_data, request=rv.wsgi_request),
        "path": content_path,
        "type": "file",
        "revision": revision_id,
    }
예제 #6
0
def test_enrich_content_without_hashes():
    assert utils.enrich_content({"id": "123"}) == {"id": "123"}
예제 #7
0
 def test_enrich_content_without_hashes(self):
     # when/then
     self.assertEqual(utils.enrich_content({'id': '123'}), {'id': '123'})