Skip to content

Commit 83e04f7

Browse files
committed
api: fix set alternate zipball URL when tag and branch having same name
1 parent f98ade2 commit 83e04f7

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

invenio_github/api.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
#
33
# This file is part of Invenio.
4-
# Copyright (C) 2023 CERN.
4+
# Copyright (C) 2023-2024 CERN.
55
#
66
# Invenio is free software; you can redistribute it
77
# and/or modify it under the terms of the GNU General Public License as
@@ -623,6 +623,9 @@ def test_zipball(self):
623623
response.status_code == 200
624624
), f"Could not retrieve archive from GitHub: {zipball_url}"
625625

626+
# Overwrite the original URL with the newly found more specific URL.
627+
self.release_payload["zipball_url"] = zipball_url
628+
626629
# High level API
627630

628631
def release_failed(self):

tests/conftest.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
#
33
# This file is part of Invenio.
4-
# Copyright (C) 2023 CERN.
4+
# Copyright (C) 2023-2024 CERN.
55
#
66
# Invenio is free software; you can redistribute it
77
# and/or modify it under the terms of the GNU General Public License as
@@ -286,10 +286,21 @@ def mock_repo_with_id(id):
286286
def mock_repo_by_name(owner, name):
287287
return repos_by_name.get("/".join((owner, name)))
288288

289+
def mock_head_status_by_repo_url(url, **kwargs):
290+
url_specific_refs_tags = (
291+
"https://github.com/auser/repo-2/zipball/refs/tags/v1.0-tag-and-branch"
292+
)
293+
if url.endswith("v1.0-tag-and-branch") and url != url_specific_refs_tags:
294+
return MagicMock(
295+
status_code=300, links={"alternate": {"url": url_specific_refs_tags}}
296+
)
297+
else:
298+
return MagicMock(status_code=200)
299+
289300
mock_api.repository_with_id.side_effect = mock_repo_with_id
290301
mock_api.repository.side_effect = mock_repo_by_name
291302
mock_api.markdown.side_effect = lambda x: x
292-
mock_api.session.head.return_value = MagicMock(status_code=200)
303+
mock_api.session.head.side_effect = mock_head_status_by_repo_url
293304
mock_api.session.get.return_value = MagicMock(raw=ZIPBALL())
294305

295306
with patch("invenio_github.api.GitHubAPI.api", new=mock_api):

tests/test_api.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2023 CERN.
3+
# Copyright (C) 2023-2024 CERN.
44
#
55
# Invenio-Github is free software; you can redistribute it and/or modify
66
# 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):
7878

7979
assert valid_remote_file_contents is not None
8080
assert valid_remote_file_contents.decoded["name"] == "test.py"
81+
82+
83+
def test_test_zipball(app, test_user, github_api):
84+
api = GitHubAPI(test_user.id)
85+
api.init_account()
86+
repo_id = 2
87+
repo_name = "repo-2"
88+
89+
# Create a repo hook
90+
hook_created = api.create_hook(repo_id=repo_id, repo_name=repo_name)
91+
assert hook_created
92+
93+
headers = [("Content-Type", "application/json")]
94+
95+
payload = github_payload_fixture(
96+
"auser", repo_name, repo_id, tag="v1.0-tag-and-branch"
97+
)
98+
with app.test_request_context(headers=headers, data=json.dumps(payload)):
99+
event = Event.create(
100+
receiver_id="github",
101+
user_id=test_user.id,
102+
)
103+
release = Release(
104+
release_id=payload["release"]["id"],
105+
tag=event.payload["release"]["tag_name"],
106+
repository_id=repo_id,
107+
event=event,
108+
status=ReleaseStatus.RECEIVED,
109+
)
110+
# Idea is to test the public interface of GithubRelease
111+
gh = GitHubRelease(release)
112+
113+
# Call the method fixing the zipball URL
114+
gh.test_zipball()
115+
116+
# Check that the zipball URL is the alternate URL specific for tags
117+
assert (
118+
gh.release_zipball_url
119+
== "https://github.com/auser/repo-2/zipball/refs/tags/v1.0-tag-and-branch"
120+
)

0 commit comments

Comments
 (0)