1# Copyright 2015 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"""Provides the web interface for adding and removing bug labels."""
6
7import json
8
9from dashboard import request_handler
10from dashboard import xsrf
11from dashboard.models import bug_label_patterns
12
13
14class EditBugLabelsHandler(request_handler.RequestHandler):
15  """Handles editing the info about perf sheriff rotations."""
16
17  def get(self):
18    """Renders the UI with all of the forms."""
19    patterns_dict = bug_label_patterns.GetBugLabelPatterns()
20    self.RenderHtml('edit_bug_labels.html', {
21        'bug_labels': sorted(patterns_dict),
22        'bug_labels_json': json.dumps(patterns_dict, indent=2, sort_keys=True)
23    })
24
25  @xsrf.TokenRequired
26  def post(self):
27    """Updates the sheriff configurations.
28
29    Each form on the edit sheriffs page has a hidden field called action, which
30    tells us which form was submitted. The other particular parameters that are
31    expected depend on which form was submitted.
32    """
33    action = self.request.get('action')
34    if action == 'add_buglabel_pattern':
35      self._AddBuglabelPattern()
36    if action == 'remove_buglabel_pattern':
37      self._RemoveBuglabelPattern()
38
39  def _AddBuglabelPattern(self):
40    """Adds a bug label to be added to a group of tests.
41
42    Request parameters:
43      buglabel_to_add: The bug label, which is a BugLabelPattern entity name.
44      pattern: A test path pattern.
45    """
46    label = self.request.get('buglabel_to_add')
47    pattern = self.request.get('pattern')
48    bug_label_patterns.AddBugLabelPattern(label, pattern)
49    self.RenderHtml('result.html', {
50        'headline': 'Added label %s' % label,
51        'results': [{'name': 'Pattern', 'value': pattern}]
52    })
53
54  def _RemoveBuglabelPattern(self):
55    """Removes a BugLabelPattern so that the label no longer applies.
56
57    Request parameters:
58      buglabel_to_remove: The bug label, which is the name of a
59      BugLabelPattern entity.
60    """
61    label = self.request.get('buglabel_to_remove')
62    bug_label_patterns.RemoveBugLabel(label)
63    self.RenderHtml('result.html', {
64        'headline': 'Deleted label %s' % label
65    })
66