diff --git a/application/tests/git_utils_test.py b/application/tests/git_utils_test.py new file mode 100644 index 000000000..7460b24cc --- /dev/null +++ b/application/tests/git_utils_test.py @@ -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() diff --git a/application/utils/git.py b/application/utils/git.py index 31ef5ccfc..7db3c0b4a 100644 --- a/application/utils/git.py +++ b/application/utils/git.py @@ -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 )