Skip to content

Commit a83ab32

Browse files
committed
Do not require settings environment vars to just run linting
1 parent 43c671f commit a83ab32

File tree

11 files changed

+41
-41
lines changed

11 files changed

+41
-41
lines changed

jbi/app.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323

2424
import jbi
2525
import jbi.queue
26-
from jbi.configuration import ACTIONS
26+
from jbi.configuration import get_actions
2727
from jbi.environment import get_settings
2828
from jbi.log import CONFIG
2929
from jbi.router import router
3030

3131
SRC_DIR = Path(__file__).parent
3232
APP_DIR = Path(__file__).parents[1]
3333

34+
ACTIONS = get_actions()
3435
settings = get_settings()
3536
version_info: dict[str, str] = get_version(APP_DIR)
3637
VERSION: str = version_info["version"]
@@ -60,8 +61,8 @@ def traces_sampler(sampling_context: dict[str, Any]) -> float:
6061
# https://github.com/tiangolo/fastapi/discussions/9241
6162
@asynccontextmanager
6263
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
63-
jira_service = jbi.jira.get_service()
64-
bugzilla_service = jbi.bugzilla.get_service()
64+
jira_service = jbi.jira.service.get_service()
65+
bugzilla_service = jbi.bugzilla.service.get_service()
6566
queue = jbi.queue.get_dl_queue()
6667

6768
checks.register(bugzilla_service.check_bugzilla_connection, name="bugzilla.up")

jbi/bugzilla/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
from .models import (
2-
Bug,
3-
BugId,
4-
WebhookEvent,
5-
WebhookRequest,
6-
)
7-
from .service import BugzillaService, get_service

jbi/configuration.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from jbi import environment
1111
from jbi.models import Actions
1212

13-
settings = environment.get_settings()
1413
logger = logging.getLogger(__name__)
1514

1615

@@ -30,9 +29,9 @@ def get_actions_from_file(jbi_config_file: str) -> Actions:
3029
raise ConfigError("Errors exist.") from exception
3130

3231

33-
def get_actions(env=settings.env):
32+
def get_actions(env=None) -> Actions:
3433
"""Load actions from file determined by ENV name"""
34+
if env is None:
35+
settings = environment.get_settings()
36+
env = settings.env
3537
return get_actions_from_file(f"config/config.{env}.yaml")
36-
37-
38-
ACTIONS = get_actions()

jbi/jira/service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from dockerflow import checks
1515
from requests import exceptions as requests_exceptions
1616

17-
from jbi import Operation, bugzilla, environment
17+
from jbi import Operation, environment
18+
from jbi.bugzilla import models as bugzilla_models
1819
from jbi.jira.utils import markdown_to_jira
1920
from jbi.models import ActionContext
2021

@@ -184,7 +185,7 @@ def add_jira_comments_for_changes(self, context: ActionContext):
184185
return jira_response_comments
185186

186187
def delete_jira_issue_if_duplicate(
187-
self, context: ActionContext, latest_bug: bugzilla.Bug
188+
self, context: ActionContext, latest_bug: bugzilla_models.Bug
188189
):
189190
"""Rollback the Jira issue creation if there is already a linked Jira issue
190191
on the Bugzilla ticket"""

jbi/models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919

2020
from jbi import Operation, steps
21-
from jbi.bugzilla import Bug, BugId, WebhookEvent
21+
from jbi.bugzilla.models import Bug, BugId, WebhookEvent
2222

2323
logger = logging.getLogger(__name__)
2424

@@ -51,7 +51,6 @@ class ActionSteps(BaseModel, frozen=True):
5151
@classmethod
5252
def validate_steps(cls, function_names: list[str]):
5353
"""Validate that all configure step functions exist in the steps module"""
54-
5554
invalid_functions = [
5655
func_name for func_name in function_names if not hasattr(steps, func_name)
5756
]

jbi/queue.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import dockerflow.checks
3838
from pydantic import BaseModel, FileUrl, ValidationError, computed_field
3939

40-
from jbi import bugzilla
40+
from jbi.bugzilla import models as bugzilla_models
4141
from jbi.environment import get_settings
4242

4343
logger = logging.getLogger(__name__)
@@ -85,7 +85,7 @@ def from_exc(cls, exc: Exception):
8585
class QueueItem(BaseModel, frozen=True):
8686
"""Dead Letter Queue entry."""
8787

