Ejemplo n.º 1
0
    def test_branch_disabled(self):
        old_rule = {
            "protection": {
                "required_status_checks": {
                    "strict": True,
                    "contexts": ["continuous-integration/no-ci"],
                },
                "required_pull_request_reviews": {
                    "dismiss_stale_reviews": True,
                    "require_code_owner_reviews": False,
                    "required_approving_review_count": 1,
                },
                "restrictions": None,
                "enforce_admins": False,
            }
        }
        gh_branch.protect(self.r_main, "disabled", old_rule)

        rule = rules.get_branch_rule(self.r_main, "disabled")
        self.assertEqual(None, rule)
        self.assertFalse(gh_branch.is_configured(self.r_main, "disabled",
                                                 rule))

        self.create_pr("disabled")
        self.assertEqual([], self.engine.get_cached_branches())
        self.assertEqual([], self.engine.build_queue("disabled"))

        self.assertTrue(gh_branch.is_configured(self.r_main, "disabled", rule))
Ejemplo n.º 2
0
    def test_missing_required_status_check(self):
        self.create_pr("stable")

        # Check policy of that branch is the expected one
        expected_rule = {
            "protection": {
                "required_status_checks": None,
                "required_pull_request_reviews": {
                    "dismiss_stale_reviews": True,
                    "require_code_owner_reviews": False,
                    "required_approving_review_count": 2,
                },
                "restrictions": None,
                "enforce_admins": False,
            }
        }
        self.assertTrue(gh_branch.is_configured(self.r_main, "stable",
                                                expected_rule))
Ejemplo n.º 3
0
    def test_change_mergify_yml(self):
        config = yaml.load(CONFIG)
        config["rules"]["branches"]["master"]["protection"][
            "required_pull_request_reviews"][
                "required_approving_review_count"] = 6
        config = yaml.dump(config)
        p1, commits1 = self.create_pr(files={".mergify.yml": config})
        pulls = self.engine.build_queue("master")
        self.assertEqual(1, len(pulls))

        self.assertEqual(1, pulls[0].mergify_engine["approvals"][2])

        # Check policy of that branch is the expected one
        expected_rule = {
            "protection": {
                "required_status_checks": {
                    "strict": True,
                    "contexts": ["continuous-integration/fake-ci"],
                },
                "required_pull_request_reviews": {
                    "dismiss_stale_reviews": True,
                    "require_code_owner_reviews": False,
                    "required_approving_review_count": 1,
                },
                "restrictions": None,
                "enforce_admins": False,
            }
        }

        with self.cassette("branch"):
            self.assertTrue(
                gh_branch.is_configured(self.r_main, "master", expected_rule))

        with self.cassette("statuses"):
            p1 = self.r_main.get_pull(p1.number)
            commit = p1.base.repo.get_commit(p1.head.sha)
            ctxts = [
                s.raw_data["context"]
                for s in reversed(list(commit.get_statuses()))
            ]

        self.assertIn("mergify/future-config-checker", ctxts)
Ejemplo n.º 4
0
    def test_basic(self):
        self.create_pr()
        p2, commits = self.create_pr()

        # Check we have only on branch registered
        self.assertEqual("queues~%s~mergify-test1~%s~False~master"
                         % (config.INSTALLATION_ID, self.name),
                         self.engine.get_cache_key("master"))
        self.assertEqual(["master"], self.engine.get_cached_branches())

        # Check policy of that branch is the expected one
        expected_rule = {
            "protection": {
                "required_status_checks": {
                    "strict": True,
                    "contexts": ["continuous-integration/fake-ci"],
                },
                "required_pull_request_reviews": {
                    "dismiss_stale_reviews": True,
                    "require_code_owner_reviews": False,
                    "required_approving_review_count": 1,
                },
                "restrictions": None,
                "enforce_admins": False,
            }
        }

        self.assertTrue(gh_branch.is_configured(self.r_main, "master",
                                                expected_rule))

        # Checks the content of the cache
        pulls = self.engine.build_queue("master")
        self.assertEqual(2, len(pulls))
        for p in pulls:
            self.assertEqual(0, p.mergify_engine['status']['mergify_state'])

        # Some Web API checks
        for url in ['/status/repos/%s/' % self.u_main.login,
                    '/status/repos/%s/' % self.r_main.full_name,
                    '/status/install/%s/' % config.INSTALLATION_ID]:
            r = self.app.get(url).get_json()
            self.assertEqual(1, len(r))
            self.assertEqual(2, len(r[0]['pulls']))
            self.assertEqual("master", r[0]['branch'])
            self.assertEqual(self.u_main.login, r[0]['owner'])
            self.assertEqual(self.r_main.name, r[0]['repo'])

        for url in ['/stream/status/repos/%s/' % self.u_main.login,
                    '/stream/status/repos/%s/' % self.r_main.full_name,
                    '/stream/status/install/%s/' % config.INSTALLATION_ID]:
            r = self.app.get(url)
            lines = [l for l in next(r.iter_encoded()).decode().split("\n")
                     if l]
            self.assertEqual("event: refresh", lines[0])
            self.assertEqual("data: ", lines[1][:6])

            r = json.loads(lines[1][6:])
            self.assertEqual(1, len(r))
            self.assertEqual(2, len(r[0]['pulls']))
            self.assertEqual("master", r[0]['branch'])
            self.assertEqual(self.u_main.login, r[0]['owner'])
            self.assertEqual(self.r_main.name, r[0]['repo'])

        self.create_status_and_push_event(p2,
                                          context="not required status check",
                                          state="error")
        self.create_status_and_push_event(p2)
        self.create_review_and_push_event(p2, commits[0])

        pulls = self.engine.build_queue("master")
        self.assertEqual(2, len(pulls))
        self.assertEqual(2, pulls[0].number)
        self.assertEqual(30,
                         pulls[0].mergify_engine['status']['mergify_state'])
        self.assertEqual("Will be merged soon",
                         pulls[0].mergify_engine['status'
                                                 ]['github_description'])

        self.assertEqual(1, pulls[1].number)
        self.assertEqual(0, pulls[1].mergify_engine['status']['mergify_state'])
        self.assertEqual("0/1 approvals required",
                         pulls[1].mergify_engine['status'
                                                 ]['github_description'])

        # Check the merged pull request is gone
        self.push_events(MERGE_EVENTS)

        pulls = self.engine.build_queue("master")
        self.assertEqual(1, len(pulls))

        r = json.loads(self.app.get(
            '/status/install/%s/' % config.INSTALLATION_ID
        ).data.decode("utf8"))
        self.assertEqual(1, len(r))
        self.assertEqual(1, len(r[0]['pulls']))
