github.com/abayer/test-infra@v0.0.5/mungegithub/example-one-off/main.go (about)

     1  /*
     2  Copyright 2016 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 main
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  
    23  	"github.com/golang/glog"
    24  	"github.com/spf13/cobra"
    25  
    26  	utilflag "k8s.io/apiserver/pkg/util/flag"
    27  	"k8s.io/test-infra/mungegithub/github"
    28  	"k8s.io/test-infra/mungegithub/options"
    29  )
    30  
    31  // MungeIssue is the real worker. It is called for every open github Issue
    32  // But note that all PRs are Issues. (Not all Issues are PRs.) This particular
    33  // function ignores all issues that are not PRs and prints the number for the
    34  // PRs.
    35  func MungeIssue(obj *github.MungeObject) error {
    36  	if !obj.IsPR() {
    37  		return nil
    38  	}
    39  	glog.Infof("PR: %d", *obj.Issue.Number)
    40  	return nil
    41  }
    42  
    43  func main() {
    44  	config := &github.Config{}
    45  	root := &cobra.Command{
    46  		Use:   filepath.Base(os.Args[0]),
    47  		Short: "A program to convert blunderbuss.yaml",
    48  		RunE: func(_ *cobra.Command, _ []string) error {
    49  			if err := config.PreExecute(); err != nil {
    50  				return err
    51  			}
    52  			if err := config.ForEachIssueDo(MungeIssue); err != nil {
    53  				glog.Errorf("Error munging PRs: %v", err)
    54  			}
    55  			return nil
    56  		},
    57  	}
    58  	root.SetGlobalNormalizationFunc(utilflag.WordSepNormalizeFunc)
    59  	config.RegisterOptions(options.New()) // Always uses defaults since Load is never called.
    60  	root.Execute()
    61  }