88-
payload: bugzilla.WebhookRequest
88+
payload: bugzilla_models.WebhookRequest
8989
error: Optional[PythonException] = None
9090
rid: Optional[str] = None
9191

@@ -316,15 +316,15 @@ async def check_readable(self) -> list[dockerflow.checks.CheckMessage]:
316316
)
317317
return results
318318

319-
async def postpone(self, payload: bugzilla.WebhookRequest, rid: str) -> None:
319+
async def postpone(self, payload: bugzilla_models.WebhookRequest, rid: str) -> None:
320320
"""
321321
Postpone the specified request for later.
322322
"""
323323
item = QueueItem(payload=payload, rid=rid)
324324
await self.backend.put(item)
325325

326326
async def track_failed(
327-
self, payload: bugzilla.WebhookRequest, exc: Exception, rid: str
327+
self, payload: bugzilla_models.WebhookRequest, exc: Exception, rid: str
328328
) -> QueueItem:
329329
"""
330330
Store the specified payload and exception information into the queue.
@@ -337,7 +337,7 @@ async def track_failed(
337337
await self.backend.put(item)
338338
return item
339339

340-
async def is_blocked(self, payload: bugzilla.WebhookRequest) -> bool:
340+
async def is_blocked(self, payload: bugzilla_models.WebhookRequest) -> bool:
341341
"""
342342
Return `True` if the specified `payload` is blocked and should be
343343
queued instead of being processed.

jbi/retry.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from dockerflow.logging import JsonLogFormatter, request_id_context
99

1010
import jbi.runner as runner
11-
from jbi.configuration import ACTIONS
11+
from jbi.configuration import get_actions
1212
from jbi.errors import IgnoreInvalidRequestError
1313
from jbi.queue import get_dl_queue
1414

@@ -22,6 +22,8 @@
2222
lsh.setFormatter(JsonLogFormatter(logger_name=__name__))
2323
logger.addHandler(lsh)
2424

25+
ACTIONS = get_actions()
26+
2527

2628
async def retry_failed(item_executor=runner.execute_action, queue=get_dl_queue()):
2729
min_event_timestamp = datetime.now(UTC) - timedelta(days=int(RETRY_TIMEOUT_DAYS))

jbi/router.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
from fastapi.security import APIKeyHeader, HTTPBasic, HTTPBasicCredentials
1313
from fastapi.templating import Jinja2Templates
1414

15-
from jbi import bugzilla, jira
16-
from jbi.configuration import ACTIONS
15+
from jbi import jira
16+
from jbi.bugzilla import models as bugzilla_models
17+
from jbi.bugzilla import service as bugzilla_service
18+
from jbi.configuration import get_actions
1719
from jbi.environment import Settings, get_settings
1820
from jbi.models import Actions
1921
from jbi.queue import DeadLetterQueue, get_dl_queue
2022
from jbi.runner import execute_or_queue
2123

2224
SettingsDep = Annotated[Settings, Depends(get_settings)]
23-
ActionsDep = Annotated[Actions, Depends(lambda: ACTIONS)]
24-
BugzillaServiceDep = Annotated[bugzilla.BugzillaService, Depends(bugzilla.get_service)]
25+
ActionsDep = Annotated[Actions, Depends(get_actions)]
26+
BugzillaServiceDep = Annotated[
27+
bugzilla_service.BugzillaService, Depends(bugzilla_service.get_service)
28+
]
2529
JiraServiceDep = Annotated[jira.JiraService, Depends(jira.get_service)]
2630

2731
router = APIRouter()
@@ -69,7 +73,7 @@ async def bugzilla_webhook(
6973
request: Request,
7074
actions: ActionsDep,
7175
queue: Annotated[DeadLetterQueue, Depends(get_dl_queue)],
72-
webhook_request: bugzilla.WebhookRequest = Body(..., embed=False),
76+
webhook_request: bugzilla_models.WebhookRequest = Body(..., embed=False),
7377
):
7478
"""API endpoint that Bugzilla Webhook Events request"""
7579
return await execute_or_queue(webhook_request, queue, actions)

jbi/runner.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
from dockerflow.logging import request_id_context
1212
from statsd.defaults.env import statsd
1313

