def auth_flow( self, request: httpx.Request ) -> typing.Generator[httpx.Request, httpx.Response, None]: token = self.get_access_token() if token: request.headers["Authorization"] = f"token {token}" response = yield request if response.status_code != 401: # due to access_token return with self.response_body_read(): auth_response = yield self.build_access_token_request() if auth_response.status_code == 401: # due to jwt auth_response = yield self.build_access_token_request( force=True) if auth_response.status_code == 404: raise exceptions.MergifyNotInstalled() elif auth_response.status_code == 403: error_message = auth_response.json()["message"] if "This installation has been suspended" in error_message: LOG.debug( "Mergify installation suspended", gh_owner=self._owner_login, error_message=error_message, ) raise exceptions.MergifyNotInstalled() http.raise_for_status(auth_response) token = self._set_access_token(auth_response.json()) request.headers["Authorization"] = f"token {token}" yield request
def auth_flow(self, request): request.headers["Authorization"] = f"token {self._token}" if self.owner_id is None: with self.response_body_read(): user_response = yield self.build_request( "GET", f"{config.GITHUB_API_URL}/users/{self.owner}") http.raise_for_status(user_response) self.owner_id = user_response.json()["id"] yield request
def auth_flow(self, request): request.headers["Authorization"] = f"token {self._token}" if self.owner_id is None or self.owner is None: with self.response_body_read(): user_response = yield self.build_request( "GET", f"{config.GITHUB_API_URL}/user") http.raise_for_status(user_response) account = typing.cast(github_types.GitHubAccount, user_response.json()) self.owner_id = account["id"] self.owner = account["login"] yield request
def auth_flow(self, request): if self.installation is None: with self.response_body_read(): installation_response = yield self.build_installation_request() if installation_response.status_code == 401: # due to jwt installation_response = yield self.build_installation_request( force=True ) if installation_response.is_redirect: installation_response = yield self.build_installation_request( url=installation_response.headers["Location"], ) if installation_response.status_code == 404: LOG.debug( "Mergify not installed", gh_owner=self.owner, error_message=installation_response.json()["message"], ) raise exceptions.MergifyNotInstalled() http.raise_for_status(installation_response) self._set_installation(installation_response) token = self._get_access_token() if token: request.headers["Authorization"] = f"token {token}" response = yield request if response.status_code != 401: # due to access_token return with self.response_body_read(): auth_response = yield self.build_access_token_request() if auth_response.status_code == 401: # due to jwt auth_response = yield self.build_access_token_request(force=True) if auth_response.status_code == 403: error_message = auth_response.json()["message"] if "This installation has been suspended" in error_message: LOG.debug( "Mergify installation suspended", gh_owner=self.owner, error_message=error_message, ) raise exceptions.MergifyNotInstalled() http.raise_for_status(auth_response) token = self._set_access_token(auth_response.json()) request.headers["Authorization"] = f"token {token}" yield request
async def test_stream_processor_ignore_503(run_engine, _, redis_stream, redis_cache, logger_checker): response = mock.Mock() response.text = "Server Error: Sorry, this diff is taking too long to generate." response.status_code = 503 response.json.side_effect = json.JSONDecodeError("whatever", "", 0) response.reason_phrase = "Service Unavailable" response.url = "https://api.github.com/repositories/1234/pulls/5/files" run_engine.side_effect = lambda *_: http.raise_for_status(response) await worker.push( redis_stream, 123, "owner1", "repo", 123, "pull_request", {"payload": "whatever"}, ) await run_worker() # Check redis is empty assert 0 == (await redis_stream.zcard("streams")) assert 0 == len(await redis_stream.keys("stream~*")) assert 0 == len(await redis_stream.hgetall("attempts"))