Skip to content

Commit

Permalink
api, main: port various functionality (bug 1887013)
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
zzzeid committed Apr 5, 2024
1 parent bd990be commit 18b180e
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/lando/api/legacy/api/diff_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import logging

from connexion import problem
from lando.main.support import problem

from lando.api.legacy.decorators import require_phabricator_api_key
from lando.main.models.revision import DiffWarning, DiffWarningStatus
Expand Down
3 changes: 1 addition & 2 deletions src/lando/api/legacy/api/landing_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import logging

from connexion import ProblemException
from flask import g
from lando.main.support import ProblemException, g

from lando.api import auth
from lando.main.models.landing_job import LandingJob, LandingJobAction, LandingJobStatus
Expand Down
2 changes: 1 addition & 1 deletion src/lando/api/legacy/api/stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import urllib.parse

from connexion import problem
from lando.main.support import problem
from lando import settings

from lando.api.legacy.commit_message import format_commit_message
Expand Down
3 changes: 1 addition & 2 deletions src/lando/api/legacy/api/transplants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from typing import Optional

import kombu
from connexion import ProblemException, problem
from lando.main.support import ProblemException, problem, g
from lando import settings
from flask import g

from lando.api import auth
from lando.api.legacy.commit_message import format_commit_message
Expand Down
8 changes: 2 additions & 6 deletions src/lando/api/legacy/api/try_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
import io
import logging

from connexion import ProblemException
from flask import (
current_app,
g,
)
from lando.main.support import ProblemException, g

from lando.api import auth
from lando.api.legacy.hgexports import (
Expand Down Expand Up @@ -110,7 +106,7 @@ def post_patches(data: dict):
patches = data["patches"]
patch_format = PatchFormat(data["patch_format"])

environment_repos = get_repos_for_env(current_app.config.get("ENVIRONMENT"))
environment_repos = get_repos_for_env(settings.ENVIRONMENT)
try_repo = environment_repos.get("try")
if not try_repo:
raise ProblemException(
Expand Down
2 changes: 1 addition & 1 deletion src/lando/api/legacy/api/uplift.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging

from connexion import problem
from lando.main.support import problem
from lando import settings

from lando.api import auth
Expand Down
8 changes: 4 additions & 4 deletions src/lando/api/legacy/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
)

import requests
from connexion import ProblemException, request
from lando.main.support import ProblemException, request
from lando import settings
from flask import g
from lando.main.support import g
from jose import jwt

from django.core.cache import cache
Expand Down Expand Up @@ -535,8 +535,8 @@ class Auth0Subsystem(Subsystem):
name = "auth0"

def ready(self) -> bool | str:
domain = self.flask_app.config.get("OIDC_DOMAIN")
identifier = self.flask_app.config.get("OIDC_IDENTIFIER")
domain = settings.OIDC_DOMAIN
identifier = settings.OIDC_IDENTIFIER

# OIDC_DOMAIN should be the domain assigned to the auth0 organization.
# Leaving this unset could cause an application security problem. We
Expand Down
4 changes: 2 additions & 2 deletions src/lando/api/legacy/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class CacheSubsystem(Subsystem):

def init_app(self, app):
super().init_app(app)
host = self.flask_app.config.get("CACHE_REDIS_HOST")
if self.flask_app.config.get("CACHE_DISABLED"):
host = settings.CACHE_REDIS_HOST
if settings.CACHE_DISABLED:
# Default to not caching for testing.
logger.warning("Cache initialized in null mode.")
cache_config = {"CACHE_TYPE": "NullCache"}
Expand Down
4 changes: 2 additions & 2 deletions src/lando/api/legacy/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ def init_app(self, app):

celery.init_app(
self.flask_app,
config={"broker_url": self.flask_app.config.get("CELERY_BROKER_URL")},
config={"broker_url": settings.CELERY_BROKER_URL},
)
celery.log.setup()

def ready(self):
if self.flask_app.config.get("DISABLE_CELERY"):
if settings.DISABLE_CELERY:
return True

# TODO: Check connection to CELERY_BROKER_URL
Expand Down
2 changes: 1 addition & 1 deletion src/lando/api/legacy/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Callable,
)

from connexion import problem, request
from lando.main.support import problem, request
from lando import settings

from lando.api.legacy.phabricator import PhabricatorClient
Expand Down
2 changes: 1 addition & 1 deletion src/lando/api/legacy/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
from typing import Optional

