k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/gubernator/pull_request.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2016 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  
    18  def builds_to_table(jobs):
    19      """
    20      Convert a list of builds into arguments suitable for rendering.
    21  
    22      Args:
    23          jobs: a dict of {job: [(build, started.json, finished.json), ...]}
    24      Returns:
    25          max_builds: the number of build columns
    26          headings: a list of [(version, column width, timestamp)]
    27          rows: a list of [(build, [(number, status) or None])]
    28      """
    29      # pylint: disable=too-many-locals
    30  
    31      def commit(started, finished):
    32          if 'pull' in started:
    33              return started['pull'].split(':')[-1]
    34          if 'version' in started:
    35              return started['version'].split('+')[-1]
    36          if finished and 'revision' in finished:
    37              return finished['revision']
    38          return 'unknown'
    39  
    40      # Compute the headings first -- versions and their maximum build counts.
    41  
    42      versions = {}       # {version: {job: build_count}}
    43      version_start = {}  # {version: first_build_start_time}
    44      for job, builds in jobs.iteritems():
    45          for build, started, finished in builds:
    46              if not started:
    47                  continue
    48              version = commit(started, finished)
    49              if not version:
    50                  continue
    51              versions.setdefault(version, {}).setdefault(job, 0)
    52              versions[version][job] += 1
    53              begin = int(started['timestamp'])
    54              version_start[version] = min(begin, version_start.get(version, begin))
    55  
    56      version_widths = {version: max(jobs.values()) for version, jobs in versions.iteritems()}
    57      versions_ordered = sorted(versions, key=lambda v: version_start[v], reverse=True)
    58      version_colstart = {}
    59      cur = 0
    60      for version in versions_ordered:
    61          version_colstart[version] = cur
    62          cur += version_widths[version]
    63  
    64      max_builds = cur
    65      headings = [(version, version_widths[version], version_start[version])
    66                  for version in versions_ordered]
    67  
    68      rows = []
    69      for job, builds in sorted(jobs.iteritems()):
    70          row = []
    71          n = 0
    72          for build, started, finished in builds:
    73              if not started or not commit(started, finished):
    74                  minspan = 0
    75              else:
    76                  minspan = version_colstart[commit(started, finished)]
    77              while n < minspan:
    78                  row.append(None)
    79                  n += 1
    80              row.append((build, finished['result'] if finished else 'unfinished'))
    81              n += 1
    82          rows.append((job, row))
    83  
    84      return max_builds, headings, rows