forked from mozilla/jira-bugzilla-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.py
110 lines (90 loc) · 3.2 KB
/
default.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
The `default` action takes a list of steps from configuration and executes them
in chain.
The `runner` will call this action with an initialized context. When a Bugzilla ticket
is created or updated, its `operation` attribute will be `Operation.CREATE` or `Operation.UPDATE`,
and when a comment is posted, it will be set to `Operation.COMMENT`.
"""
import logging
from typing import Optional
from jbi import ActionResult, Operation
from jbi.actions import steps as steps_module
from jbi.environment import get_settings
from jbi.models import ActionContext
settings = get_settings()
logger = logging.getLogger(__name__)
JIRA_REQUIRED_PERMISSIONS = {
"ADD_COMMENTS",
"CREATE_ISSUES",
"DELETE_ISSUES",
"EDIT_ISSUES",
}
DEFAULT_STEPS = {
"new": [
"create_issue",
"maybe_delete_duplicate",
"add_link_to_bugzilla",
"add_link_to_jira",
],
"existing": [
"update_issue",
"add_jira_comments_for_changes",
],
"comment": [
"create_comment",
],
}
def groups2operation(steps):
"""In the configuration files, the steps are grouped by `new`, `existing`,
and `comment`. Internally, this correspond to enums of `Operation`.
This helper remaps the list of steps.
"""
group_to_operation = {
"new": Operation.CREATE,
"existing": Operation.UPDATE,
"comment": Operation.COMMENT,
}
try:
by_operation = {
group_to_operation[entry]: steps_list for entry, steps_list in steps.items()
}
except KeyError as err:
raise ValueError(f"Unsupported entry in `steps`: {err}") from err
return by_operation
def init(
jira_project_key,
steps: Optional[dict[str, list[str]]] = None,
**kwargs,
):
"""Function that takes required and optional params and returns a callable object"""
# Merge specified steps with default ones.
steps = {**DEFAULT_STEPS, **(steps or {})}
steps_by_operation = groups2operation(steps)
# Turn the steps strings into references to functions of the `jbi.actions.steps` module.
steps_callables = {
group: [getattr(steps_module, step_str) for step_str in steps_list]
for group, steps_list in steps_by_operation.items()
}
return Executor(jira_project_key=jira_project_key, steps=steps_callables, **kwargs)
class Executor:
"""Callable class that encapsulates the default action."""
def __init__(self, steps, **parameters):
"""Initialize Executor Object"""
self.steps = steps
self.parameters = parameters
def __call__(self, context: ActionContext) -> ActionResult:
"""Called from `runner` when the action is used."""
responses = tuple() # type: ignore
for step in self.steps[context.operation]:
context, step_responses = step(context=context, **self.parameters)
for response in step_responses:
logger.debug(
"Received %s",
response,
extra={
"response": response,
**context.dict(),
},
)
responses += step_responses
return True, {"responses": responses}