Example #1
0
    def tearDown(self):
        # NOTE(sileht): I'm guessing that deleting repository help to get
        # account flagged, so just keep them
        # self.r_fork.delete()
        # self.r_main.delete()

        failed_queue = rq.Queue("failed", connection=utils.get_redis_for_rq())
        self.assertEqual(0, len(failed_queue))
        self.assertEqual([], self.remaining_events)
        super(TestEngineScenario, self).tearDown()
Example #2
0
def main():  # pragma: no cover
    utils.setup_logging()
    config.log()
    gh_pr.monkeypatch_github()
    r = utils.get_redis_for_rq()
    if config.FLUSH_REDIS_ON_STARTUP:
        r.flushall()
    with rq.Connection(r):
        worker = rq.Worker(['default'])
        if config.SENTRY_URL:
            client = raven.Client(config.SENTRY_URL, transport=HTTPTransport)
            register_sentry(client, worker)
        worker.work()
Example #3
0
def get_queue():
    if not hasattr(flask.g, 'rq_queue'):
        flask.g.rq_queue = rq.Queue(connection=utils.get_redis_for_rq())
    return flask.g.rq_queue
Example #4
0
def get_queue(slug, subscription):
    global RING
    name = "%s-%s" % (RING.get_node(slug),
                      "high" if subscription["subscribed"] else "low")
    return rq.Queue(name, connection=utils.get_redis_for_rq())
Example #5
0
    def setUp(self):
        super(TestEngineScenario, self).setUp()

        self.cassette_library_dir = os.path.join(CASSETTE_LIBRARY_DIR_BASE,
                                                 self._testMethodName)

        if RECORD_MODE != "none":
            if os.path.exists(self.cassette_library_dir):
                shutil.rmtree(self.cassette_library_dir)
            os.makedirs(self.cassette_library_dir)

        self.recorder = vcr.VCR(
            cassette_library_dir=self.cassette_library_dir,
            record_mode=RECORD_MODE,
            match_on=['method', 'uri'],
            filter_headers=[
                ('Authorization', '<TOKEN>'),
                ('X-Hub-Signature', '<SIGNATURE>'),
                ('User-Agent', None),
                ('Accept-Encoding', None),
                ('Connection', None),
            ],
            before_record_response=self.response_filter,
            custom_patches=((github.MainClass, 'HTTPSConnection',
                             vcr.stubs.VCRHTTPSConnection), ))

        self.useFixture(
            fixtures.MockPatchObject(
                branch_updater.utils, 'Gitter',
                lambda: GitterRecorder(self.cassette_library_dir)))

        self.useFixture(
            fixtures.MockPatchObject(
                backports.utils, 'Gitter',
                lambda: GitterRecorder(self.cassette_library_dir)))

        # Web authentification always pass
        self.useFixture(
            fixtures.MockPatch('hmac.compare_digest', return_value=True))

        reponame_path = os.path.join(self.cassette_library_dir, "reponame")

        gen_new_uuid = (RECORD_MODE == 'all'
                        or (RECORD_MODE == 'once'
                            and not os.path.exists(reponame_path)))

        if gen_new_uuid:
            REPO_UUID = str(uuid.uuid4())
            with open(reponame_path, "w") as f:
                f.write(REPO_UUID)
        else:
            with open(reponame_path, "r") as f:
                REPO_UUID = f.read()

        self.name = "repo-%s-%s" % (REPO_UUID, self._testMethodName)

        self.pr_counter = 0
        self.remaining_events = []

        utils.setup_logging()
        config.log()

        self.git = GitterRecorder(self.cassette_library_dir, "tests")
        self.addCleanup(self.git.cleanup)

        web.app.testing = True
        self.app = web.app.test_client()

        # NOTE(sileht): Prepare a fresh redis
        self.redis = utils.get_redis_for_cache()
        self.redis.flushall()
        subscription = {"token": config.MAIN_TOKEN, "subscribed": False}
        self.redis.set("subscription-cache-%s" % config.INSTALLATION_ID,
                       json.dumps(subscription))

        # Let's start recording
        cassette = self.recorder.use_cassette("http.json")
        cassette.__enter__()
        self.addCleanup(cassette.__exit__)

        self.session = requests.Session()
        self.session.trust_env = False

        # Cleanup the remote testing redis
        r = self.session.delete(
            "https://gh.mergify.io/events-testing",
            data=FAKE_DATA,
            headers={"X-Hub-Signature": "sha1=" + FAKE_HMAC})
        r.raise_for_status()

        self.g_main = github.Github(config.MAIN_TOKEN)
        self.g_fork = github.Github(config.FORK_TOKEN)

        self.u_main = self.g_main.get_user()
        self.u_fork = self.g_fork.get_user()
        assert self.u_main.login == "mergify-test1"
        assert self.u_fork.login == "mergify-test2"

        self.r_main = self.u_main.create_repo(self.name)
        self.url_main = "https://github.com/%s" % self.r_main.full_name
        self.url_fork = "https://github.com/%s/%s" % (self.u_fork.login,
                                                      self.r_main.name)

        integration = github.GithubIntegration(config.INTEGRATION_ID,
                                               config.PRIVATE_KEY)

        access_token = integration.get_access_token(
            config.INSTALLATION_ID).token
        g = github.Github(access_token)
        user = g.get_user("mergify-test1")
        repo = user.get_repo(self.name)

        # Used to access the cache with its helper
        self.engine = engine.MergifyEngine(g, config.INSTALLATION_ID,
                                           access_token, subscription, user,
                                           repo)
        self.processor = self.engine.get_processor()

        self.rq_worker = rq.SimpleWorker([
            "incoming-events", "localhost-000-high", "localhost-001-high",
            "localhost-000-low", "localhost-001-low"
        ],
                                         connection=utils.get_redis_for_rq())

        if self._testMethodName != "test_creation_pull_of_initial_config":
            self.git("init")
            self.git.configure()
            self.git.add_cred(config.MAIN_TOKEN, "", self.r_main.full_name)
            self.git.add_cred(config.FORK_TOKEN, "",
                              "%s/%s" % (self.u_fork.login, self.r_main.name))
            self.git("config", "user.name", "%s-tester" % config.CONTEXT)
            self.git("remote", "add", "main", self.url_main)
            self.git("remote", "add", "fork", self.url_fork)

            with open(self.git.tmp + "/.mergify.yml", "w") as f:
                f.write(CONFIG)
            self.git("add", ".mergify.yml")
            self.git("commit", "--no-edit", "-m", "initial commit")
            self.git("push", "--quiet", "main", "master")

            self.git("checkout", "-b", "stable", "--quiet")
            self.git("push", "--quiet", "main", "stable")

            self.git("checkout", "-b", "nostrict", "--quiet")
            self.git("push", "--quiet", "main", "nostrict")

            self.git("checkout", "-b", "disabled", "--quiet")
            self.git("push", "--quiet", "main", "disabled")

            self.git("checkout", "-b", "enabling_label", "--quiet")
            self.git("push", "--quiet", "main", "enabling_label")

            self.r_fork = self.u_fork.create_fork(self.r_main)
            self.git("fetch", "--quiet", "fork")

            # NOTE(sileht): Github looks buggy here:
            # We receive for the new repo the expected events:
            # * installation_repositories
            # * integration_installation_repositories
            # but we receive them 6 times with the same sha1...
            self.push_events([(None, {"action": "added"})] * 12)
Example #6
0
def publish(message_kind, *args, **kwargs):
    q = rq.Queue("incoming-events", connection=utils.get_redis_for_rq())
    q.enqueue(dispatch, message_kind, *args, **kwargs)