]> git.ipfire.org Git - pbs.git/blob - src/web/builds.py
builds: Drop changing priority UI
[pbs.git] / src / web / builds.py
1 #!/usr/bin/python
2
3 import tornado.web
4
5 from . import base
6 from . import ui_modules
7
8 class BuildBaseHandler(base.BaseHandler):
9 def get_build(self, uuid):
10 build = self.backend.builds.get_by_uuid(uuid)
11 if not build:
12 raise tornado.web.HTTPError(404, "No such build: %s" % uuid)
13
14 return build
15
16
17 class IndexHandler(base.BaseHandler):
18 def get(self):
19 # Fetch the most recent builds
20 builds = self.backend.builds.get_recent(limit=25)
21
22 self.render("builds/index.html", builds=builds)
23
24
25 class ShowHandler(BuildBaseHandler):
26 def get(self, uuid):
27 build = self.backend.builds.get_by_uuid(uuid)
28 if not build:
29 raise tornado.web.HTTPError(404, "Could not find build %s" % uuid)
30
31 # Cache the log.
32 log = build.get_log()
33
34 # Bugs.
35 bugs = build.get_bugs()
36
37 self.render("builds/show.html", build=build, log=log, pkg=build.pkg,
38 distro=build.distro, bugs=bugs, repo=build.repo)
39
40
41 class BuildDeleteHandler(BuildBaseHandler):
42 @tornado.web.authenticated
43 def get(self, uuid):
44 build = self.get_build(uuid)
45
46 # Check if the user has got sufficient rights to do this modification.
47 if not build.has_perm(self.current_user):
48 raise tornado.web.HTTPError(403)
49
50 # Check if the user confirmed the action.
51 confirmed = self.get_argument("confirmed", None)
52 if confirmed:
53 # Save the name of the package to redirect the user
54 # to the other packages of that name.
55 package_name = build.pkg.name
56
57 # Delete the build and everything that comes with it.
58 with self.db.transaction():
59 build.delete()
60
61 return self.redirect("/package/%s" % package_name)
62
63 self.render("build-delete.html", build=build)
64
65
66 class BuildBugsHandler(base.BaseHandler):
67 @tornado.web.authenticated
68 def get(self, uuid):
69 build = self.backend.builds.get_by_uuid(uuid)
70 if not build:
71 raise tornado.web.HTTPError(404, "No such build: %s" % uuid)
72
73 # Check if the user has got the right to alter this build.
74 if not build.has_perm(self.current_user):
75 raise tornado.web.HTTPError(403)
76
77 # Bugs.
78 fixed_bugs = build.get_bugs()
79 open_bugs = []
80
81 for bug in self.backend.bugzilla.get_bugs_from_component(build.pkg.name):
82 if bug in fixed_bugs:
83 continue
84
85 open_bugs.append(bug)
86
87 self.render("build-bugs.html", build=build, pkg=build.pkg,
88 fixed_bugs=fixed_bugs, open_bugs=open_bugs)
89
90 @tornado.web.authenticated
91 def post(self, uuid):
92 build = self.backend.builds.get_by_uuid(uuid)
93 if not build:
94 raise tornado.web.HTTPError(404, "No such build: %s" % uuid)
95
96 # Check if the user has got the right to alter this build.
97 if not build.has_perm(self.current_user):
98 raise tornado.web.HTTPError(403)
99
100 action = self.get_argument("action", None)
101 bugid = self.get_argument("bugid")
102
103 # Convert the bug id to integer.
104 try:
105 bugid = int(bugid)
106 except ValueError:
107 raise tornado.web.HTTPError(400, "Bad bug id given: %s" % bugid)
108
109 if action == "add":
110 # Add bug to the build.
111 build.add_bug(bugid, user=self.current_user)
112
113 elif action == "remove":
114 # Remove bug from the build.
115 build.rem_bug(bugid, user=self.current_user)
116
117 else:
118 raise tornado.web.HTTPError(400, "Unhandled action: %s" % action)
119
120 self.redirect("/build/%s/bugs" % build.uuid)
121
122
123 class BuildStateHandler(base.BaseHandler):
124 def get(self, uuid):
125 build = self.backend.builds.get_by_uuid(uuid)
126 if not build:
127 raise tornado.web.HTTPError(404, "No such build: %s" % uuid)
128
129 self.render("build-state.html", build=build)
130
131 @tornado.web.authenticated
132 def post(self, uuid):
133 build = self.backend.builds.get_by_uuid(uuid)
134 if not build:
135 raise tornado.web.HTTPError(404, "No such build: %s" % uuid)
136
137 # Check if user has the right to perform this action.
138 if not build.has_perm(self.current_user):
139 raise tornado.web.HTTPError(403, "User is not allowed to perform this action")
140
141 # Check if given state is valid.
142 state = self.get_argument("state", None)
143 if not state in ("broken", "unbreak", "obsolete"):
144 raise tornado.web.HTTPError(400, "Invalid argument given: %s" % state)
145
146 # XXX this is not quite accurate
147 if state == "unbreak":
148 state = "stable"
149
150 rem_from_repo = self.get_argument("rem_from_repo", False)
151 if rem_from_repo == "on":
152 rem_from_repo = True
153
154 # Perform the state change.
155 build.update_state(state, user=self.current_user, remove=rem_from_repo)
156
157 self.redirect("/build/%s" % build.uuid)
158
159
160 class BuildDetailCommentHandler(base.BaseHandler):
161 @tornado.web.authenticated
162 def post(self, uuid):
163 build = self.backend.builds.get_by_uuid(uuid)
164
165 if not build:
166 raise tornado.web.HTTPError(404, "Build not found")
167
168 vote = self.get_argument("vote", "none")
169
170 if vote == "up":
171 vote = 1
172 elif vote == "down":
173 vote = -1
174 else:
175 vote = 0
176
177 text = self.get_argument("text", "")
178
179 # Add a new comment to the build.
180 if text or vote:
181 build.add_comment(self.current_user, text, vote)
182
183 # Redirect to the build detail page.
184 self.redirect("/build/%s" % build.uuid)
185
186
187 class BuildManageHandler(base.BaseHandler):
188 @tornado.web.authenticated
189 def get(self, uuid):
190 build = self.backend.builds.get_by_uuid(uuid)
191 if not build:
192 raise tornado.web.HTTPError(404, "Build not found: %s" % uuid)
193
194 mode = "user"
195 if self.current_user.is_admin():
196 mode = self.get_argument("mode", "user")
197
198 # Get the next repo.
199 if build.repo:
200 next_repo = build.repo.__next__
201 else:
202 next_repo = build.distro.first_repo
203
204 self.render("build-manage.html", mode=mode, build=build,
205 distro=build.distro, repo=build.repo, next_repo=next_repo)
206
207 @tornado.web.authenticated
208 def post(self, uuid):
209 build = self.backend.builds.get_by_uuid(uuid)
210 if not build:
211 raise tornado.web.HTTPError(404, "Build not found: %s" % uuid)
212
213 # check for sufficient permissions
214 if not build.has_perm(self.current_user):
215 raise tornado.web.HTTPError(403)
216
217 action = self.get_argument("action")
218 assert action in ("push", "unpush")
219
220 current_repo = build.repo
221
222 if action == "unpush":
223 current_repo.rem_build(build, user=self.current_user)
224
225 elif action == "push":
226 repo_name = self.get_argument("repo")
227 next_repo = build.distro.get_repo(repo_name)
228
229 if not next_repo:
230 raise tornado.web.HTTPError(404, "No such repository: %s" % next_repo)
231
232 if not self.current_user.is_admin():
233 if not distro.repo.__next__ == next_repo:
234 raise tornado.web.HTTPError(403)
235
236 if current_repo:
237 current_repo.move_build(build, next_repo, user=self.current_user)
238 else:
239 next_repo.add_build(build, user=self.current_user)
240
241 self.redirect("/build/%s" % build.uuid)
242
243
244 class BuildWatchersHandler(base.BaseHandler):
245 def get(self, uuid):
246 build = self.backend.builds.get_by_uuid(uuid)
247
248 if not build:
249 raise tornado.web.HTTPError(404, "Build not found")
250
251 # Get a list of all watchers and sort them by their realname.
252 watchers = build.get_watchers()
253 watchers.sort(key=lambda watcher: watcher.realname)
254
255 self.render("builds-watchers-list.html", build=build, watchers=watchers)
256
257
258 class BuildWatchersAddHandler(base.BaseHandler):
259 @tornado.web.authenticated
260 def get(self, uuid, error_msg=None):
261 build = self.backend.builds.get_by_uuid(uuid)
262
263 if not build:
264 raise tornado.web.HTTPError(404, "Build not found")
265
266 # Get a list of all users that are currently watching this build.
267 watchers = build.get_watchers()
268
269 self.render("builds-watchers-add.html", error_msg=error_msg,
270 build=build, users=self.backend.users, watchers=watchers)
271
272 @tornado.web.authenticated
273 def post(self, uuid):
274 build = self.backend.builds.get_by_uuid(uuid)
275
276 if not build:
277 raise tornado.web.HTTPError(404, "Build not found")
278
279 # Get the user id of the new watcher.
280 user_id = self.current_user.id
281
282 if self.current_user.is_admin():
283 user_id = self.get_argument("user_id", self.current_user.id)
284 assert user_id
285
286 user = self.backend.users.get_by_id(user_id)
287 if not user:
288 _ = self.locale.translate
289 error_msg = _("User not found.")
290
291 return self.get(uuid, error_msg=error_msg)
292
293 # Actually add the user to the list of watchers.
294 build.add_watcher(user)
295
296 # Send user back to the build detail page.
297 self.redirect("/build/%s" % build.uuid)
298
299
300 class BuildListHandler(base.BaseHandler):
301 def get(self):
302 builder = self.get_argument("builder", None)
303 state = self.get_argument("state", None)
304
305 builds = self.backend.builds.get_latest(state=state, builder=builder,
306 limit=25)
307
308 self.render("build-list.html", builds=builds)
309
310
311 class ListModule(ui_modules.UIModule):
312 def render(self, builds):
313 return self.render_string("builds/modules/list.html", builds=builds)