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
55 changes: 55 additions & 0 deletions application/tests/git_utils_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import unittest
from unittest.mock import MagicMock, patch

from application.utils.git import createPullRequest


class TestGitUtils(unittest.TestCase):
@patch("application.utils.git.Github")
def test_create_pull_request_uses_target_branch(self, mocked_github) -> None:
github_client = MagicMock()
repo = MagicMock()
mocked_github.return_value = github_client
github_client.get_repo.return_value = repo

createPullRequest(
apiToken="token",
repo="OWASP/OpenCRE",
title="test-title",
srcBranch="feature-branch",
targetBranch="main",
)

mocked_github.assert_called_once_with("token")
github_client.get_repo.assert_called_once_with("OWASP/OpenCRE")
repo.create_pull.assert_called_once_with(
title="test-title",
body="CRE Sync test-title",
head="feature-branch",
base="main",
)

@patch("application.utils.git.Github")
def test_create_pull_request_defaults_to_master(self, mocked_github) -> None:
github_client = MagicMock()
repo = MagicMock()
mocked_github.return_value = github_client
github_client.get_repo.return_value = repo

createPullRequest(
apiToken="token",
repo="OWASP/OpenCRE",
title="default-target",
srcBranch="feature-branch",
)

repo.create_pull.assert_called_once_with(
title="default-target",
body="CRE Sync default-target",
head="feature-branch",
base="master",
)


if __name__ == "__main__":
unittest.main()
7 changes: 4 additions & 3 deletions application/utils/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ def createPullRequest(
apiToken: str, repo: str, title: str, srcBranch: str, targetBranch: str = "master"
) -> None:
logger.info(
"Issuing pull request from %s to master for repo %s" % (srcBranch, repo)
"Issuing pull request from %s to %s for repo %s"
% (srcBranch, targetBranch, repo)
)
github = Github(apiToken)
body = "CRE Sync %s" % title
pr = github.get_repo(repo).create_pull(
title=title, body=body, head=srcBranch, base="master"
github.get_repo(repo).create_pull(
title=title, body=body, head=srcBranch, base=targetBranch
)


Expand Down
Loading