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
·176 lines (140 loc) · 6.26 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2015,2016 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):
super(TagChecker, self).__init__(*args, **kwargs)
self.factory = "openSUSE:Factory"
self.review_messages['declined'] = """
(This is a script, so report bugs)
The project you submitted to requires a bug tracker ID marked in the
.changes file. OBS supports several patterns, see
$ osc api /issue_trackers
See also https://en.opensuse.org/openSUSE:Packaging_Patches_guidelines#Current_set_of_abbreviations
Note that not all of the tags listed there are necessarily supported
by OBS on which this bot relies.
"""
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):
u = osc.core.makeurl(self.apiurl,
['source', a.src_project, a.src_package],
{'cmd': 'diff',
'onlyissues': '1',
'view': 'xml',
'opackage': a.tgt_package,
'oproject': a.tgt_project,
'expand': '1',
'rev': a.src_rev})
try:
f = osc.core.http_POST(u)
except (HTTPError, URLError):
if self.isNewPackage(a.tgt_project, a.tgt_package):
self.review_messages['accepted'] = 'New package'
return True
self.logger.debug('error loading diff, assume transient error')
return None
xml = ET.parse(f)
issues = len(xml.findall('./issues/issue'))
removed = len(xml.findall('./issues/issue[@state="removed"]'))
if issues == 0:
self.logger.debug("reject: diff contains no tags")
return False
if removed > 0:
self.review_messages['accepted'] = 'Warning: {} issues reference(s) removed'.format(removed)
return True
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 have an 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_checker.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)
self.clazz = TagChecker
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):
bot = ReviewBot.CommandLineInterface.setup_checker(self)
if self.options.factory:
bot.factory = self.options.factory
return bot
if __name__ == "__main__":
app = CommandLineInterface()
sys.exit(app.main())
# vim: sw=4 et