1# Copyright 2016 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""URL endpoint to record bad bisect.""" 6 7import json 8 9from google.appengine.api import users 10from google.appengine.ext.webapp import util 11 12from dashboard import quick_logger 13from dashboard import request_handler 14from dashboard import utils 15from dashboard import xsrf 16from dashboard.models import try_job 17 18 19class BadBisectHandler(request_handler.RequestHandler): 20 21 def get(self): 22 """Renders bad_bisect.html.""" 23 if self.request.get('list'): 24 self.response.out.write(_PrintRecentFeedback()) 25 return 26 self._RenderForm() 27 28 @util.login_required 29 def _RenderForm(self): 30 if not utils.IsValidSheriffUser(): 31 self._RenderError('No permission.') 32 return 33 if not self.request.get('try_job_id'): 34 self._RenderError('Missing try_job_id.') 35 return 36 37 self.RenderHtml('bad_bisect.html', 38 {'try_job_id': self.request.get('try_job_id')}) 39 40 @xsrf.TokenRequired 41 def post(self): 42 """Handles post requests from bad_bisect.html.""" 43 if not utils.IsValidSheriffUser(): 44 self._RenderError('No permission.') 45 return 46 if not self.request.get('try_job_id'): 47 self._RenderError('Missing try_job_id.') 48 return 49 50 try_job_id = int(self.request.get('try_job_id')) 51 job = try_job.TryJob.get_by_id(try_job_id) 52 if not job: 53 self._RenderError('TryJob doesn\'t exist.') 54 return 55 56 user = users.get_current_user() 57 email = user.email() 58 if not job.bad_result_emails: 59 job.bad_result_emails = set() 60 if email not in job.bad_result_emails: 61 job.bad_result_emails.add(email) 62 job.put() 63 _LogFeedback(try_job_id, email) 64 65 self.RenderHtml('result.html', { 66 'headline': 'Confirmed bad bisect. Thank you for reporting.'}) 67 68 def _RenderError(self, error): 69 self.RenderHtml('result.html', {'errors': [error]}) 70 71 72def _LogFeedback(try_job_id, email): 73 formatter = quick_logger.Formatter() 74 logger = quick_logger.QuickLogger('bad_bisect', 'report', formatter) 75 message = '%s marked try job %d.' % (email, try_job_id) 76 logger.Log(message) 77 logger.Save() 78 79def _PrintRecentFeedback(): 80 jobs = try_job.TryJob.query().fetch() 81 results = [] 82 for job in jobs: 83 if not job.bad_result_emails: 84 continue 85 results.append({ 86 'bad_result_emails': list(job.bad_result_emails), 87 'try_job_id': job.key.id(), 88 'status': job.results_data.get('status'), 89 'buildbot_log_url': job.results_data.get('buildbot_log_url'), 90 'bug_id': job.results_data.get('bug_id'), 91 'score': job.results_data.get('score'), 92 }) 93 return json.dumps(results) 94