sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/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 "context" 21 22 prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1" 23 "sigs.k8s.io/prow/pkg/config" 24 "sigs.k8s.io/prow/pkg/github" 25 "sigs.k8s.io/prow/pkg/pjutil" 26 ) 27 28 func listPushEventChanges(pe github.PushEvent) config.ChangedFilesProvider { 29 return func() ([]string, error) { 30 changed := make(map[string]bool) 31 for _, commit := range pe.Commits { 32 for _, added := range commit.Added { 33 changed[added] = true 34 } 35 for _, removed := range commit.Removed { 36 changed[removed] = true 37 } 38 for _, modified := range commit.Modified { 39 changed[modified] = true 40 } 41 } 42 var changedFiles []string 43 for file := range changed { 44 changedFiles = append(changedFiles, file) 45 } 46 return changedFiles, nil 47 } 48 } 49 50 func createRefs(pe github.PushEvent) prowapi.Refs { 51 return prowapi.Refs{ 52 Org: pe.Repo.Owner.Name, 53 Repo: pe.Repo.Name, 54 RepoLink: pe.Repo.HTMLURL, 55 BaseRef: pe.Branch(), 56 BaseSHA: pe.After, 57 BaseLink: pe.Compare, 58 } 59 } 60 61 func handlePE(c Client, pe github.PushEvent) error { 62 if pe.Deleted || pe.After == "0000000000000000000000000000000000000000" { 63 // we should not trigger jobs for a branch deletion 64 return nil 65 } 66 67 org := pe.Repo.Owner.Login 68 repo := pe.Repo.Name 69 70 shaGetter := func() (string, error) { 71 return pe.After, nil 72 } 73 74 postsubmits := getPostsubmits(c.Logger, c.GitClient, c.Config, org+"/"+repo, shaGetter) 75 76 for _, j := range postsubmits { 77 if shouldRun, err := j.ShouldRun(pe.Branch(), listPushEventChanges(pe)); err != nil { 78 return err 79 } else if !shouldRun { 80 continue 81 } 82 refs := createRefs(pe) 83 labels := make(map[string]string) 84 for k, v := range j.Labels { 85 labels[k] = v 86 } 87 labels[github.EventGUID] = pe.GUID 88 pj := pjutil.NewProwJob(pjutil.PostsubmitSpec(j, refs), labels, j.Annotations, pjutil.RequireScheduling(c.Config.Scheduler.Enabled)) 89 c.Logger.WithFields(pjutil.ProwJobFields(&pj)).Info("Creating a new prowjob.") 90 if err := createWithRetry(context.TODO(), c.ProwJobClient, &pj); err != nil { 91 return err 92 } 93 } 94 return nil 95 }