github.com/jenkins-x/jx/v2@v2.1.155/pkg/prow/config/tide.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/jenkins-x/jx-logging/pkg/log"
     8  	"github.com/jenkins-x/jx/v2/pkg/util"
     9  	"github.com/jenkins-x/lighthouse/pkg/config/keeper"
    10  )
    11  
    12  // CreateTide creates a default Tide Config object
    13  func CreateTide(tideURL string) keeper.Config {
    14  	t := keeper.Config{
    15  		TargetURL: tideURL,
    16  	}
    17  
    18  	var qs []keeper.Query
    19  	qs = append(qs, createApplicationTideQuery())
    20  	qs = append(qs, createEnvironmentTideQuery())
    21  	t.Queries = qs
    22  
    23  	myTrue := true
    24  	myFalse := false
    25  
    26  	t.SyncPeriod = time.Duration(30)
    27  	t.StatusUpdatePeriod = time.Duration(30)
    28  	t.ContextOptions = keeper.ContextPolicyOptions{
    29  		ContextPolicy: keeper.ContextPolicy{
    30  			FromBranchProtection: &myTrue,
    31  			SkipUnknownContexts:  &myFalse,
    32  		},
    33  	}
    34  
    35  	return t
    36  }
    37  
    38  // AddRepoToTideConfig adds a code repository to the Tide section of the Prow Config
    39  func AddRepoToTideConfig(t *keeper.Config, repo string, kind Kind) error {
    40  	switch kind {
    41  	case Application:
    42  		found := false
    43  		for index, q := range t.Queries {
    44  			if util.Contains(q.Labels, "approved") {
    45  				found = true
    46  				repos := t.Queries[index].Repos
    47  				if !util.Contains(repos, repo) {
    48  					repos = append(repos, repo)
    49  					t.Queries[index].Repos = repos
    50  				}
    51  			}
    52  		}
    53  
    54  		if !found {
    55  			log.Logger().Infof("Failed to find 'application' tide config, adding...")
    56  			t.Queries = append(t.Queries, createApplicationTideQuery())
    57  		}
    58  	case Environment, RemoteEnvironment:
    59  		found := false
    60  		for index, q := range t.Queries {
    61  			if !util.Contains(q.Labels, "approved") {
    62  				found = true
    63  				repos := t.Queries[index].Repos
    64  				if !util.Contains(repos, repo) {
    65  					repos = append(repos, repo)
    66  					t.Queries[index].Repos = repos
    67  				}
    68  			}
    69  		}
    70  
    71  		if !found {
    72  			log.Logger().Infof("Failed to find 'environment' tide config, adding...")
    73  			t.Queries = append(t.Queries, createEnvironmentTideQuery())
    74  		}
    75  	case Protection:
    76  		// No Tide config needed for Protection
    77  	default:
    78  		return fmt.Errorf("unknown Prow config kind %s", kind)
    79  	}
    80  	return nil
    81  }
    82  
    83  // RemoveRepoFromTideConfig adds a code repository to the Tide section of the Prow Config
    84  func RemoveRepoFromTideConfig(t *keeper.Config, repo string, kind Kind) error {
    85  	switch kind {
    86  	case Application:
    87  		found := false
    88  		for index, q := range t.Queries {
    89  			if util.Contains(q.Labels, "approved") {
    90  				found = true
    91  				t.Queries[index].Repos = util.RemoveStringFromSlice(t.Queries[index].Repos, repo)
    92  			}
    93  		}
    94  
    95  		if !found {
    96  			log.Logger().Infof("Failed to find 'application' tide config, adding...")
    97  		}
    98  	case Environment, RemoteEnvironment:
    99  		found := false
   100  		for index, q := range t.Queries {
   101  			if !util.Contains(q.Labels, "approved") {
   102  				found = true
   103  				t.Queries[index].Repos = util.RemoveStringFromSlice(t.Queries[index].Repos, repo)
   104  			}
   105  		}
   106  
   107  		if !found {
   108  			log.Logger().Infof("Failed to find 'environment' tide config, adding...")
   109  		}
   110  	case Protection:
   111  		// No Tide config needed for Protection
   112  	default:
   113  		return fmt.Errorf("unknown Prow config kind %s", kind)
   114  	}
   115  	return nil
   116  }
   117  
   118  func createApplicationTideQuery() keeper.Query {
   119  	return keeper.Query{
   120  		Repos:         []string{"jenkins-x/dummy"},
   121  		Labels:        []string{"approved"},
   122  		MissingLabels: []string{"do-not-merge", "do-not-merge/hold", "do-not-merge/work-in-progress", "needs-ok-to-test", "needs-rebase"},
   123  	}
   124  }
   125  
   126  func createEnvironmentTideQuery() keeper.Query {
   127  	return keeper.Query{
   128  		Repos:         []string{"jenkins-x/dummy-environment"},
   129  		Labels:        []string{},
   130  		MissingLabels: []string{"do-not-merge", "do-not-merge/hold", "do-not-merge/work-in-progress", "needs-ok-to-test", "needs-rebase"},
   131  	}
   132  }