Skip to content

Commit eb6f15c

Browse files
committed
api, main: port various functionality (bug 1887013)
WIP
1 parent fec047d commit eb6f15c

23 files changed

+86
-59
lines changed

src/lando/api/legacy/api/diff_warnings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import logging
1313

14-
from connexion import problem
14+
from lando.main.support import problem
1515

1616
from lando.api.legacy.decorators import require_phabricator_api_key
1717
from lando.main.models.revision import DiffWarning, DiffWarningStatus

src/lando/api/legacy/api/landing_jobs.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
import logging
55

6-
from connexion import ProblemException
7-
from flask import g
6+
from lando.main.support import ProblemException, g
87

98
from lando.api import auth
109
from lando.main.models.landing_job import LandingJob, LandingJobAction, LandingJobStatus

src/lando/api/legacy/api/stacks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import urllib.parse
66

7-
from connexion import problem
7+
from lando.main.support import problem
88
from lando import settings
99

1010
from lando.api.legacy.commit_message import format_commit_message

src/lando/api/legacy/api/transplants.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
from typing import Optional
88

99
import kombu
10-
from connexion import ProblemException, problem
10+
from lando.main.support import ProblemException, problem, g
1111
from lando import settings
12-
from flask import g
1312

1413
from lando.api import auth
1514
from lando.api.legacy.commit_message import format_commit_message

src/lando/api/legacy/api/try_push.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
import io
99
import logging
1010

11-
from connexion import ProblemException
12-
from flask import (
13-
current_app,
14-
g,
15-
)
11+
from lando.main.support import ProblemException, g
1612

1713
from lando.api import auth
1814
from lando.api.legacy.hgexports import (
@@ -110,7 +106,7 @@ def post_patches(data: dict):
110106
patches = data["patches"]
111107
patch_format = PatchFormat(data["patch_format"])
112108

113-
environment_repos = get_repos_for_env(current_app.config.get("ENVIRONMENT"))
109+
environment_repos = get_repos_for_env(settings.ENVIRONMENT)
114110
try_repo = environment_repos.get("try")
115111
if not try_repo:
116112
raise ProblemException(

src/lando/api/legacy/api/uplift.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import logging
66

7-
from connexion import problem
7+
from lando.main.support import problem
88
from lando import settings
99

1010
from lando.api import auth

src/lando/api/legacy/auth.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
)
1616

1717
import requests
18-
from connexion import ProblemException, request
18+
from lando.main.support import ProblemException, request
1919
from lando import settings
20-
from flask import g
20+
from lando.main.support import g
2121
from jose import jwt
2222

2323
from django.core.cache import cache
@@ -535,8 +535,8 @@ class Auth0Subsystem(Subsystem):
535535
name = "auth0"
536536

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

541541
# OIDC_DOMAIN should be the domain assigned to the auth0 organization.
542542
# Leaving this unset could cause an application security problem. We

src/lando/api/legacy/cache.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class CacheSubsystem(Subsystem):
2626

2727
def init_app(self, app):
2828
super().init_app(app)
29-
host = self.flask_app.config.get("CACHE_REDIS_HOST")
30-
if self.flask_app.config.get("CACHE_DISABLED"):
29+
host = settings.CACHE_REDIS_HOST
30+
if settings.CACHE_DISABLED:
3131
# Default to not caching for testing.
3232
logger.warning("Cache initialized in null mode.")
3333
cache_config = {"CACHE_TYPE": "NullCache"}

src/lando/api/legacy/celery.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ def init_app(self, app):
142142

143143
celery.init_app(
144144
self.flask_app,
145-
config={"broker_url": self.flask_app.config.get("CELERY_BROKER_URL")},
145+
config={"broker_url": settings.CELERY_BROKER_URL},
146146
)
147147
celery.log.setup()
148148

149149
def ready(self):
150-
if self.flask_app.config.get("DISABLE_CELERY"):
150+
if settings.DISABLE_CELERY:
151151
return True
152152

153153
# TODO: Check connection to CELERY_BROKER_URL

src/lando/api/legacy/decorators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Callable,
77
)
88

9-
from connexion import problem, request
9+
from lando.main.support import problem, request
1010
from lando import settings
1111

1212
from lando.api.legacy.phabricator import PhabricatorClient

