예제 #1
0
def test_get_current_commit_bad():
    thing = Gitator()
    thing.create_github_connection = MagicMock()

    github_connection = create_autospec(Github)
    thing.g = github_connection

    repo = create_autospec(Repository.Repository, instance=True)
    github_connection.get_repo = MagicMock(return_value=repo)

    repo.get_commits = MagicMock(side_effect=Exception("Can't get commits"))
    # thing.run_checks = MagicMock(return_value=True)
    with pytest.raises(Exception) as ex:
        assert thing.get_current_commit("repo_name") == False
    repo.get_commits.assert_called_once_with()
예제 #2
0
def test_get_current_commit_good():
    thing = Gitator()
    thing.create_github_connection = MagicMock()

    github_connection = create_autospec(Github)
    thing.g = github_connection

    repo = create_autospec(Repository.Repository, instance=True)
    github_connection.get_repo = MagicMock(return_value=repo)

    commits = create_autospec(PaginatedList.PaginatedList)
    repo.get_commits = MagicMock(return_value=commits)

    commits[0].sha = 'commit_sha'

    # thing.run_checks = MagicMock(return_value=True)
    assert thing.get_current_commit("repo_name") == 'commit_sha'
    repo.get_commits.assert_called_once_with()
예제 #3
0
    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)