github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/prow.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mungers
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"k8s.io/test-infra/mungegithub/mungers/mungerutil"
    23  )
    24  
    25  // xref k8s.io/test-infra/prow/cmd/deck/jobs.go
    26  type prowJob struct {
    27  	Type     string `json:"type"`
    28  	Repo     string `json:"repo"`
    29  	Refs     string `json:"refs"`
    30  	Number   int    `json:"number"`
    31  	BuildID  string `json:"build_id"`
    32  	Job      string `json:"job"`
    33  	Finished string `json:"finished"`
    34  	State    string `json:"state"`
    35  	Context  string `json:"context"`
    36  	URL      string `json:"url"`
    37  }
    38  
    39  type prowJobs []prowJob
    40  
    41  // getJobs reads job information as JSON from a given URL.
    42  func getJobs(url string) (prowJobs, error) {
    43  	body, err := mungerutil.ReadHTTP(url)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	jobs := prowJobs{}
    49  	err = json.Unmarshal(body, &jobs)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return jobs, nil
    54  }
    55  
    56  func (j prowJobs) filter(pred func(prowJob) bool) prowJobs {
    57  	out := prowJobs{}
    58  	for _, job := range j {
    59  		if pred(job) {
    60  			out = append(out, job)
    61  		}
    62  	}
    63  	return out
    64  }
    65  
    66  func (j prowJobs) repo(repo string) prowJobs {
    67  	return j.filter(func(job prowJob) bool { return job.Repo == repo })
    68  }
    69  
    70  func (j prowJobs) batch() prowJobs {
    71  	return j.filter(func(job prowJob) bool { return job.Type == "batch" })
    72  }
    73  
    74  func (j prowJobs) successful() prowJobs {
    75  	return j.filter(func(job prowJob) bool { return job.State == "success" })
    76  }
    77  
    78  func (j prowJobs) firstUnfinished() *prowJob {
    79  	for _, job := range j {
    80  		if job.State == "triggered" || job.State == "pending" {
    81  			return &job
    82  		}
    83  	}
    84  	return nil
    85  }