forked from RedHatProductSecurity/osidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.py
59 lines (49 loc) · 1.85 KB
/
save.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
from collectors.bzimport.collectors import BugzillaConnector
from osidb.models import Flaw
from .query import BugzillaQueryBuilder
class BugzillaSaver(BugzillaConnector):
"""
Bugzilla flaw bug save handler
flaw validity is assumed and not checked
"""
class UnsaveableFlaw(Exception):
"""
error caused by attempt to save a flaw which cannot be saved
either by its nature or due to the current saver capabilities
"""
pass
def __init__(self, flaw):
"""
init stuff
"""
self.flaw = flaw
def save(self):
"""
generic save serving as class entry point
which calls create or update handler to continue
returns an updated flaw instance (without saving)
"""
return self.create() if self.flaw.bz_id is None else self.update()
def create(self):
"""
create flaw in Bugilla
"""
bugzilla_query_builder = BugzillaQueryBuilder(self.flaw)
response = self.bz_conn.createbug(bugzilla_query_builder.query)
self.flaw.meta_attr["bz_id"] = response.id
return self.flaw
def update(self):
"""
update flaw in Bugzilla
"""
# TODO flaws with multiple CVEs are non-trivial to update
# and save back to Bugzilla so let us restrict this for now
if Flaw.objects.filter(meta_attr__bz_id=self.flaw.bz_id).count() > 1:
raise self.UnsaveableFlaw(
"Unable to save a flaw with multiple CVEs to Bugzilla "
"due to an ambigous N to 1 OSIDB to Buzilla flaw mapping"
)
old_flaw = Flaw.objects.get(uuid=self.flaw.uuid)
bugzilla_query_builder = BugzillaQueryBuilder(self.flaw, old_flaw)
self.bz_conn.update_bugs([self.flaw.bz_id], bugzilla_query_builder.query)
return self.flaw