diff --git a/helpers/GitHelper.py b/helpers/GitHelper.py index 96c29f9..b51482d 100644 --- a/helpers/GitHelper.py +++ b/helpers/GitHelper.py @@ -10,7 +10,7 @@ class GitHelper(object): def create_pull_request(self, repository, title, description, head_branch, base_branch, git_token): """Creates the pull request for the head_branch against the base_branch""" - git_pulls_api = "https://github.com/api/v3/repos/{repository}/pulls".format(repository) + git_pulls_api = "https://github.com/api/v3/repos/{repository}/pulls".format(repository=repository) headers = { "Authorization": "token {0}".format(git_token), @@ -24,13 +24,16 @@ def create_pull_request(self, repository, title, description, head_branch, base_ "base": base_branch, } - r = requests.post( - git_pulls_api, - headers=headers, - data=json.dumps(payload)) - - if not r.ok: - print("Request Failed: {0}".format(r.text)) + try: + r = requests.post( + git_pulls_api, + headers=headers, + data=json.dumps(payload)) + if not r.ok: + print("Request Failed: {0}".format(r.text)) + except requests.exceptions.RequestException as e: + print("Request Failed: {0}".format(e)) + raise e def push_changes_in_pull_request(self, repository, message, target_branch, head_branch, token, clone_path): diff --git a/tests/helpers/test_GitHelper.py b/tests/helpers/test_GitHelper.py index e69de29..915130a 100644 --- a/tests/helpers/test_GitHelper.py +++ b/tests/helpers/test_GitHelper.py @@ -0,0 +1,38 @@ +import unittest +import requests +from helpers.GitHelper import GitHelper +from unittest import mock + +# ------------------------------------------------------------------------------------------- +# Mocks used in the below unit test class +# ------------------------------------------------------------------------------------------- +def mocked_pull_request_failure(*args, **kwargs): + """ + Mock function to mimic a failure to create a pull request + """ + raise requests.exceptions.HTTPError + +# ------------------------------------------------------------------------------------------- +# Unit test class +# ------------------------------------------------------------------------------------------- +class TestGitHelper(unittest.TestCase): + + # Mock some methods. The mock object is passed in to our test case method. + @mock.patch('helpers.GitHelper.requests.post', side_effect=mocked_pull_request_failure) + def test_create_pull_request_ko(self, mock_requests_post): + git_helper = GitHelper() + repository = "repository" + title = "title" + description = "description" + head_branch = "head_branch" + base_branch = "base_branch" + git_token = "git_token" + self.assertRaises(requests.exceptions.HTTPError, + lambda: git_helper.create_pull_request(repository, title, description, head_branch, + base_branch, git_token)) + +# ------------------------------------------------------------------------------------------- +# Main +# ------------------------------------------------------------------------------------------- +if __name__ == "__main__": + unittest.main()