Ejemplo n.º 5
0
    def test_basic(self):
        self.create_pr()
        p2, commits = self.create_pr()

        # Check we have only on branch registered
        self.assertEqual(
            "queues~%s~mergify-test1~%s~master" % (INSTALLATION_ID, self.name),
            self.engine.get_cache_key("master"))
        self.assertEqual(["master"], self.engine.get_cached_branches())

        # Check policy of that branch is the expected one
        expected_rule = {
            "protection": {
                "required_status_checks": {
                    "strict": True,
                    "contexts": ["continuous-integration/fake-ci"],
                },
                "required_pull_request_reviews": {
                    "dismiss_stale_reviews": True,
                    "require_code_owner_reviews": False,
                    "required_approving_review_count": 1,
                },
                "restrictions": None,
                "enforce_admins": False,
            }
        }

        with self.cassette("branch"):
            self.assertTrue(
                gh_branch.is_configured(self.r_main, "master", expected_rule))

        # Checks the content of the cache
        pulls = self.engine.build_queue("master")
        self.assertEqual(2, len(pulls))
        for p in pulls:
            self.assertEqual(-1, p.mergify_engine['weight'])

        r = json.loads(
            self.app.get('/status/install/' + INSTALLATION_ID +
                         "/").data.decode("utf8"))
        self.assertEqual(1, len(r))
        self.assertEqual(2, len(r[0]['pulls']))
        self.assertEqual("master", r[0]['branch'])
        self.assertEqual(self.u_main.login, r[0]['owner'])
        self.assertEqual(self.r_main.name, r[0]['repo'])

        self.create_status_and_push_event(p2, commits[0])
        self.create_review_and_push_event(p2, commits[0])

        pulls = self.engine.build_queue("master")
        self.assertEqual(2, len(pulls))
        self.assertEqual(2, pulls[0].number)
        self.assertEqual(11, pulls[0].mergify_engine['weight'])
        self.assertEqual("Will be merged soon",
                         pulls[0].mergify_engine['status_desc'])

        self.assertEqual(1, pulls[1].number)
        self.assertEqual(-1, pulls[1].mergify_engine['weight'])
        self.assertEqual("Waiting for approvals",
                         pulls[1].mergify_engine['status_desc'])

        # Check the merged pull request is gone
        self.push_events(2)

        pulls = self.engine.build_queue("master")
        self.assertEqual(1, len(pulls))

        r = json.loads(
            self.app.get('/status/install/' + INSTALLATION_ID +
                         "/").data.decode("utf8"))
        self.assertEqual(1, len(r))
        self.assertEqual(1, len(r[0]['pulls']))
Ejemplo n.º 6
0
    def test_basic(self):
        self.create_pr()
        p2, commits = self.create_pr()

        # Check we have only on branch registered
        self.assertEqual("queues~%s~mergify-test1~%s~False~master"
                         % (config.INSTALLATION_ID, self.name),
                         self.engine._get_cache_key("master"))
        self.assertEqual(["master"], self.engine.get_cached_branches())

        # Check policy of that branch is the expected one
        expected_rule = {
            "protection": {
                "required_status_checks": {
                    "strict": True,
                    "contexts": ["continuous-integration/fake-ci"],
                },
                "required_pull_request_reviews": {
                    "dismiss_stale_reviews": True,
                    "require_code_owner_reviews": False,
                    "required_approving_review_count": 1,
                },
                "restrictions": None,
                "enforce_admins": False,
            }
        }

        data = gh_branch.get_protection(self.r_main, "master")
        self.assertTrue(gh_branch.is_configured(self.r_main, "master",
                                                expected_rule, data))

        # Checks the content of the cache
        pulls = self.processor._build_queue("master")
        self.assertEqual(2, len(pulls))
        for p in pulls:
            self.assertEqual(0, p.mergify_engine['status']['mergify_state'])

        self.create_status_and_push_event(p2,
                                          context="not required status check",
                                          state="error")
        self.create_status_and_push_event(p2)
        self.create_review_and_push_event(p2, commits[0])

        pulls = self.processor._build_queue("master")
        self.assertEqual(2, len(pulls))
        self.assertEqual(2, pulls[0].number)
        self.assertEqual(30,
                         pulls[0].mergify_engine['status']['mergify_state'])
        self.assertEqual("Will be merged soon",
                         pulls[0].mergify_engine['status'
                                                 ]['github_description'])

        self.assertEqual(1, pulls[1].number)
        self.assertEqual(0, pulls[1].mergify_engine['status']['mergify_state'])
        self.assertEqual("0/1 approvals required",
                         pulls[1].mergify_engine['status'
                                                 ]['github_description'])

        # Check the merged pull request is gone
        self.push_events(MERGE_EVENTS)

        pulls = self.processor._build_queue("master")
        self.assertEqual(1, len(pulls))