from connexion import FlaskApi, problem
from lando.main.support import FlaskApi, problem
from flask import (
Flask,
Response,
Expand Down
9 changes: 5 additions & 4 deletions src/lando/api/legacy/phabricator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import requests

from lando import settings
from lando.api.legacy.systems import Subsystem

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -362,8 +363,8 @@ class PhabricatorSubsystem(Subsystem):
name = "phabricator"

def ready(self) -> bool | str:
unpriv_key = self.flask_app.config["PHABRICATOR_UNPRIVILEGED_API_KEY"]
priv_key = self.flask_app.config["PHABRICATOR_ADMIN_API_KEY"]
unpriv_key = settings.PHABRICATOR_UNPRIVILEGED_API_KEY
priv_key = settings.PHABRICATOR_ADMIN_API_KEY

if unpriv_key and PHAB_API_KEY_RE.search(unpriv_key) is None:
return (
Expand All @@ -382,8 +383,8 @@ def ready(self) -> bool | str:
def healthy(self) -> bool | str:
try:
PhabricatorClient(
self.flask_app.config["PHABRICATOR_URL"],
self.flask_app.config["PHABRICATOR_UNPRIVILEGED_API_KEY"],
settings.PHABRICATOR_URL,
settings.PHABRICATOR_UNPRIVILEGED_API_KEY,
).call_conduit("conduit.ping")
except PhabricatorAPIException as exc:
return "PhabricatorAPIException: {!s}".format(exc)
Expand Down
8 changes: 4 additions & 4 deletions src/lando/api/legacy/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,13 @@ class RepoCloneSubsystem(Subsystem):
name = "repo_clone"

def ready(self) -> Optional[bool | str]:
clones_path = self.flask_app.config["REPO_CLONES_PATH"]
repo_names = self.flask_app.config["REPOS_TO_LAND"]
clones_path = settings.REPO_CLONES_PATH
repo_names = settings.REPOS_TO_LAND

if not clones_path and not repo_names:
return None

clones_path = pathlib.Path(self.flask_app.config["REPO_CLONES_PATH"])
clones_path = pathlib.Path(settings.REPO_CLONES_PATH)
if not clones_path.exists() or not clones_path.is_dir():
return (
"REPO_CLONES_PATH ({}) is not a valid path to an existing "
Expand All @@ -428,7 +428,7 @@ def ready(self) -> Optional[bool | str]:
"of repository names."
)

repos = get_repos_for_env(self.flask_app.config.get("ENVIRONMENT"))
repos = get_repos_for_env(settings.ENVIRONMENT)
if not all(name in repos for name in repo_names):
return "REPOS_TO_LAND contains unsupported repository names."

Expand Down
8 changes: 4 additions & 4 deletions src/lando/api/legacy/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging

import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.django import DjangoIntegration

from lando.api.legacy.systems import Subsystem

Expand All @@ -30,14 +30,14 @@ class SentrySubsystem(Subsystem):
def init_app(self, app):
super().init_app(app)

sentry_dsn = self.flask_app.config.get("SENTRY_DSN")
sentry_dsn = settings.SENTRY_DSN
logger.info("sentry status", extra={"enabled": bool(sentry_dsn)})
sentry_sdk.init(
before_send=before_send,
dsn=sentry_dsn,
integrations=[FlaskIntegration()],
integrations=[DjangoIntegration()],
traces_sample_rate=1.0,
release=self.flask_app.config.get("VERSION").get("version", "0.0.0"),
release=self.settings.VERSION.get("version", "0.0.0"),
)


Expand Down
26 changes: 12 additions & 14 deletions src/lando/api/legacy/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ def init_app(self, app):
def suppressed(self):
return (
self.flask_app is None
or bool(self.flask_app.config.get("MAIL_SUPPRESS_SEND"))
or not self.flask_app.config.get("MAIL_SERVER")
or bool(settings.MAIL_SUPPRESS_SEND)
or not settings.MAIL_SERVER
)

@property
def default_from(self):
return self.flask_app.config.get("MAIL_FROM") or "[email protected]"
return settings.MAIL_FROM or "[email protected]"

@contextmanager
def connection(self):
if self.suppressed:
raise ValueError("Supressed SMTP has no connection")

host = self.flask_app.config.get("MAIL_SERVER") or None
port = self.flask_app.config.get("MAIL_PORT") or None
use_ssl = self.flask_app.config.get("MAIL_USE_SSL")
use_tls = self.flask_app.config.get("MAIL_USE_TLS")
host = settings.MAIL_SERVER or None
port = settings.MAIL_PORT or None
use_ssl = settings.MAIL_USE_SSL
use_tls = settings.MAIL_USE_TLS

username = self.flask_app.config.get("MAIL_USERNAME") or None
password = self.flask_app.config.get("MAIL_PASSWORD") or None
username = settings.MAIL_USERNAME or None
password = settings.MAIL_PASSWORD or None

smtp_class = smtplib.SMTP_SSL if use_ssl else smtplib.SMTP
c = smtp_class(host, port)
Expand All @@ -60,7 +60,7 @@ def recipient_allowed(self, email):
if self.flask_app is None:
return True

whitelist = self.flask_app.config.get("MAIL_RECIPIENT_WHITELIST") or None
whitelist = settings.MAIL_RECIPIENT_WHITELIST or None
if whitelist is None:
return True

Expand All @@ -82,10 +82,8 @@ def ready(self):
logger.warning(
"SMTP is suppressed, assuming ready",
extra={
"MAIL_SERVER": self.flask_app.config.get("MAIL_SERVER"),
"MAIL_SUPPRESS_SEND": self.flask_app.config.get(
"MAIL_SUPPRESS_SEND"
),
"MAIL_SERVER": settings.MAIL_SERVER,
"MAIL_SUPPRESS_SEND": settings.MAIL_SUPPRESS_SEND,
},
)
return True
Expand Down
2 changes: 1 addition & 1 deletion src/lando/api/legacy/transplants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from datetime import datetime, timezone

import requests
from connexion import ProblemException
from lando.main.support import ProblemException
from lando import settings

from lando.main.models.landing_job import LandingJob, LandingJobStatus
Expand Down
4 changes: 2 additions & 2 deletions src/lando/api/legacy/treestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ class TreeStatusSubsystem(Subsystem):
def init_app(self, app):
super().init_app(app)

self.client = TreeStatus(url=self.flask_app.config["TREESTATUS_URL"])
version_sha = self.flask_app.config["VERSION"].get("version", "dev")
self.client = TreeStatus(url=settings.TREESTATUS_URL)
version_sha = settings.VERSION.get("version", "dev")
self.client.session.headers.update(
{"User-Agent": f"landoapi.treestatus.TreeStatus/{version_sha}"}
)
Expand Down
2 changes: 1 addition & 1 deletion src/lando/api/legacy/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LandoUISubsystem(Subsystem):
name = "lando_ui"

def ready(self) -> bool | str:
url = urlparse(self.flask_app.config["LANDO_UI_URL"])
url = urlparse(settings.LANDO_UI_URL)
if not url.scheme or not url.netloc:
return "Invalid LANDO_UI_URL, missing a scheme and/or hostname"

Expand Down
2 changes: 1 addition & 1 deletion src/lando/api/legacy/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import re

from connexion import ProblemException
from lando.main.support import ProblemException

REVISION_ID_RE = re.compile(r"^D(?P<id>[1-9][0-9]*)$")

Expand Down
5 changes: 2 additions & 3 deletions src/lando/api/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import pytest
import requests
import requests_mock
from connexion import ProblemException
from connexion.lifecycle import ConnexionResponse
from flask import g
from lando.main.support import ProblemException, ConnexionResponse
from lando.main.support import g

from lando.api.legacy.auth import (
A0User,
Expand Down
2 changes: 1 addition & 1 deletion src/lando/api/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import pytest
from connexion.lifecycle import ConnexionResponse
from lando.main.support import ConnexionResponse

from lando.api.legacy.decorators import require_phabricator_api_key
from lando.api.legacy.phabricator import PhabricatorClient
Expand Down
2 changes: 1 addition & 1 deletion src/lando/api/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import pytest
from connexion import ProblemException
from lando.main.support import ProblemException

from lando.api.legacy.validation import revision_id_to_int

Expand Down
35 changes: 35 additions & 0 deletions src/lando/main/support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.http import HttpResponse


class ProblemException(Exception):
def __init__(self, *, status=500, title=None, detail=None, type=None, instance=None, headers=None, ext=None):
# TODO: this should be reimplemented as either middleware or HttpResponse return values.
super().__init__(self)


def problem(status, title, detail, type=None, instance=None, headers=None, ext=None):
return HttpResponse(content=detail, headers=headers, status_code=status)


request = {
"headers": {},
}

session = {}


class g:
auth0_user = None
access_token = None
access_token_payload = None
_request_start_timestamp = None


class FlaskApi:
@classmethod
def get_response(self, _problem):
return _problem


class ConnexionResponse(HttpResponse):
pass

0 comments on commit 18b180e

Please sign in to comment.