github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/experiment/bilge.py (about)

     1  #!/usr/bin/env python2
     2  
     3  # Copyright 2017 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  """Cancel old, duplicate tests of the same pr.
    18  
    19  This can happen with repeated '/test all' commands, or
    20  even something as simple as pushing new code while builds are already queued.
    21  """
    22  
    23  import sys
    24  
    25  import requests
    26  
    27  
    28  def prune(host):
    29      """Cancel old, duplicate tests of the same pr."""
    30      queue = requests.get('http://%s/queue/api/json' % host).json()
    31  
    32      dupes = {}
    33      for item in queue['items']:
    34          params = {}
    35          for action in item['actions']:
    36              if 'parameters' in action:
    37                  params = {d['name']: d['value'] for d in action['parameters']}
    38          if 'PULL_NUMBER' in params:
    39              name = item['task']['name']
    40              queue_id = item['id']
    41              since = item['inQueueSince']
    42              dupes.setdefault((name, params['PULL_NUMBER']), []).append((since, queue_id, params))
    43  
    44      for key, val in dupes.iteritems():
    45          if len(val) < 2:
    46              continue
    47          val.sort()
    48          print '===', key
    49          for pull in val[:-1]:
    50              print pull
    51              _resp = requests.post('http://%s/queue/cancelItem?id=%d' % (host, pull[1]))
    52          print 'GOOD:', val[-1]
    53  
    54  
    55  if __name__ == '__main__':
    56      HOST = sys.argv[1] if len(sys.argv) > 1 else 'localhost:8080'
    57      prune(HOST)