src/lando/api/legacy/hooks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import time
77
from typing import Optional
88

9-
from connexion import FlaskApi, problem
9+
from lando.main.support import FlaskApi, problem
1010
from flask import (
1111
Flask,
1212
Response,

src/lando/api/legacy/phabricator.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import requests
2626

27+
from lando import settings
2728
from lando.api.legacy.systems import Subsystem
2829

2930
logger = logging.getLogger(__name__)
@@ -362,8 +363,8 @@ class PhabricatorSubsystem(Subsystem):
362363
name = "phabricator"
363364

364365
def ready(self) -> bool | str:
365-
unpriv_key = self.flask_app.config["PHABRICATOR_UNPRIVILEGED_API_KEY"]
366-
priv_key = self.flask_app.config["PHABRICATOR_ADMIN_API_KEY"]
366+
unpriv_key = settings.PHABRICATOR_UNPRIVILEGED_API_KEY
367+
priv_key = settings.PHABRICATOR_ADMIN_API_KEY
367368

368369
if unpriv_key and PHAB_API_KEY_RE.search(unpriv_key) is None:
369370
return (
@@ -382,8 +383,8 @@ def ready(self) -> bool | str:
382383
def healthy(self) -> bool | str:
383384
try:
384385
PhabricatorClient(
385-
self.flask_app.config["PHABRICATOR_URL"],
386-
self.flask_app.config["PHABRICATOR_UNPRIVILEGED_API_KEY"],
386+
settings.PHABRICATOR_URL,
387+
settings.PHABRICATOR_UNPRIVILEGED_API_KEY,
387388
).call_conduit("conduit.ping")
388389
except PhabricatorAPIException as exc:
389390
return "PhabricatorAPIException: {!s}".format(exc)

src/lando/api/legacy/repos.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,13 @@ class RepoCloneSubsystem(Subsystem):
408408
name = "repo_clone"
409409

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

414414
if not clones_path and not repo_names:
415415
return None
416416

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

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

src/lando/api/legacy/sentry.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55

66
import sentry_sdk
7-
from sentry_sdk.integrations.flask import FlaskIntegration
7+
from sentry_sdk.integrations.django import DjangoIntegration
88

99
from lando.api.legacy.systems import Subsystem
1010

@@ -30,14 +30,14 @@ class SentrySubsystem(Subsystem):
3030
def init_app(self, app):
3131
super().init_app(app)
3232

33-
sentry_dsn = self.flask_app.config.get("SENTRY_DSN")
33+
sentry_dsn = settings.SENTRY_DSN
3434
logger.info("sentry status", extra={"enabled": bool(sentry_dsn)})
3535
sentry_sdk.init(
3636
before_send=before_send,
3737
dsn=sentry_dsn,
38-
integrations=[FlaskIntegration()],
38+
integrations=[DjangoIntegration()],
3939
traces_sample_rate=1.0,
40-
release=self.flask_app.config.get("VERSION").get("version", "0.0.0"),
40+
release=self.settings.VERSION.get("version", "0.0.0"),
4141
)
4242

4343

src/lando/api/legacy/smtp.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ def init_app(self, app):
2323
def suppressed(self):
2424
return (
2525
self.flask_app is None
26-
or bool(self.flask_app.config.get("MAIL_SUPPRESS_SEND"))
27-
or not self.flask_app.config.get("MAIL_SERVER")
26+
or bool(settings.MAIL_SUPPRESS_SEND)
27+
or not settings.MAIL_SERVER
2828
)
2929

3030
@property
3131
def default_from(self):
32-
return self.flask_app.config.get("MAIL_FROM") or "[email protected]"
32+
return settings.MAIL_FROM or "[email protected]"
3333

3434
@contextmanager
3535
def connection(self):
3636
if self.suppressed:
3737
raise ValueError("Supressed SMTP has no connection")
3838

39-
host = self.flask_app.config.get("MAIL_SERVER") or None
40-
port = self.flask_app.config.get("MAIL_PORT") or None
41-
use_ssl = self.flask_app.config.get("MAIL_USE_SSL")
42-
use_tls = self.flask_app.config.get("MAIL_USE_TLS")
39+
host = settings.MAIL_SERVER or None
40+
port = settings.MAIL_PORT or None
41+
use_ssl = settings.MAIL_USE_SSL
42+
use_tls = settings.MAIL_USE_TLS
4343

44-
username = self.flask_app.config.get("MAIL_USERNAME") or None
45-
password = self.flask_app.config.get("MAIL_PASSWORD") or None
44+
username = settings.MAIL_USERNAME or None
45+
password = settings.MAIL_PASSWORD or None
4646

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

63-
whitelist = self.flask_app.config.get("MAIL_RECIPIENT_WHITELIST") or None
63+
whitelist = settings.MAIL_RECIPIENT_WHITELIST or None
6464
if whitelist is None:
6565
return True
6666

@@ -82,10 +82,8 @@ def ready(self):
8282
logger.warning(
8383
"SMTP is suppressed, assuming ready",
8484
extra={
85-
"MAIL_SERVER": self.flask_app.config.get("MAIL_SERVER"),
86-
"MAIL_SUPPRESS_SEND": self.flask_app.config.get(
87-
"MAIL_SUPPRESS_SEND"
88-
),
85+
"MAIL_SERVER": settings.MAIL_SERVER,
86+
"MAIL_SUPPRESS_SEND": settings.MAIL_SUPPRESS_SEND,
8987
},
9088
)
9189
return True

src/lando/api/legacy/transplants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from datetime import datetime, timezone
1010

1111
import requests
12-
from connexion import ProblemException
12+
from lando.main.support import ProblemException
1313
from lando import settings
1414

1515
from lando.main.models.landing_job import LandingJob, LandingJobStatus

src/lando/api/legacy/treestatus.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ class TreeStatusSubsystem(Subsystem):
156156
def init_app(self, app):
157157
super().init_app(app)
158158

159-
self.client = TreeStatus(url=self.flask_app.config["TREESTATUS_URL"])
160-
version_sha = self.flask_app.config["VERSION"].get("version", "dev")
159+
self.client = TreeStatus(url=settings.TREESTATUS_URL)
160+
version_sha = settings.VERSION.get("version", "dev")
161161
self.client.session.headers.update(
162162
{"User-Agent": f"landoapi.treestatus.TreeStatus/{version_sha}"}
163163
)

src/lando/api/legacy/ui.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class LandoUISubsystem(Subsystem):
1616
name = "lando_ui"
1717

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

src/lando/api/legacy/validation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
import re
55

6-
from connexion import ProblemException
6+
from lando.main.support import ProblemException
77

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

src/lando/api/tests/test_auth.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import pytest
88
import requests
99
import requests_mock
10-
from connexion import ProblemException
11-
from connexion.lifecycle import ConnexionResponse
12-
from flask import g
10+
from lando.main.support import ProblemException, ConnexionResponse
11+
from lando.main.support import g
1312

1413
from lando.api.legacy.auth import (
1514
A0User,

src/lando/api/tests/test_decorators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
import pytest
5-
from connexion.lifecycle import ConnexionResponse
5+
from lando.main.support import ConnexionResponse
66

77
from lando.api.legacy.decorators import require_phabricator_api_key
88
from lando.api.legacy.phabricator import PhabricatorClient

src/lando/api/tests/test_validation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
import pytest
5-
from connexion import ProblemException
5+
from lando.main.support import ProblemException
66

77
from lando.api.legacy.validation import revision_id_to_int
88

src/lando/main/support.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from django.http import HttpResponse
2+
3+
4+
class ProblemException(Exception):
5+
def __init__(self, *, status=500, title=None, detail=None, type=None, instance=None, headers=None, ext=None):
6+
# TODO: this should be reimplemented as either middleware or HttpResponse return values.
7+
super().__init__(self)
8+
9+
10+
def problem(status, title, detail, type=None, instance=None, headers=None, ext=None):
11+
return HttpResponse(content=detail, headers=headers, status_code=status)
12+
13+
14+
request = {
15+
"headers": {},
16+
}
17+
18+
session = {}
19+
20+
21+
class g:
22+
auth0_user = None
23+
access_token = None
24+
access_token_payload = None
25+
_request_start_timestamp = None
26+
27+
28+
class FlaskApi:
29+
@classmethod
30+
def get_response(self, _problem):
31+
return _problem
32+
33+
34+
class ConnexionResponse(HttpResponse):
35+
pass

0 commit comments

Comments
 (0)