github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/trigger/push.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 trigger
    18  
    19  import (
    20  	"k8s.io/test-infra/prow/github"
    21  	"k8s.io/test-infra/prow/kube"
    22  	"k8s.io/test-infra/prow/pjutil"
    23  )
    24  
    25  func listPushEventChanges(pe github.PushEvent) []string {
    26  	changed := make(map[string]bool)
    27  	for _, commit := range pe.Commits {
    28  		for _, added := range commit.Added {
    29  			changed[added] = true
    30  		}
    31  		for _, removed := range commit.Removed {
    32  			changed[removed] = true
    33  		}
    34  		for _, modified := range commit.Modified {
    35  			changed[modified] = true
    36  		}
    37  	}
    38  	changedFiles := []string{}
    39  	for file := range changed {
    40  		changedFiles = append(changedFiles, file)
    41  	}
    42  	return changedFiles
    43  }
    44  
    45  func handlePE(c Client, pe github.PushEvent) error {
    46  	if pe.Deleted {
    47  		// we should not trigger jobs for a branch deletion
    48  		return nil
    49  	}
    50  	for _, j := range c.Config.Postsubmits[pe.Repo.FullName] {
    51  		if !j.RunsAgainstBranch(pe.Branch()) {
    52  			continue
    53  		}
    54  		changedFiles := listPushEventChanges(pe)
    55  		if !j.RunsAgainstChanges(changedFiles) {
    56  			continue
    57  		}
    58  		kr := kube.Refs{
    59  			Org:     pe.Repo.Owner.Name,
    60  			Repo:    pe.Repo.Name,
    61  			BaseRef: pe.Branch(),
    62  			BaseSHA: pe.After,
    63  		}
    64  		labels := make(map[string]string)
    65  		for k, v := range j.Labels {
    66  			labels[k] = v
    67  		}
    68  		labels[github.EventGUID] = pe.GUID
    69  		pj := pjutil.NewProwJob(pjutil.PostsubmitSpec(j, kr), labels)
    70  		c.Logger.WithFields(pjutil.ProwJobFields(&pj)).Info("Creating a new prowjob.")
    71  		if _, err := c.KubeClient.CreateProwJob(pj); err != nil {
    72  			return err
    73  		}
    74  	}
    75  	return nil
    76  }