From 56d619580a93e3e8eb86219b99269939d16401f8 Mon Sep 17 00:00:00 2001 From: JEAN REGIS <190766850+Jean-Regis-M@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:11:22 +0300 Subject: [PATCH] Fix repository name parsing in commit_export create_pull_request used str.rstrip(".git") to drop a trailing ".git" from the export repository URL. rstrip removes any trailing run of the characters ".", "g", "i" and "t", so a repository such as "aboutcode-org/vcio-export" was parsed as "vcio-expor" and the GitHub API request targeted a repository that does not exist. Use str.removesuffix(".git") instead and add regression tests for create_pull_request. Signed-off-by: JEAN REGIS <190766850+Jean-Regis-M@users.noreply.github.com> --- .../management/commands/commit_export.py | 2 +- .../tests/test_commit_export_command.py | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 vulnerabilities/tests/test_commit_export_command.py diff --git a/vulnerabilities/management/commands/commit_export.py b/vulnerabilities/management/commands/commit_export.py index 9d47904f3..27f6ab64f 100644 --- a/vulnerabilities/management/commands/commit_export.py +++ b/vulnerabilities/management/commands/commit_export.py @@ -152,7 +152,7 @@ def create_pull_request(self, repo_url, branch, title, body, token): """Create a pull request in the GitHub repository.""" url_parts = urlparse(repo_url).path - path_parts = url_parts.strip("/").rstrip(".git").split("/") + path_parts = url_parts.strip("/").removesuffix(".git").split("/") if len(path_parts) >= 2: repo_owner = path_parts[0] diff --git a/vulnerabilities/tests/test_commit_export_command.py b/vulnerabilities/tests/test_commit_export_command.py new file mode 100644 index 000000000..d34cabf26 --- /dev/null +++ b/vulnerabilities/tests/test_commit_export_command.py @@ -0,0 +1,74 @@ +# +# Copyright (c) nexB Inc. and others. All rights reserved. +# VulnerableCode is a trademark of nexB Inc. +# SPDX-License-Identifier: Apache-2.0 +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. +# See https://github.com/aboutcode-org/vulnerablecode for support or download. +# See https://aboutcode.org for more information about nexB OSS projects. +# + +from unittest.mock import MagicMock +from unittest.mock import patch + +import pytest + +from vulnerabilities.management.commands.commit_export import Command + + +@pytest.mark.parametrize( + "repo_url, expected_api_url", + [ + ( + "https://github.com/aboutcode-org/vulnerablecode-data", + "https://api.github.com/repos/aboutcode-org/vulnerablecode-data/pulls", + ), + ( + "https://github.com/aboutcode-org/vulnerablecode-data.git", + "https://api.github.com/repos/aboutcode-org/vulnerablecode-data/pulls", + ), + ( + "https://github.com/aboutcode-org/vulnerablecode-data/", + "https://api.github.com/repos/aboutcode-org/vulnerablecode-data/pulls", + ), + # Repository names ending with any of the ".git" characters must not be truncated. + ( + "https://github.com/aboutcode-org/vcio-export", + "https://api.github.com/repos/aboutcode-org/vcio-export/pulls", + ), + ( + "https://github.com/aboutcode-org/digit.git", + "https://api.github.com/repos/aboutcode-org/digit/pulls", + ), + ( + "https://github.com/aboutcode-org/test.git/", + "https://api.github.com/repos/aboutcode-org/test/pulls", + ), + ], +) +@patch("vulnerabilities.management.commands.commit_export.requests.post") +def test_create_pull_request_uses_correct_repository(mock_post, repo_url, expected_api_url): + mock_post.return_value = MagicMock( + status_code=201, json=lambda: {"html_url": "https://example.com/pr/1"} + ) + + Command().create_pull_request( + repo_url=repo_url, + branch="export-update", + title="title", + body="body", + token="token", + ) + + mock_post.assert_called_once() + assert mock_post.call_args.args[0] == expected_api_url + + +def test_create_pull_request_rejects_url_without_owner_and_repo(): + with pytest.raises(ValueError): + Command().create_pull_request( + repo_url="https://github.com/aboutcode-org", + branch="export-update", + title="title", + body="body", + token="token", + )