Repository: jenkins-x/lighthouse (go)

Issue:
Lighthouse jobs label dont have nested group
Lighthouse jobs created from nested gitlab repos dont have groups included in them
This breaks the jx gitops variable step in jenkinsx pipelne.

Example:
Create a repo in gitlab `import-test-1` under `jx-gitlab-test` org and `cluster` sub-repo

Expected labels:
```
lighthouse.jenkins-x.io/refs.org: jx-gitlab-test
lighthouse.jenkins-x.io/refs.repo: cluster/import-test-1
```

Actual labels:
```
lighthouse.jenkins-x.io/refs.org: jx-gitlab-test
lighthouse.jenkins-x.io/refs.repo: import-test-1
```



Pull request description:
fix: keep group in lj label for nested gitlab repos
Signed-off-by: ankitm123 <ankitmohapatra123@gmail.com>

fixes #1408 

Patch:
diff --git a/pkg/jobutil/jobutil.go b/pkg/jobutil/jobutil.go
index d64e82c3..466d1fbc 100644
--- a/pkg/jobutil/jobutil.go
+++ b/pkg/jobutil/jobutil.go
@@ -19,6 +19,7 @@ package jobutil
 
 import (
 	"fmt"
+	"os"
 	"path/filepath"
 	"strconv"
 	"strings"
@@ -193,7 +194,7 @@ func completePrimaryRefs(refs v1alpha1.Refs, jb job.Base) *v1alpha1.Refs {
 	}
 	refs.SkipSubmodules = jb.SkipSubmodules
 	// TODO
-	//refs.CloneDepth = jb.CloneDepth
+	// refs.CloneDepth = jb.CloneDepth
 	return &refs
 }
 
