forked from GothenburgBitFactory/bugwarrior
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
112 lines (86 loc) · 3.27 KB
/
Copy pathbase.py
File metadata and controls
112 lines (86 loc) · 3.27 KB
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
111
112
import contextlib
import typing
import unittest.mock
from bugwarrior import config, services
from bugwarrior.collect import get_service_instances
from bugwarrior.config import validation
from bugwarrior.config.load import format_config
from .services.base import get_mock_service
class DumbConfig(config.ServiceConfig):
service: typing.Literal["test"] = "test"
KEYRING_SERVICE = 'test://'
import_labels_as_tags: bool = False
label_template: str = "{{label}}"
class DumbIssue(services.Issue):
URL = "dumburl"
TYPE = "dumbtype"
UDAS = {
URL: {"type": "string", "label": "Dumb URL"},
TYPE: {"type": "string", "label": "Dumb Type"},
}
UNIQUE_KEY = (URL,)
PRIORITY_MAP: dict = {}
def get_default_description(self):
return self.build_default_description(
title=self.record.get("title", ""),
url=self.record.get("url", ""),
number=self.record.get("number", ""),
)
def to_taskwarrior(self):
return {
"project": self.extra.get("project"),
"priority": self.config.default_priority,
"annotations": self.extra.get("annotations", []),
"tags": self.get_tags_from_labels(self.record.get("labels", [])),
self.URL: self.record.get("url", ""),
self.TYPE: self.extra.get("type", "issue"),
}
class DumbService(services.Service):
API_VERSION = services.LATEST_API_VERSION
ISSUE_CLASS = DumbIssue
CONFIG_SCHEMA = DumbConfig
def issues(self):
raise NotImplementedError
def make_issue(general_overrides=None, config_overrides=None):
service = get_mock_service(
DumbService, config_overrides, general_overrides=general_overrides
)
return service.get_issue_for_record({})
#: Modules that import get_service by name and must be patched together so the
#: fake service resolves consistently across config loading, collection, and db.
_GET_SERVICE_MODULES = (
'bugwarrior.config.schema',
'bugwarrior.config.validation',
'bugwarrior.config',
'bugwarrior.db',
'bugwarrior.collect',
)
@contextlib.contextmanager
def register_services(mapping=None):
"""
Make fake services resolvable via get_service for the duration of a block.
Patches get_service in every module that imports it so that orchestration
code (config loading, collection, db) resolves the given name-to-class
mapping instead of the real entry points. Defaults to mapping the "test"
service to DumbService.
"""
mapping = mapping or {'test': DumbService}
def fake_get_service(name):
try:
return mapping[name]
except KeyError:
raise ValueError(
f"Configured service '{name}' not found. "
"Is it installed? Or misspelled?"
)
with contextlib.ExitStack() as stack:
for module in _GET_SERVICE_MODULES:
stack.enter_context(
unittest.mock.patch(f'{module}.get_service', fake_get_service)
)
yield
def validate(config) -> validation.Config:
formatted_config = format_config(config)
return validation.validate_config(formatted_config, 'general', 'configpath')
def get_validated_service(config):
return get_service_instances(validate(config))[0]