Skip to content

Commit 839ec28

Browse files
authored
Remove top-level actions config key (#84)
I noticed that we had a top-level actions key in project config. This seemed somewhat unnecessary since there aren't any other config objects besides actions. That meant that any time we wanted to access actions, we'd need to do `actions_instance.actions.<whatever>`. This commit removes the top-level actions key in config and modifies the Actions Pydantic model to give it a `__root__` property to contain the actions. It also: * Uses the `parse_file` method of pydantic-yaml to read config * add descriptions and contact fields to Action model This includes the `config/` directory and example config in tests
1 parent 7ad1eba commit 839ec28

File tree

10 files changed

+161
-175
lines changed

10 files changed

+161
-175
lines changed

config/config.nonprod.yaml

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
---
2-
32
# Action Config
4-
actions:
5-
devtest:
6-
contact: tbd
7-
description: DevTest whiteboard tag
8-
enabled: true
9-
parameters:
10-
jira_project_key: JST
11-
whiteboard_tag: devtest
12-
flowstate:
13-
allow_private: true
14-
15-
description: Flowstate whiteboard tag
16-
enabled: true
17-
parameters:
18-
jira_project_key: MR2
19-
whiteboard_tag: flowstate
3+
devtest:
4+
contact: tbd
5+
description: DevTest whiteboard tag
6+
enabled: true
7+
parameters:
8+
jira_project_key: JST
9+
whiteboard_tag: devtest
10+
flowstate:
11+
allow_private: true
12+
13+
description: Flowstate whiteboard tag
14+
enabled: true
15+
parameters:
16+
jira_project_key: MR2
17+
whiteboard_tag: flowstate

config/config.prod.yaml

Lines changed: 100 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,102 @@
11
---
2-
32
# Action Config
4-
actions:
5-
addons:
6-
# action: src.jbi.whiteboard_actions.default
7-
contact: tbd
8-
description: Addons whiteboard tag for AMO Team
9-
enabled: true
10-
parameters:
11-
jira_project_key: WEBEXT
12-
whiteboard_tag: addons
13-
fidedi:
14-
contact: tbd
15-
description: Firefox Desktop Integration whiteboard tag
16-
enabled: true
17-
parameters:
18-
jira_project_key: FIDEDI
19-
whiteboard_tag: fidedi
20-
fidefe:
21-
contact: tbd
22-
description: Firefox Front End whiteboard tag
23-
enabled: true
24-
parameters:
25-
jira_project_key: FIDEFE
26-
whiteboard_tag: fidefe
27-
flowstate:
28-
allow_private: true
29-
30-
description: Flowstate whiteboard tag
31-
enabled: true
32-
parameters:
33-
jira_project_key: MR2
34-
whiteboard_tag: flowstate
35-
fxatps:
36-
contact: tbd
37-
description: Privacy & Security and Anti-Tracking Team whiteboard tag
38-
enabled: true
39-
parameters:
40-
jira_project_key: FXATPS
41-
whiteboard_tag: fxatps
42-
fxcm:
43-
contact: tbd
44-
description: Firefox Credential Management Team whiteboard tag
45-
enabled: true
46-
parameters:
47-
jira_project_key: FXCM
48-
whiteboard_tag: fxcm
49-
fxsync:
50-
contact: tbd
51-
description: Firefox Sync Team whiteboard tag
52-
enabled: true
53-
parameters:
54-
jira_project_key: SYNC
55-
whiteboard_tag: fxsync
56-
gv:
57-
contact: tbd
58-
description: GeckoView Team whiteboard tag
59-
enabled: true
60-
parameters:
61-
jira_project_key: ANDP
62-
whiteboard_tag: gv
63-
mv3:
64-
contact: tbd
65-
description: MV3 whiteboard tag
66-
enabled: true
67-
parameters:
68-
jira_project_key: WEBEXT
69-
whiteboard_tag: mv3
70-
nimbus:
71-
contact: tbd
72-
description: Nimbus whiteboard tag
73-
enabled: true
74-
parameters:
75-
jira_project_key: EXP
76-
whiteboard_tag: nimbus
77-
prodtest:
78-
contact: tbd
79-
description: ProdTest tag
80-
enabled: true
81-
parameters:
82-
jira_project_key: OSS
83-
whiteboard_tag: prodtest
84-
proton:
85-
contact: tbd
86-
description: Proton whiteboard tag for Firefox Frontend
87-
enabled: true
88-
parameters:
89-
jira_project_key: FIDEFE
90-
whiteboard_tag: proton
91-
relops:
92-
contact: tbd
93-
description: Release Operations Team Tag
94-
enabled: true
95-
parameters:
96-
jira_project_key: RELOPS
97-
whiteboard_tag: relops
98-
snt:
99-
contact: tbd
100-
description: Search/NewTab Team Tag
101-
enabled: true
102-
parameters:
103-
jira_project_key: SNT
104-
whiteboard_tag: snt
3+
addons:
4+
# action: src.jbi.whiteboard_actions.default
5+
contact: tbd
6+
description: Addons whiteboard tag for AMO Team
7+
enabled: true
8+
parameters:
9+
jira_project_key: WEBEXT
10+
whiteboard_tag: addons
11+
fidedi:
12+
contact: tbd
13+
description: Firefox Desktop Integration whiteboard tag
14+
enabled: true
15+
parameters:
16+
jira_project_key: FIDEDI
17+
whiteboard_tag: fidedi
18+
fidefe:
19+
contact: tbd
20+
description: Firefox Front End whiteboard tag
21+
enabled: true
22+
parameters:
23+
jira_project_key: FIDEFE
24+
whiteboard_tag: fidefe
25+
flowstate:
26+
allow_private: true
27+
28+
description: Flowstate whiteboard tag
29+
enabled: true
30+
parameters:
31+
jira_project_key: MR2
32+
whiteboard_tag: flowstate
33+
fxatps:
34+
contact: tbd
35+
description: Privacy & Security and Anti-Tracking Team whiteboard tag
36+
enabled: true
37+
parameters:
38+
jira_project_key: FXATPS
39+
whiteboard_tag: fxatps
40+
fxcm:
41+
contact: tbd
42+
description: Firefox Credential Management Team whiteboard tag
43+
enabled: true
44+
parameters:
45+
jira_project_key: FXCM
46+
whiteboard_tag: fxcm
47+
fxsync:
48+
contact: tbd
49+
description: Firefox Sync Team whiteboard tag
50+
enabled: true
51+
parameters:
52+
jira_project_key: SYNC
53+
whiteboard_tag: fxsync
54+
gv:
55+
contact: tbd
56+
description: GeckoView Team whiteboard tag
57+
enabled: true
58+
parameters:
59+
jira_project_key: ANDP
60+
whiteboard_tag: gv
61+
mv3:
62+
contact: tbd
63+
description: MV3 whiteboard tag
64+
enabled: true
65+
parameters:
66+
jira_project_key: WEBEXT
67+
whiteboard_tag: mv3
68+
nimbus:
69+
contact: tbd
70+
description: Nimbus whiteboard tag
71+
enabled: true
72+
parameters:
73+
jira_project_key: EXP
74+
whiteboard_tag: nimbus
75+
prodtest:
76+
contact: tbd
77+
description: ProdTest tag
78+
enabled: true
79+
parameters:
80+
jira_project_key: OSS
81+
whiteboard_tag: prodtest
82+
proton:
83+
contact: tbd
84+
description: Proton whiteboard tag for Firefox Frontend
85+
enabled: true
86+
parameters:
87+
jira_project_key: FIDEFE
88+
whiteboard_tag: proton
89+
relops:
90+
contact: tbd
91+
description: Release Operations Team Tag
92+
enabled: true
93+
parameters:
94+
jira_project_key: RELOPS
95+
whiteboard_tag: relops
96+
snt:
97+
contact: tbd
98+
description: Search/NewTab Team Tag
99+
enabled: true
100+
parameters:
101+
jira_project_key: SNT
102+
whiteboard_tag: snt

src/app/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sentry_sdk
1111
import uvicorn # type: ignore
1212
from fastapi import Body, Depends, FastAPI, Request
13+
from fastapi.encoders import jsonable_encoder
1314
from fastapi.responses import HTMLResponse, ORJSONResponse
1415
from fastapi.staticfiles import StaticFiles
1516
from fastapi.templating import Jinja2Templates
@@ -110,7 +111,7 @@ def get_whiteboard_tag(
110111
"""API for viewing whiteboard_tags and associated data"""
111112
if existing := actions.get(whiteboard_tag):
112113
return {whiteboard_tag: existing}
113-
return actions.all()
114+
return actions
114115

115116

116117
@app.get("/jira_projects/")
@@ -128,12 +129,11 @@ def powered_by_jbi(
128129
actions: Actions = Depends(configuration.get_actions),
129130
):
130131
"""API for `Powered By` endpoint"""
131-
entries = actions.all()
132132
context = {
133133
"request": request,
134134
"title": "Powered by JBI",
135-
"num_configs": len(entries),
136-
"data": entries,
135+
"num_configs": len(actions),
136+
"data": jsonable_encoder(actions),
137137
"enable_query": enabled,
138138
}
139139
return templates.TemplateResponse("powered_by_template.html", context)

src/app/configuration.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ def get_actions(
2222
jbi_config_file: str = f"config/config.{settings.env}.yaml",
2323
) -> Actions:
2424
"""Convert and validate YAML configuration to `Action` objects"""
25-
26-
with open(jbi_config_file, encoding="utf-8") as file:
27-
try:
28-
yaml_data = file.read()
29-
actions: Actions = Actions.parse_raw(yaml_data)
30-
return actions
31-
except ValidationError as exception:
32-
logger.exception(exception)
33-
raise ConfigError("Errors exist.") from exception
25+
try:
26+
actions: Actions = Actions.parse_file(jbi_config_file)
27+
return actions
28+
except ValidationError as exception:
29+
logger.exception(exception)
30+
raise ConfigError("Errors exist.") from exception

src/jbi/models.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Action(YamlModel):
1616
Action is the inner model for each action in the configuration file"""
1717

1818
action: str = "src.jbi.whiteboard_actions.default"
19+
contact: str
20+
description: str
1921
enabled: bool = False
2022
allow_private: bool = False
2123
parameters: dict = {}
@@ -57,22 +59,24 @@ class Config:
5759

5860
class Actions(YamlModel):
5961
"""
60-
Actions is the overall model for the list of `actions` in the configuration file
62+
Actions is the container model for the list of actions in the configuration file
6163
"""
6264

63-
actions: Mapping[str, Action]
65+
__root__: Mapping[str, Action]
66+
67+
def __len__(self):
68+
return len(self.__root__)
69+
70+
def __getitem__(self, item):
71+
return self.__root__[item]
6472

6573
def get(self, tag: Optional[str]) -> Optional[Action]:
6674
"""Lookup actions by whiteboard tag"""
67-
return self.actions.get(tag.lower()) if tag else None
68-
69-
def all(self):
70-
"""Return mapping of all actions"""
71-
return self.actions
75+
return self.__root__.get(tag.lower()) if tag else None
7276

73-
@validator("actions")
77+
@validator("__root__")
7478
def validate_action_matches_whiteboard(
75-
cls, actions
79+
cls, actions: Mapping[str, Action]
7680
): # pylint: disable=no-self-argument, no-self-use
7781
"""
7882
Validate that the inner actions are named as expected

tests/unit/app/bad-config.yaml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
---
2-
32
# Action Config
4-
actions:
5-
should-be-A:
6-
action: src.jbi.whiteboard_actions.default
7-
contact: tbd
8-
description: Mock Test Config
9-
enabled: true
10-
parameters:
11-
jira_project_key: KEY
12-
whiteboard_tag: A
3+
should-be-A:
4+
action: src.jbi.whiteboard_actions.default
5+
contact: tbd
6+
description: test config
7+
enabled: true
8+
parameters:
9+
jira_project_key: KEY
10+
whiteboard_tag: A

tests/unit/app/test_configuration.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ def test_actual_jbi_files():
2121

2222
def test_no_actions_fails():
2323
with pytest.raises(ValueError) as exc_info:
24-
Actions.parse_obj({"actions": {}})
24+
Actions.parse_obj({})
2525
assert "no actions configured" in str(exc_info.value)
2626

2727

2828
def test_unknown_module_fails():
2929
with pytest.raises(ValueError) as exc_info:
30-
Actions.parse_obj({"actions": {"x": {"action": "path.to.unknown"}}})
30+
Actions.parse_obj({"x": {"action": "path.to.unknown"}})
3131
assert "unknown action `path.to.unknown`" in str(exc_info.value)
3232

3333

3434
def test_bad_module_fails():
3535
with pytest.raises(ValueError) as exc_info:
36-
Actions.parse_obj({"actions": {"x": {"action": "src.jbi.runner"}}})
36+
Actions.parse_obj({"x": {"action": "src.jbi.runner"}})
3737
assert "action is not properly setup" in str(exc_info.value)

tests/unit/conftest.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,13 @@ def webhook_modify_private_example(
169169
def actions_example() -> Actions:
170170
return Actions.parse_obj(
171171
{
172-
"actions": {
173-
"devtest": {
174-
"action": "tests.unit.jbi.noop_action",
175-
"description": "Mocked config file",
176-
"parameters": {
177-
"whiteboard_tag": "devtest",
178-
},
179-
}
172+
"devtest": {
173+
"action": "tests.unit.jbi.noop_action",
174+
"contact": "tbd",
175+
"description": "test config",
176+
"parameters": {
177+
"whiteboard_tag": "devtest",
178+
},
180179
}
181180
}
182181
)

0 commit comments

Comments
 (0)