forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_tags_in_requests.py
executable file
·186 lines (151 loc) · 6.8 KB
/
check_tags_in_requests.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2015 SUSE Linux GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import re
import osc.conf
import osc.core
try:
from xml.etree import cElementTree as ET
except ImportError:
import cElementTree as ET
try:
from urllib.error import HTTPError
except ImportError:
# python 2.x
from urllib2 import HTTPError
try:
from urllib.error import URLError
except ImportError:
# python 2.x
from urllib2 import URLError
import ReviewBot
import check_source_in_factory
class TagChecker(ReviewBot.ReviewBot):
""" simple bot that checks that a submit request has corrrect tags specified
"""
def __init__(self, *args, **kwargs):
self.factory = None
if 'factory' in kwargs:
self.factory = kwargs['factory']
del kwargs['factory']
if self.factory is None:
self.factory = "openSUSE:Factory"
super(TagChecker, self).__init__(*args, **kwargs)
needed_tags = [r'bnc#[0-9]+',
r'cve-[0-9]{4}-[0-9]+',
r'fate#[0-9]+',
r'boo#[0-9]+',
r'bsc#[0-9]+',
r'bgo#[0-9]+']
self.needed_tags_re = [re.compile(tag, re.IGNORECASE) for tag in needed_tags]
self.review_messages['declined'] = """
(This is a script running, so report bugs)
We require a ID marked in .changes file to detect later if the changes
are also merged into openSUSE:Factory. We accept bnc#, cve#, fate#, boo#, bsc# and bgo# atm.
Note: there is no whitespace behind before or after the number sign
(compare with the packaging policies)
"""
def isNewPackage(self, tgt_project, tgt_package):
try:
self.logger.debug("package_meta %s %s/%s" % (self.apiurl, tgt_project, tgt_package))
osc.core.show_package_meta(self.apiurl, tgt_project, tgt_package)
except (HTTPError, URLError):
return True
return False
def checkTagInRequest(self, req, a):
is_new = False
u = osc.core.makeurl(self.apiurl,
['source', a.tgt_project, a.tgt_package],
{'cmd': 'diff',
'onlyissues': '1',
'view': 'xml',
'opackage': a.src_package,
'oproject': a.src_project,
'orev': a.src_rev})
try:
f = osc.core.http_POST(u)
except (HTTPError, URLError):
is_new = self.isNewPackage(a.tgt_project, a.tgt_package)
# in case the quest have not the matched revision in Factory
# and it is a new package to target project, then leave it to
# human review
if is_new:
self.logger.info("New package to %s and have not the matched revision in Factory"%a.tgt_project)
return True
xml = ET.parse(f)
has_changes = list(xml.findall('./issues/issue'))
if not has_changes:
self.logger.debug("reject: diff contains no tags")
return False
return True
def checkTagNotRequired(self, req, a):
# if there is no diff, no tag is required
diff = osc.core.request_diff(self.apiurl, req.reqid)
if not diff:
return True
# 1) A tag is not required only if the package is
# already in Factory with the same revision,
# and the package is being introduced, not updated
# 2) A new package must be have a issue tag
factory_checker = check_source_in_factory.FactorySourceChecker(apiurl=self.apiurl,
dryrun=self.dryrun,
logger=self.logger,
user=self.review_user,
group=self.review_group,
factory=self.factory)
factory_ok = factory_checker.check_source_submission(a.src_project, a.src_package, a.src_rev,
a.tgt_project, a.tgt_package)
return factory_ok
def checkTagNotRequiredOrInRequest(self, req, a):
if self.checkTagNotRequired(req, a):
return True
return self.checkTagInRequest(req, a)
def check_action_submit(self, req, a):
return self.checkTagNotRequiredOrInRequest(req, a)
def check_action_maintenance_incident(self, req, a):
return self.checkTagInRequest(req, a)
def check_action_maintenance_release(self, req, a):
return self.checkTagInRequest(req, a)
def check_action__default(self, req, a):
# accept all other requests
self.logger.debug("auto accept request type %s"%a.type)
return True
class CommandLineInterface(ReviewBot.CommandLineInterface):
def __init__(self, *args, **kwargs):
ReviewBot.CommandLineInterface.__init__(self, args, kwargs)
def get_optparser(self):
parser = ReviewBot.CommandLineInterface.get_optparser(self)
parser.add_option("--factory", metavar="project", help="the openSUSE Factory project")
return parser
def setup_checker(self):
apiurl = osc.conf.config['apiurl']
if apiurl is None:
raise osc.oscerr.ConfigError("missing apiurl")
return TagChecker(apiurl=apiurl,
factory=self.options.factory,
dryrun=self.options.dry,
group=self.options.group,
logger=self.logger)
if __name__ == "__main__":
app = CommandLineInterface()
sys.exit(app.main())
# vim: sw=4 et