14-
from jbi import ActionResult, Operation, bugzilla, jira
14+
from jbi import ActionResult, Operation, jira
1515
from jbi import steps as steps_module
16+
from jbi.bugzilla import models as bugzilla_models
1617
from jbi.bugzilla.client import BugNotAccessibleError
18+
from jbi.bugzilla.service import get_service as get_bugzilla_service
1719
from jbi.environment import get_settings
1820
from jbi.errors import ActionNotFoundError, IgnoreInvalidRequestError
1921
from jbi.models import (
@@ -56,7 +58,7 @@ def groups2operation(steps: ActionSteps):
5658
return by_operation
5759

5860

59-
def lookup_action(bug: bugzilla.Bug, actions: Actions) -> Action:
61+
def lookup_action(bug: bugzilla_models.Bug, actions: Actions) -> Action:
6062
"""
6163
Find first matching action from bug's whiteboard field.
6264
@@ -82,7 +84,7 @@ def __init__(
8284
):
8385
self.parameters = parameters
8486
if not bugzilla_service:
85-
self.bugzilla_service = bugzilla.get_service()
87+
self.bugzilla_service = get_bugzilla_service()
8688
if not jira_service:
8789
self.jira_service = jira.get_service()
8890
self.steps = self._initialize_steps(parameters.steps)
@@ -162,7 +164,7 @@ def __call__(self, context: ActionContext) -> ActionResult:
162164

163165

164166
async def execute_or_queue(
165-
request: bugzilla.WebhookRequest, queue: DeadLetterQueue, actions: Actions
167+
request: bugzilla_models.WebhookRequest, queue: DeadLetterQueue, actions: Actions
166168
):
167169
request_id = request_id_context.get()
168170

@@ -198,7 +200,7 @@ async def execute_or_queue(
198200

199201
@statsd.timer("jbi.action.execution.timer")
200202
def execute_action(
201-
request: bugzilla.WebhookRequest,
203+
request: bugzilla_models.WebhookRequest,
202204
actions: Actions,
203205
):
204206
"""Execute the configured action for the specified `request`.
@@ -230,7 +232,7 @@ def execute_action(
230232
extra=runner_context.model_dump(),
231233
)
232234
try:
233-
bug = bugzilla.get_service().refresh_bug_data(bug)
235+
bug = get_bugzilla_service().refresh_bug_data(bug)
234236
except BugNotAccessibleError as err:
235237
# This can happen if the bug is made private after the webhook
236238
# is processed (eg. if it spent some time in the DL queue)

jbi/steps.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Each step takes an `ActionContext` and a list of arbitrary parameters.
55
"""
66

7-
# This import is needed (as of Pyhon 3.11) to enable type checking with modules
7+
# This import is needed (as of Python 3.11) to enable type checking with modules
88
# imported under `TYPE_CHECKING`
99
# https://docs.python.org/3/whatsnew/3.7.html#pep-563-postponed-evaluation-of-annotations
1010
# https://docs.python.org/3/whatsnew/3.11.html#pep-563-may-not-be-the-future
@@ -19,8 +19,6 @@
1919
from jbi import Operation
2020
from jbi.environment import get_settings
2121

22-
settings = get_settings()
23-
2422

2523
class StepStatus(Enum):
2624
"""
@@ -99,6 +97,7 @@ def add_link_to_jira(
9997
context: ActionContext, *, bugzilla_service: BugzillaService
10098
) -> StepResult:
10199
"""Add the URL to the Jira issue in the `see_also` field on the Bugzilla ticket"""
100+
settings = get_settings()
102101
jira_url = f"{settings.jira_base_url}browse/{context.jira.issue}"
103102
logger.info(
104103
"Link %r on Bug %s",

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ def dl_queue(tmp_path):
127127
def mocked_bugzilla(request):
128128
if "no_mocked_bugzilla" in request.keywords:
129129
yield None
130-
bugzilla.get_service.cache_clear()
130+
bugzilla.service.get_service.cache_clear()
131131
else:
132132
with mock.patch("jbi.bugzilla.service.BugzillaClient") as mocked_bz:
133133
yield mocked_bz()
134-
bugzilla.get_service.cache_clear()
134+
bugzilla.service.get_service.cache_clear()
135135

136136

137137
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)