-
Notifications
You must be signed in to change notification settings - Fork 96
/
bugowner.py
executable file
·246 lines (197 loc) · 8.97 KB
/
bugowner.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2018 SUSE LLC
#
# 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.
from lxml import etree as ET
from collections import namedtuple
import sys
import cmdln
import logging
import osc.core
import ToolBase
logger = logging.getLogger()
FACTORY = "openSUSE:Factory"
Owner = namedtuple('Owner', ('kind', 'name'))
Person = namedtuple('Person', ('login', 'email', 'realname'))
class BugownerTool(ToolBase.ToolBase):
def __init__(self):
ToolBase.ToolBase.__init__(self)
self.project = None
self.reference_projects = None
self.package_metas = dict()
self.release_managers = None
self.persons = {}
def resolve_person(self, name):
if name in self.persons:
return self.persons[name]
url = self.makeurl(['person', name])
root = ET.fromstring(self.cached_GET(url))
person = Person(*[root.find(f'./{field}').text for field in Person._fields])
self.persons[name] = person
return person
def find_packages_with_missing_bugowner(self):
url = self.makeurl(['search', 'missing_owner'], {'project': self.project, 'filter': 'bugowner'})
root = ET.fromstring(self.cached_GET(url))
missing = []
for node in root.findall('missing_owner'):
missing.append(node.get('package'))
return missing
def find_owner(self, package, role='bugowner'):
# XXX: not actually looking for package but binary
# https://github.com/openSUSE/open-build-service/issues/4359
url = self.makeurl(['search', 'owner'], {'binary': package})
root = ET.fromstring(self.cached_GET(url))
ret = []
for node in root.findall(f'./owner/person[@role="{role}"]'):
ret.append(Owner('person', node.get('name')))
for node in root.findall(f'./owner/group[@role="{role}"]'):
ret.append(Owner('group', node.get('name')))
return ret
def add_bugowner(self, package, owner):
url = self.makeurl(['source', self.project, package, '_meta'])
root = ET.fromstring(self.cached_GET(url))
idname = 'userid' if owner.kind == 'person' else 'groupid'
# XXX: can't use 'and' here to filter for bugowner too
exists = root.findall(f'./{owner.kind}[@{idname}="{owner.name}"]')
for node in exists:
if node.get('role') == 'bugowner':
logger.debug("%s/%s already has %s %s", self.project, package, owner.kind, owner.name)
return
node = ET.SubElement(root, owner.kind)
node.set(idname, owner.name)
node.set('role', 'bugowner')
data = ET.tostring(root)
logger.debug(data)
self.http_PUT(url, data=data)
def package_get_last_committer(self, package):
project = self.project
srcrev = osc.core.get_source_rev(self.apiurl, project, package)
if 'requestid' in srcrev:
r = osc.core.get_request(self.apiurl, srcrev['requestid'])
user = r.statehistory[0].who
else:
user = srcrev['user']
if self.is_release_manager(user):
logging.debug(f"{package} was last touched by {user}, ignored.")
return None
return [Owner('person', user)]
def is_release_manager(self, name):
if self.release_managers is None:
self.release_managers = set()
url = self.makeurl(['group', 'sle-release-managers'])
root = ET.fromstring(self.cached_GET(url))
for node in root.findall('.//person[@userid]'):
self.release_managers.add(node.get('userid'))
logger.debug("release managers %s", self.release_managers)
return name in self.release_managers
class CommandLineInterface(ToolBase.CommandLineInterface):
def __init__(self, *args, **kwargs):
ToolBase.CommandLineInterface.__init__(self, args, kwargs)
def get_optparser(self):
parser = ToolBase.CommandLineInterface.get_optparser(self)
parser.add_option('-p', '--project', dest='project', metavar='PROJECT',
help=f'project to process (default: {FACTORY})',
default=FACTORY)
parser.add_option('--reference-project', metavar='PROJECT',
action='append', help='reference project')
return parser
def setup_tool(self):
tool = BugownerTool()
tool.project = self.options.project
return tool
def do_missing(self, subcmd, opts):
"""${cmd_name}: find packages with missing bugowner
Beware of https://github.com/openSUSE/open-build-service/issues/4172
when using this with SLE service packs or update projects
${cmd_usage}
${cmd_option_list}
"""
pkgs = self.tool.find_packages_with_missing_bugowner()
for p in pkgs:
print(p)
@cmdln.option('-r', '--role', metavar='ROLE', help='role to look up', default="bugowner")
@cmdln.option('-s', '--set', action='store_true', help='set bugowner in specified project')
@cmdln.option('--request', action='store_true', help='print osc request lines')
@cmdln.option('--employee', action='store_true', help='only filter employees')
def do_owner(self, subcmd, opts, *package):
"""${cmd_name}: find owners of the given package
${cmd_usage}
${cmd_option_list}
"""
for p in package:
owners = self.tool.find_owner(p, opts.role)
if not owners:
logger.info("%s does not have owners", p)
continue
for o in owners:
logger.info("%s -> %s %s", p, o.kind, o.name)
if opts.set:
self.tool.add_bugowner(p, o)
elif opts.request:
name = o.name
if o.kind == 'group':
name = 'group:' + name
print("osc -A {} reqbs -r bugowner -m 'copy bug owner from previous codestream' {} {} {}".format(
self.tool.apiurl, self.tool.project, p, name))
elif opts.employee:
if o.kind != 'person':
logger.debug('%s not a person', o.name)
continue
person = self.tool.resolve_person(o.name)
if person.email.endswith('@suse.com'):
print(p, o.name)
else:
logger.debug('%s skipped', o.name)
def do_addbugowner(self, subcmd, opts, package, *persons):
"""${cmd_name}: add person as bugowner unless already set
${cmd_usage}
${cmd_option_list}
"""
for p in persons:
o = Owner('person', p)
logger.info("%s -> %s %s", package, o.kind, o.name)
self.tool.add_bugowner(p, o)
@cmdln.option('--set', action='store_true',
help='request bugowner')
@cmdln.option('--request', action='store_true', help='print osc request lines')
def do_lastsubmitter(self, subcmd, opts, *packages):
"""${cmd_name}: show last committer for packages
excludes release managers
${cmd_name} PROJECT PACKAGE...
${cmd_option_list}
"""
for p in packages:
owners = self.tool.package_get_last_committer(p)
if not owners:
logger.info("%s does not have owners", p)
continue
for o in owners:
logger.info("%s -> %s %s", p, o.kind, o.name)
if opts.set:
self.tool.add_bugowner(p, o)
if opts.request:
name = o.name
if o.kind == 'group':
name = 'group:' + name
print("osc -A {} reqbs -r bugowner -m 'add last submitter as bug owner' {} {} {}".format(
self.tool.apiurl, self.tool.project, p, name))
if __name__ == "__main__":
app = CommandLineInterface()
sys.exit(app.main())