github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/updateconfig/updateconfig.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 updateconfig
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	"k8s.io/test-infra/prow/github"
    25  	"k8s.io/test-infra/prow/kube"
    26  	"k8s.io/test-infra/prow/plugins"
    27  )
    28  
    29  const (
    30  	pluginName = "config-updater"
    31  )
    32  
    33  func init() {
    34  	plugins.RegisterPullRequestHandler(pluginName, handlePullRequest)
    35  }
    36  
    37  type githubClient interface {
    38  	CreateComment(owner, repo string, number int, comment string) error
    39  	GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error)
    40  	GetFile(org, repo, filepath, commit string) ([]byte, error)
    41  }
    42  
    43  type kubeClient interface {
    44  	ReplaceConfigMap(name string, config kube.ConfigMap) (kube.ConfigMap, error)
    45  }
    46  
    47  func handlePullRequest(pc plugins.PluginClient, pre github.PullRequestEvent) error {
    48  	configFile := pc.PluginConfig.ConfigUpdater.ConfigFile
    49  	pluginFile := pc.PluginConfig.ConfigUpdater.PluginFile
    50  	return handle(pc.GitHubClient, pc.KubeClient, pc.Logger, pre, configFile, pluginFile)
    51  }
    52  
    53  func handleConfig(gc githubClient, kc kubeClient, org, repo, commit, configFile string) error {
    54  	content, err := gc.GetFile(org, repo, configFile, commit)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	c := kube.ConfigMap{
    60  		Metadata: kube.ObjectMeta{
    61  			Name: "config",
    62  		},
    63  		Data: map[string]string{
    64  			"config": string(content),
    65  		},
    66  	}
    67  
    68  	_, err = kc.ReplaceConfigMap("config", c)
    69  	return err
    70  }
    71  
    72  func handlePlugin(gc githubClient, kc kubeClient, org, repo, commit, pluginFile string) error {
    73  	content, err := gc.GetFile(org, repo, pluginFile, commit)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	c := kube.ConfigMap{
    79  		Metadata: kube.ObjectMeta{
    80  			Name: "plugins",
    81  		},
    82  		Data: map[string]string{
    83  			"plugins": string(content),
    84  		},
    85  	}
    86  
    87  	_, err = kc.ReplaceConfigMap("plugins", c)
    88  	return err
    89  }
    90  
    91  func handle(gc githubClient, kc kubeClient, log *logrus.Entry, pre github.PullRequestEvent, configFile, pluginFile string) error {
    92  	// Only consider newly merged PRs
    93  	if pre.Action != github.PullRequestActionClosed {
    94  		return nil
    95  	}
    96  
    97  	pr := pre.PullRequest
    98  	if !pr.Merged || pr.MergeSHA == nil {
    99  		return nil
   100  	}
   101  
   102  	org := pr.Base.Repo.Owner.Login
   103  	repo := pr.Base.Repo.Name
   104  
   105  	// Process change to prow/config.yaml and prow/plugin.yaml
   106  	changes, err := gc.GetPullRequestChanges(org, repo, pr.Number)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	var msg string
   112  	for _, change := range changes {
   113  		if change.Filename == configFile {
   114  			if err := handleConfig(gc, kc, org, repo, *pr.MergeSHA, configFile); err != nil {
   115  				return err
   116  			}
   117  
   118  			msg += fmt.Sprintf("I updated Prow config for you!")
   119  
   120  		} else if change.Filename == pluginFile {
   121  			if err := handlePlugin(gc, kc, org, repo, *pr.MergeSHA, pluginFile); err != nil {
   122  				return err
   123  			}
   124  
   125  			if msg != "" {
   126  				msg += "\n--------------------------\n"
   127  			}
   128  
   129  			msg += fmt.Sprintf("I updated Prow plugins config for you!")
   130  		}
   131  	}
   132  
   133  	if msg != "" {
   134  		if err := gc.CreateComment(org, repo, pr.Number, plugins.FormatResponseRaw(pr.Body, pr.HTMLURL, pr.User.Login, msg)); err != nil {
   135  			return fmt.Errorf("comment err: %v", err)
   136  		}
   137  	}
   138  
   139  	return nil
   140  }