From 83e04f7f8d04cebdc6785600f56e8cbe3974bc07 Mon Sep 17 00:00:00 2001 From: Pablo Tamarit Date: Fri, 6 Sep 2024 15:27:22 +0200 Subject: [PATCH] api: fix set alternate zipball URL when tag and branch having same name --- invenio_github/api.py | 5 ++++- tests/conftest.py | 15 +++++++++++++-- tests/test_api.py | 42 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/invenio_github/api.py b/invenio_github/api.py index 20f9f76..d53e47c 100644 --- a/invenio_github/api.py +++ b/invenio_github/api.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2023 CERN. +# Copyright (C) 2023-2024 CERN. # # Invenio is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License as @@ -623,6 +623,9 @@ def test_zipball(self): response.status_code == 200 ), f"Could not retrieve archive from GitHub: {zipball_url}" + # Overwrite the original URL with the newly found more specific URL. + self.release_payload["zipball_url"] = zipball_url + # High level API def release_failed(self): diff --git a/tests/conftest.py b/tests/conftest.py index 7f065fb..829d7e6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Invenio. -# Copyright (C) 2023 CERN. +# Copyright (C) 2023-2024 CERN. # # Invenio is free software; you can redistribute it # and/or modify it under the terms of the GNU General Public License as @@ -286,10 +286,21 @@ def mock_repo_with_id(id): def mock_repo_by_name(owner, name): return repos_by_name.get("/".join((owner, name))) + def mock_head_status_by_repo_url(url, **kwargs): + url_specific_refs_tags = ( + "https://github.com/auser/repo-2/zipball/refs/tags/v1.0-tag-and-branch" + ) + if url.endswith("v1.0-tag-and-branch") and url != url_specific_refs_tags: + return MagicMock( + status_code=300, links={"alternate": {"url": url_specific_refs_tags}} + ) + else: + return MagicMock(status_code=200) + mock_api.repository_with_id.side_effect = mock_repo_with_id mock_api.repository.side_effect = mock_repo_by_name mock_api.markdown.side_effect = lambda x: x - mock_api.session.head.return_value = MagicMock(status_code=200) + mock_api.session.head.side_effect = mock_head_status_by_repo_url mock_api.session.get.return_value = MagicMock(raw=ZIPBALL()) with patch("invenio_github.api.GitHubAPI.api", new=mock_api): diff --git a/tests/test_api.py b/tests/test_api.py index 96582f4..0347e45 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2023 CERN. +# Copyright (C) 2023-2024 CERN. # # Invenio-Github is free software; you can redistribute it and/or modify # it under the terms of the MIT License; see LICENSE file for more details. @@ -78,3 +78,43 @@ def test_release_api(app, test_user, github_api): assert valid_remote_file_contents is not None assert valid_remote_file_contents.decoded["name"] == "test.py" + + +def test_test_zipball(app, test_user, github_api): + api = GitHubAPI(test_user.id) + api.init_account() + repo_id = 2 + repo_name = "repo-2" + + # Create a repo hook + hook_created = api.create_hook(repo_id=repo_id, repo_name=repo_name) + assert hook_created + + headers = [("Content-Type", "application/json")] + + payload = github_payload_fixture( + "auser", repo_name, repo_id, tag="v1.0-tag-and-branch" + ) + with app.test_request_context(headers=headers, data=json.dumps(payload)): + event = Event.create( + receiver_id="github", + user_id=test_user.id, + ) + release = Release( + release_id=payload["release"]["id"], + tag=event.payload["release"]["tag_name"], + repository_id=repo_id, + event=event, + status=ReleaseStatus.RECEIVED, + ) + # Idea is to test the public interface of GithubRelease + gh = GitHubRelease(release) + + # Call the method fixing the zipball URL + gh.test_zipball() + + # Check that the zipball URL is the alternate URL specific for tags + assert ( + gh.release_zipball_url + == "https://github.com/auser/repo-2/zipball/refs/tags/v1.0-tag-and-branch" + )