Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vulnerabilities/management/commands/commit_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
74 changes: 74 additions & 0 deletions vulnerabilities/tests/test_commit_export_command.py
Original file line number Diff line number Diff line change
@@ -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",
)