Repository: jenkins-x/lighthouse
Pull request #1194 (2021-01-04 07:22:30)

fix: convert old incorrect configuration
lots of in-repo triggers have incorrectly used `/test` and `/retest` and `/lint` and `/relint` for `trigger` and `rerun_command` due to the bug where we didn't used to properly validate JobConfig for in repo.

to make it easy to transition to the latest lighthouse lets add a little migrator function to switch those bad configurations to their correct values so that lighthouse still works if its upgraded for existing triggers in Jenkins X

jenkins-x-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **jstrachan**
You can assign the PR to them by writing `/assign @jstrachan` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=jenkins-x%2Flighthouse).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/jenkins-x/lighthouse/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["jstrachan"]} -->
rawlingsj: great - this avoids breaking existing users inrepo config when upgrading to latest lighthouse
/lgtm
jenkins-x-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *[rawlingsj](https://github.com/jenkins-x/lighthouse/pull/1194#issuecomment-753913402 "LGTM")*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=jenkins-x%2Flighthouse).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com#issuecomment-753913402/jenkins-x/lighthouse/blob/master/OWNERS)~~ [rawlingsj]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/triggerconfig/merge/merge.go b/pkg/triggerconfig/merge/merge.go
index 6562d33e..d8b226de 100644
--- a/pkg/triggerconfig/merge/merge.go
+++ b/pkg/triggerconfig/merge/merge.go
@@ -80,6 +80,7 @@ func ConfigMerge(cfg *config.Config, pluginsCfg *plugins.Configuration, repoConf
 	if err != nil {
 		return errors.Wrapf(err, "failed to validate plugins")
 	}
+	migrateOldConfig(&cfg.JobConfig)
 	err = cfg.Init(cfg.ProwConfig)
 	if err != nil {
 		return errors.Wrapf(err, "failed to initialize config")
@@ -90,3 +91,19 @@ func ConfigMerge(cfg *config.Config, pluginsCfg *plugins.Configuration, repoConf
 	}
 	return nil
 }
+
+// migrateOldConfig lets handle some old incorrect configuration where the trigger and rerun_command values were not setup properly
+func migrateOldConfig(cfg *job.Config) {
+	for _, ps := range cfg.Presubmits {
+		for i := range ps {
+			presubmit := &ps[i]
+			if presubmit.Trigger == "/test" && presubmit.RerunCommand == "/retest" {
+				presubmit.Trigger = "(?m)^/test,?($|\\s.*)"
+				presubmit.RerunCommand = "/test"
+			} else if presubmit.Trigger == "/lint" && presubmit.RerunCommand == "/relint" {
+				presubmit.Trigger = "(?m)^/lint,?($|\\s.*)"
+				presubmit.RerunCommand = "/lint"
+			}
+		}
+	}
+}


Test patch:
diff --git a/pkg/triggerconfig/inrepo/load_integration_test.go b/pkg/triggerconfig/inrepo/load_integration_test.go
index 7678df83..06faaeb0 100644
--- a/pkg/triggerconfig/inrepo/load_integration_test.go
+++ b/pkg/triggerconfig/inrepo/load_integration_test.go
@@ -49,7 +49,7 @@ func LogConfig(t *testing.T, cfg *config.Config) {
 		t.Logf("presubmits for repository %s\n", k)
 
 		for _, r := range v {
-			t.Logf("  presubmit %s\n", r.Name)
+			t.Logf("  presubmit %s trigger: %s rerun_command: %s\n", r.Name, r.Trigger, r.RerunCommand)
 		}
 	}
 	for k, v := range cfg.Postsubmits {
diff --git a/pkg/triggerconfig/inrepo/test_data/myorg/loadtest/.lighthouse/jenkins-x/triggers.yaml b/pkg/triggerconfig/inrepo/test_data/myorg/loadtest/.lighthouse/jenkins-x/triggers.yaml
index 4a23d364..ee859264 100644
--- a/pkg/triggerconfig/inrepo/test_data/myorg/loadtest/.lighthouse/jenkins-x/triggers.yaml
+++ b/pkg/triggerconfig/inrepo/test_data/myorg/loadtest/.lighthouse/jenkins-x/triggers.yaml
@@ -6,7 +6,7 @@ spec:
     context: "test"
     always_run: true
     optional: false
-    trigger: "(?:/test|/retest)"
+    trigger: "/test"
     rerun_command: "/retest"
     agent: tekton-pipeline
   postsubmits:
diff --git a/pkg/triggerconfig/inrepo/test_data/myorg/loadtest/.lighthouse/linter/triggers.yaml b/pkg/triggerconfig/inrepo/test_data/myorg/loadtest/.lighthouse/linter/triggers.yaml
index 1cdb3e32..f0989dcc 100644
--- a/pkg/triggerconfig/inrepo/test_data/myorg/loadtest/.lighthouse/linter/triggers.yaml
+++ b/pkg/triggerconfig/inrepo/test_data/myorg/loadtest/.lighthouse/linter/triggers.yaml
@@ -6,7 +6,7 @@ spec:
     context: "lint"
     always_run: true
     optional: false
-    trigger: "(?:/lint|/relint)"
+    trigger: "/lint"
     rerun_command: "/relint"
     agent: tekton-pipeline