Repository: kubesphere/ks-devops
Pull request #803 (2022-08-29 07:08:52)

Support to set the cluster name
<!-- Thanks for sending a pull request! Here are some tips for you:

1. If you want **faster** PR reviews, read how: https://github.com/kubesphere/community/blob/master/developer-guide/development/the-pr-author-guide-to-getting-through-code-review.md
2. In case you want to know how your PR got reviewed, read: https://github.com/kubesphere/community/blob/master/developer-guide/development/code-review-guide.md
3. Here are some coding conventions followed by the KubeSphere community: https://github.com/kubesphere/community/blob/master/developer-guide/development/coding-conventions.md
4. Additional open-source best practice: https://github.com/LinuxSuRen/open-source-best-practice
-->

### What type of PR is this?
<!-- 
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind design
/kind chore

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->
/kind feature

### What this PR does / why we need it:

### Which issue(s) this PR fixes:
<!--
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
Please leave it or change # to be None if there is no corresponding issue that exists
-->
Fixes #

### Special notes for reviewers:
<!--
You can use the following command to let the DevOps SIG members help you to review your PR.
/cc @kubesphere/sig-devops 
And please avoid cc any individual.
-->
Please check the following list before waiting reviewers:

- [ ] Already committed the CRD files to [the Helm Chart](https://github.com/kubesphere-sigs/ks-devops-helm-chart/) if you created some new CRDs
- [ ] Already [added the permission](https://github.com/kubesphere/ks-installer/blob/9e063b085a0e43fdb3d0d9e3e7f4149146f14b9c/roles/ks-core/prepare/files/ks-init/role-templates.yaml) for the new API
- [ ] Already added the RBAC markers for the new controllers

### Does this PR introduce a user-facing change??
<!--
If no, just write "None" in the release-note block below.
If yes, a release note is required:
Enter your extended-release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".

Please keep the note be same as your PR title if you believe it should be in the release notes.
-->
```release-note
None
```


ks-ci-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 ask for approval from **linuxsuren** after the PR has been reviewed.

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

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

- **[OWNERS](https://github.com/kubesphere/ks-devops/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":["linuxsuren"]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/kubesphere/ks-devops/pull/803?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) Report
> Merging [#803](https://codecov.io/gh/kubesphere/ks-devops/pull/803?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (6fbee7a) into [master](https://codecov.io/gh/kubesphere/ks-devops/commit/f69d2264e742b7b55ae502443396c1281cbf0c81?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (f69d226) will **decrease** coverage by `0.03%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master     #803      +/-   ##
==========================================
- Coverage   55.10%   55.06%   -0.04%     
==========================================
  Files         183      183              
  Lines       10891    10893       +2     
==========================================
- Hits         6001     5998       -3     
- Misses       4562     4566       +4     
- Partials      328      329       +1     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `55.06% <100.00%> (-0.04%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/kubesphere/ks-devops/pull/803?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) | Coverage Δ | |
|---|---|---|
| [cmd/controller/app/options/feature.go](https://codecov.io/gh/kubesphere/ks-devops/pull/803/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y21kL2NvbnRyb2xsZXIvYXBwL29wdGlvbnMvZmVhdHVyZS5nbw==) | `100.00% <100.00%> (ø)` | |
| [...rs/gitrepository/pull\_request\_status\_controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/803/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvZ2l0cmVwb3NpdG9yeS9wdWxsX3JlcXVlc3Rfc3RhdHVzX2NvbnRyb2xsZXIuZ28=) | `86.20% <100.00%> (+0.09%)` | :arrow_up: |
| [...ontrollers/jenkins/pipeline/pipeline\_controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/803/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvamVua2lucy9waXBlbGluZS9waXBlbGluZV9jb250cm9sbGVyLmdv) | `39.39% <0.00%> (-3.79%)` | :arrow_down: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere)


Patch:
diff --git a/.github/Jenkinsfile b/.github/Jenkinsfile
new file mode 100644
index 0000000..1f088bb
--- /dev/null
+++ b/.github/Jenkinsfile
@@ -0,0 +1,72 @@
+pipeline {
+  agent {
+    node {
+      label 'go-builder'
+    }
+  }
+
+  stages {
+    stage('Code check') {
+      parallel {
+        stage('Lint') {
+          agent none
+          steps {
+            container('golangci') {
+              sh 'golangci-lint run ./... -v'
+            }
+          }
+        }
+
+        stage('Test') {
+          agent none
+          steps {
+            container('go') {
+              sh 'make test'
+            }
+          }
+        }
+
+        stage('License') {
+          agent none
+          steps {
+            container('skywalking-eyes') {
+              sh 'license-eye header check'
+            }
+          }
+        }
+      }
+    }
+
+    stage('Build Image') {
+      parallel {
+        stage('Build') {
+          agent none
+          steps {
+            container('base') {
+              sh 'docker build . -f config/dockerfiles/apiserver/Dockerfile -t surenpi/test:latest'
+            }
+          }
+        }
+
+        stage('Build Controller') {
+          agent none
+          steps {
+            container('base') {
+              sh 'docker build . -f config/dockerfiles/controller-manager/Dockerfile -t surenpi/test:latest'
+            }
+          }
+        }
+
+        stage('Build Tool') {
+          agent none
+          steps {
+            container('base') {
+              sh 'docker build . -f config/dockerfiles/tools/Dockerfile -t surenpi/test:latest'
+            }
+          }
+        }
+
+      }
+    }
+  }
+}
diff --git a/cmd/controller/app/controllers.go b/cmd/controller/app/controllers.go
index 908a12d..05a9abe 100644
--- a/cmd/controller/app/controllers.go
+++ b/cmd/controller/app/controllers.go
@@ -144,6 +144,7 @@ func getAllControllers(mgr manager.Manager, client k8s.Client, informerFactory i
 			err := (&gitrepository.PullRequestStatusReconciler{
 				Client:          mgr.GetClient(),
 				ExternalAddress: s.FeatureOptions.ExternalAddress,
+				ClusterName:     s.FeatureOptions.ClusterName,
 			}).SetupWithManager(mgr)
 			if err != nil {
 				return err
diff --git a/cmd/controller/app/options/feature.go b/cmd/controller/app/options/feature.go
index 06319e5..339b30b 100644
--- a/cmd/controller/app/options/feature.go
+++ b/cmd/controller/app/options/feature.go
@@ -29,6 +29,7 @@ type FeatureOptions struct {
 	Controllers     map[string]bool
 	SystemNamespace string
 	ExternalAddress string
+	ClusterName     string
 }
 
 // GetControllers returns the controllers map
@@ -79,6 +80,7 @@ func (o *FeatureOptions) AddFlags(fs *pflag.FlagSet, c *FeatureOptions) {
 	fs.StringVarP(&o.SystemNamespace, "system-namespace", "", "kubesphere-devops-system",
 		"The system namespace that contains ConfigMap, Secrets e.g.")
 	fs.StringVarP(&o.ExternalAddress, "external-address", "", "", "The external address for the UI")
+	fs.StringVarP(&o.ClusterName, "cluster-name", "", "default", "Current cluster name")
 }
 
 func (o *FeatureOptions) knownControllers() []string {
diff --git a/controllers/gitrepository/pull_request_status_controller.go b/controllers/gitrepository/pull_request_status_controller.go
index 8dca6df..5d8b96b 100644
--- a/controllers/gitrepository/pull_request_status_controller.go
+++ b/controllers/gitrepository/pull_request_status_controller.go
@@ -35,6 +35,7 @@ import (
 type PullRequestStatusReconciler struct {
 	client.Client
 	ExternalAddress string
+	ClusterName     string
 
 	log      logr.Logger
 	recorder record.EventRecorder
@@ -150,8 +151,9 @@ func getRepoInfo(repo *v1alpha3.MultiBranchPipeline) (info repoInfo) {
 func (r *PullRequestStatusReconciler) getExternalPipelineRunAddress(ctx context.Context, pipelineRun *v1alpha3.PipelineRun) (target string, err error) {
 	var ws string
 	if ws, err = r.getWorkspace(ctx, pipelineRun.GetNamespace()); err == nil {
-		target = fmt.Sprintf("%s/%s/clusters/default/devops/%s/pipelines/%s/run/%s/task-status",
-			net.ParseURL(r.ExternalAddress), ws, pipelineRun.Namespace, pipelineRun.Spec.PipelineRef.Name, pipelineRun.Name)
+		target = fmt.Sprintf("%s/%s/clusters/%s/devops/%s/pipelines/%s/branch/%s/run/%s/task-status",
+			net.ParseURL(r.ExternalAddress), ws, r.ClusterName,
+			pipelineRun.Namespace, pipelineRun.Spec.PipelineRef.Name, pipelineRun.Spec.SCM.RefName, pipelineRun.Name)
 	}
 	return
 }


Test patch:
diff --git a/cmd/controller/app/options/feature_test.go b/cmd/controller/app/options/feature_test.go
index 042bc07..153a1ba 100644
--- a/cmd/controller/app/options/feature_test.go
+++ b/cmd/controller/app/options/feature_test.go
@@ -119,4 +119,5 @@ func TestFeatureOptions(t *testing.T) {
 	assert.NotNil(t, flagSet.Lookup("enabled-controllers"))
 	assert.NotNil(t, flagSet.Lookup("system-namespace"))
 	assert.NotNil(t, flagSet.Lookup("external-address"))
+	assert.NotNil(t, flagSet.Lookup("cluster-name"))
 }