@@ -306,6 +307,11 @@ func LabelsAndAnnotationsForSpec(spec v1alpha1.LighthouseJobSpec, extraLabels, e
 	// let's validate labels
 	for key, value := range labels {
 		if errs := validation.IsValidLabelValue(value); len(errs) > 0 {
+			// ToDo: Use util.GitKind function instead
+			// For nested repos only in gitlab, we do not want to remove the sub group name, which comes before /
+			if key == util.RepoLabel && os.Getenv("GIT_KIND") == "gitlab" {
+				value = strings.Replace(value, "/", "-", -1)
+			}
 			// try to use basename of a path, if path contains invalid //
 			base := filepath.Base(value)
 			if errs := validation.IsValidLabelValue(base); len(errs) == 0 {
@@ -330,7 +336,6 @@ func LabelsAndAnnotationsForSpec(spec v1alpha1.LighthouseJobSpec, extraLabels, e
 	for k, v := range extraAnnotations {
 		annotations[k] = v
 	}
-
 	return labels, annotations
 }
 
diff --git a/pkg/plugins/trigger/trigger.go b/pkg/plugins/trigger/trigger.go
index 6c22ec78..8221ee76 100644
--- a/pkg/plugins/trigger/trigger.go
+++ b/pkg/plugins/trigger/trigger.go
@@ -41,41 +41,39 @@ const (
 	customerTriggerCommandEnvVar = "LH_CUSTOM_TRIGGER_COMMAND"
 )
 
-var (
-	plugin = plugins.Plugin{
-		Description: `The trigger plugin starts tests in reaction to commands and pull request events. It is responsible for ensuring that test jobs are only run on trusted PRs. A PR is considered trusted if the author is a member of the 'trusted organization' for the repository or if such a member has left an '/ok-to-test' command on the PR.
+var plugin = plugins.Plugin{
+	Description: `The trigger plugin starts tests in reaction to commands and pull request events. It is responsible for ensuring that test jobs are only run on trusted PRs. A PR is considered trusted if the author is a member of the 'trusted organization' for the repository or if such a member has left an '/ok-to-test' command on the PR.
 <br>Trigger starts jobs automatically when a new trusted PR is created or when an untrusted PR becomes trusted, but it can also be used to start jobs manually via the '/test' command.
 <br>The '/retest' command can be used to rerun jobs that have reported failure.`,
-		ConfigHelpProvider: configHelp,
-		PullRequestHandler: handlePullRequest,
-		PushEventHandler:   handlePush,
-		Commands: []plugins.Command{{
-			Name:        "ok-to-test",
-			Description: "Marks a PR as 'trusted' and starts tests.",
-			WhoCanUse:   "Members of the trusted organization for the repo.",
-			Action: plugins.
-				Invoke(handleGenericCommentEvent).
-				When(plugins.Action(scm.ActionCreate), plugins.IsPR(), plugins.IssueState("open")),
-		}, {
-			Name: "test",
-			Arg: &plugins.CommandArg{
-				Pattern: `[-\w]+(?:,[-\w]+)*`,
-			},
-			Description: "Manually starts a/all test job(s).",
-			Featured:    true,
-			Action: plugins.
-				Invoke(handleGenericCommentEvent).
-				When(plugins.Action(scm.ActionCreate), plugins.IsPR(), plugins.IssueState("open")),
-		}, {
-			Name:        "retest",
-			Description: "Rerun test jobs that have failed.",
-			Featured:    true,
-			Action: plugins.
-				Invoke(handleGenericCommentEvent).
-				When(plugins.Action(scm.ActionCreate), plugins.IsPR(), plugins.IssueState("open")),
-		}},
-	}
-)
+	ConfigHelpProvider: configHelp,
+	PullRequestHandler: handlePullRequest,
+	PushEventHandler:   handlePush,
+	Commands: []plugins.Command{{
+		Name:        "ok-to-test",
+		Description: "Marks a PR as 'trusted' and starts tests.",
+		WhoCanUse:   "Members of the trusted organization for the repo.",
+		Action: plugins.
+			Invoke(handleGenericCommentEvent).
+			When(plugins.Action(scm.ActionCreate), plugins.IsPR(), plugins.IssueState("open")),
+	}, {
+		Name: "test",
+		Arg: &plugins.CommandArg{
+			Pattern: `[-\w]+(?:,[-\w]+)*`,
+		},
+		Description: "Manually starts a/all test job(s).",
+		Featured:    true,
+		Action: plugins.
+			Invoke(handleGenericCommentEvent).
+			When(plugins.Action(scm.ActionCreate), plugins.IsPR(), plugins.IssueState("open")),
+	}, {
+		Name:        "retest",
+		Description: "Rerun test jobs that have failed.",
+		Featured:    true,
+		Action: plugins.
+			Invoke(handleGenericCommentEvent).
+			When(plugins.Action(scm.ActionCreate), plugins.IsPR(), plugins.IssueState("open")),
+	}},
+}
 
 func init() {
 	customTriggerCommand := os.Getenv(customerTriggerCommandEnvVar)


Test patch:
diff --git a/pkg/jobutil/jobutil_test.go b/pkg/jobutil/jobutil_test.go
index 6626c2ea..4201043c 100644
--- a/pkg/jobutil/jobutil_test.go
+++ b/pkg/jobutil/jobutil_test.go
@@ -17,6 +17,7 @@ limitations under the License.
 package jobutil
 
 import (
+	"os"
 	"reflect"
 	"testing"
 
@@ -285,6 +286,7 @@ func TestBatchSpec(t *testing.T) {
 func TestNewLighthouseJob(t *testing.T) {
 	var testCases = []struct {
 		name                string
+		gitKind             string
 		spec                v1alpha1.LighthouseJobSpec
 		labels              map[string]string
 		expectedLabels      map[string]string
@@ -293,6 +295,7 @@ func TestNewLighthouseJob(t *testing.T) {
 	}{
 		{
 			name: "periodic job, no extra labels",
+			gitKind: "github",
 			spec: v1alpha1.LighthouseJobSpec{
 				Job:  "job",
 				Type: job.PeriodicJob,
@@ -309,6 +312,7 @@ func TestNewLighthouseJob(t *testing.T) {
 		},
 		{
 			name: "periodic job, extra labels",
+			gitKind: "github",
 			spec: v1alpha1.LighthouseJobSpec{
 				Job:  "job",
 				Type: job.PeriodicJob,
@@ -328,6 +332,7 @@ func TestNewLighthouseJob(t *testing.T) {
 		},
 		{
 			name: "presubmit job",
+			gitKind: "github",
 			spec: v1alpha1.LighthouseJobSpec{
 				Job:  "job",
 				Type: job.PresubmitJob,
@@ -361,8 +366,47 @@ func TestNewLighthouseJob(t *testing.T) {
 				util.LighthouseJobAnnotation: "job",
 			},
 		},
+		{
+			name: "presubmit job with nested repos",
+			gitKind: "gitlab",
+			spec: v1alpha1.LighthouseJobSpec{
+				Job:  "job",
+				Type: job.PresubmitJob,
+				Refs: &v1alpha1.Refs{
+					Org:     "org",
+					Repo:    "group/repo",
+					BaseSHA: "abcd1234",
+					Pulls: []v1alpha1.Pull{
+						{
+							Number: 1,
+							SHA:    "1234abcd",
+						},
+					},
+					CloneURI: "https://gitlab.jx.com/org/group/repo.git",
+				},
+				Context: "pr-build",
+			},
+			labels: map[string]string{},
+			expectedLabels: map[string]string{
+				job.CreatedByLighthouseLabel: "true",
+				util.LighthouseJobAnnotation: "job",
+				job.LighthouseJobTypeLabel:   "presubmit",
+				util.OrgLabel:                "org",
+				util.RepoLabel:               "group-repo",
+				util.PullLabel:               "1",
+				util.BranchLabel:             "PR-1",
+				util.ContextLabel:            "pr-build",
+				util.BaseSHALabel:            "abcd1234",
+				util.LastCommitSHALabel:      "1234abcd",
+			},
+			expectedAnnotations: map[string]string{
+				util.CloneURIAnnotation: "https://gitlab.jx.com/org/group/repo.git",
+				util.LighthouseJobAnnotation: "job",
+			},
+		},
 		{
 			name: "non-github presubmit job",
+			gitKind: "gerrit",
 			spec: v1alpha1.LighthouseJobSpec{
 				Job:  "job",
 				Type: job.PresubmitJob,
@@ -395,6 +439,7 @@ func TestNewLighthouseJob(t *testing.T) {
 			},
 		}, {
 			name: "job with name too long to fit in a label",
+			gitKind: "github",
 			spec: v1alpha1.LighthouseJobSpec{
 				Job:  "job-created-by-someone-who-loves-very-very-very-long-names-so-long-that-it-does-not-fit-into-the-Kubernetes-label-so-it-needs-to-be-truncated-to-63-characters",
 				Type: job.PresubmitJob,
@@ -430,6 +475,7 @@ func TestNewLighthouseJob(t *testing.T) {
 		},
 		{
 			name: "periodic job, extra labels, extra annotations",
+			gitKind: "github",
 			spec: v1alpha1.LighthouseJobSpec{
 				Job:  "job",
 				Type: job.PeriodicJob,
@@ -454,6 +500,7 @@ func TestNewLighthouseJob(t *testing.T) {
 	}
 	for _, testCase := range testCases {
 		t.Run(testCase.name, func(t *testing.T) {
+			os.Setenv("GIT_KIND", testCase.gitKind)
 			pj := NewLighthouseJob(testCase.spec, testCase.labels, testCase.annotations)
 			if actual, expected := pj.Spec, testCase.spec; !equality.Semantic.DeepEqual(actual, expected) {
 				t.Errorf("%s: incorrect PipelineOptionsSpec created: %s", testCase.name, diff.ObjectReflectDiff(actual, expected))