def request_pull(self, github_token, repo_name, branch_name): print("Entering method requestpull(**********, " + repo_name + "," + branch_name + ")") gitator = Gitator() gitator.create_github_connection(github_token) gitator.check_repo_existence(repo_name) if branch_name == 'merge_back': branch_list = gitator.list_branches(repo_name) gitator.delete_prs(repo_name) branch_name_origin = 'master' for branch in branch_list: branch_name_destination = branch.name i = gitator.create_issue(repo_name, branch_name_origin, branch_name_destination) gitator.create_pr(repo_name, branch_name_origin, branch_name_destination, i) else: branch_name_origin = branch_name branch_name_destination = 'master' if not gitator.check_branch_existence(repo_name, branch_name_origin): raise Exception('Branch not found.') i = gitator.create_issue(repo_name, branch_name_origin, branch_name_destination) gitator.create_pr(repo_name, branch_name_origin, branch_name_destination, i)
def test_check_repo_existence_bad(): thing = Gitator() thing.create_github_connection = MagicMock() thing.g = MagicMock() thing.g.get_repo = MagicMock(return_value=False) with pytest.raises(Exception) as ex: assert thing.check_repo_existence('repo_name') == False assert ex.value.__str__() == "'bool' object has no attribute 'id'"
def test_check_repo_existence_good(): thing = Gitator() thing.create_github_connection = MagicMock() thing.g = MagicMock() assert thing.check_repo_existence('repo_name') == True
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)