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

     1  /*
     2  Copyright 2015 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 reports
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/util/sets"
    23  	"k8s.io/test-infra/mungegithub/github"
    24  	"k8s.io/test-infra/mungegithub/options"
    25  
    26  	"github.com/golang/glog"
    27  )
    28  
    29  // Report is the interface which all reports must implement to register
    30  type Report interface {
    31  	// Take action on a specific github issue:
    32  	Report(config *github.Config) error
    33  	RegisterOptions(opts *options.Options) sets.String
    34  	Name() string
    35  }
    36  
    37  var reportMap = map[string]Report{}
    38  var reports = []Report{}
    39  
    40  // GetAllReports returns a slice of all registered reports. This list is
    41  // completely independent of the reports selected at runtime in --pr-reports.
    42  // This is all possible reports.
    43  func GetAllReports() []Report {
    44  	out := []Report{}
    45  	for _, report := range reportMap {
    46  		out = append(out, report)
    47  	}
    48  	return out
    49  }
    50  
    51  // GetActiveReports returns a slice of all reports which both registered and
    52  // were requested by the user
    53  func GetActiveReports() []Report {
    54  	return reports
    55  }
    56  
    57  // RegisterReport should be called in `init()` by each report to make itself
    58  // available by name
    59  func RegisterReport(report Report) error {
    60  	if _, found := reportMap[report.Name()]; found {
    61  		return fmt.Errorf("a report with that name (%s) already exists", report.Name())
    62  	}
    63  	reportMap[report.Name()] = report
    64  	glog.Infof("Registered %#v at %s", report, report.Name())
    65  	return nil
    66  }
    67  
    68  // RegisterReportOrDie will call RegisterReport but will be fatal on error
    69  func RegisterReportOrDie(report Report) {
    70  	if err := RegisterReport(report); err != nil {
    71  		glog.Fatalf("Failed to register report: %s", err)
    72  	}
    73  }
    74  
    75  // RunReports runs the specified reports.
    76  func RunReports(cfg *github.Config, runReports ...string) error {
    77  	for _, name := range runReports {
    78  		report, ok := reportMap[name]
    79  		if !ok {
    80  			return fmt.Errorf("%v: not a valid report", name)
    81  		}
    82  		if err := report.Report(cfg); err != nil {
    83  			return fmt.Errorf("Error running %v: %v", report.Name(), err)
    84  		}
    85  	}
    86  	return nil
    87  }
    88  
    89  // RegisterOptions registers options used by reports and returns any options that should trigger a
    90  // restart if they are changed.
    91  func RegisterOptions(opts *options.Options) sets.String {
    92  	immutables := sets.NewString()
    93  	for _, report := range reportMap {
    94  		immutables = immutables.Union(report.RegisterOptions(opts))
    95  	}
    96  	return immutables
    97  }