def test_check_pr_existence_good(): thing = Gitator() thing.create_github_connection = MagicMock() thing.g = MagicMock() repo = create_autospec(Repository, instance=True) repo.get_pull = MagicMock() pr = create_autospec(PullRequest.PullRequest, instance=True) thing.g.get_repo = MagicMock(return_value=repo) repo.get_pull = MagicMock(return_value=pr) assert thing.check_pr_existence('repo_name', 1) == pr repo.get_pull.assert_called_once_with(1)
def test_check_pr_existence_bad(): thing = Gitator() thing.create_github_connection = MagicMock() thing.g = MagicMock() repo = create_autospec(Repository, instance=True) ex = Exception() ex.status = 404 ex.message = "PR not found" repo.get_pull = MagicMock(side_effect=ex) thing.g.get_repo = MagicMock(return_value=repo) with pytest.raises(Exception) as ex: assert thing.check_pr_existence('repo_name', 1) == False assert str(ex.value.message) == "PR not found" repo.get_pull.assert_called_once_with(1)
def mergefy(self, github_token, repo_name, pr_id, slack_token): print("Entering method mergefy(**********, " + repo_name + "," + pr_id + ")") gitator = Gitator() gitator.create_github_connection(github_token) gitator.check_repo_existence(repo_name) if pr_id == 'merge_back': # List PRs prs = gitator.list_prs(repo_name) # For each pr for pr in prs: print(pr) # check if pr is merge back if (pr.base.ref != 'master' and pr.head.ref == 'master'): # check_pr_mergeability try: gitator.check_pr_mergeability(pr) # merge_pr gitator.merge_pr(repo_name, pr) sc = SlackClient(slack_token) sc.api_call( 'chat.postMessage', channel='#merge_back', attachments=[{ "color": "#00FF00", "title": "Merge Back Com Sucesso", "fallback": "Merge Back Com Sucesso", "title_link": pr.html_url, "text": 'Pessoal, fiz um merge back com sucesso. Fiquem atentos às mudanças de código nas branches', "fields": [{ "title": "Repo", "value": "<" + pr.base.repo.html_url + "|" + repo_name + ">" }, { "title": "Branch", "value": pr.base.ref }, { "title": "PR", "value": "<" + pr.html_url + "|" + pr.title + " (#" + str(pr.number) + ")>" }] }]) except Exception as ex: # can't merge? exception handling sc = SlackClient(slack_token) sc.api_call( 'chat.postMessage', channel='#merge_back', attachments=[{ "color": "#FF0000", "title": "Merge Back FALHOU", "fallback": "Merge Back FALHOU", "title_link": pr.html_url, "text": 'Pessoal, não consegui fazer o merge de uma PR. O dono da branch tem que analisar se existem conflitos a serem resolvidos e solicitar o merge novamente.', "fields": [{ "title": "Repo", "value": "<" + pr.base.repo.html_url + "|" + repo_name + ">" }, { "title": "Branch", "value": pr.base.ref }, { "title": "PR", "value": "<" + pr.html_url + "|" + pr.title + " (#" + str(pr.number) + ")>" }, { "title": "Error Message", "value": str(ex) }] }]) print("Can't merge this one. Slack message sent.") print(ex) else: pr = gitator.check_pr_existence(repo_name, pr_id) gitator.check_pr_mergeability(pr) gitator.merge_pr(repo_name, pr) gitator.delete_branch(repo_name, pr) commit_sha = gitator.get_current_commit(repo_name) gitator.tag_master(repo_name, commit_sha)