Repository: kubesphere/ks-devops
Pull request #499 (2022-03-16 15:53:16)

Replace it with the new Jenkins client
<!-- 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 feature
/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 flake
### What this PR does / why we need it:
Please refer to: #496 

### 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.
-->
https://github.com/kubesphere/ks-devops/blob/14ea1b04b27be835c1d8f399c057fdfb4bb027cc/pkg/client/devops/jclient/jenkins.go#L27-L30
IMO, the structure as a whole should be a `devopsClient.Interface` . this makes it easy to add lines to the API, or to rewrite the old API

### 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: *<a href="https://github.com/kubesphere/ks-devops/pull/499#" title="Author self-approved">mzmuer</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **linuxsuren** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @linuxsuren` 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=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/499?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) Report
> Merging [#499](https://codecov.io/gh/kubesphere/ks-devops/pull/499?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (64e442e) into [master](https://codecov.io/gh/kubesphere/ks-devops/commit/14ea1b04b27be835c1d8f399c057fdfb4bb027cc?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (14ea1b0) will **decrease** coverage by `0.02%`.
> The diff coverage is `n/a`.

```diff
@@            Coverage Diff             @@
##           master     #499      +/-   ##
==========================================
- Coverage   77.25%   77.23%   -0.03%     
==========================================
  Files         134      134              
  Lines       22874    22874              
==========================================
- Hits        17671    17666       -5     
- Misses       5006     5011       +5     
  Partials      197      197              
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `77.23% <ø> (-0.03%)` | :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/499?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) | Coverage Δ | |
|---|---|---|
| [pkg/client/devops/jenkins/devops.go](https://codecov.io/gh/kubesphere/ks-devops/pull/499/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-cGtnL2NsaWVudC9kZXZvcHMvamVua2lucy9kZXZvcHMuZ28=) | `0.00% <ø> (ø)` | |
| [...ns/devopscredential/devopscredential\_controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/499/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvamVua2lucy9kZXZvcHNjcmVkZW50aWFsL2Rldm9wc2NyZWRlbnRpYWxfY29udHJvbGxlci5nbw==) | `33.33% <0.00%> (-3.41%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kubesphere/ks-devops/pull/499?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kubesphere/ks-devops/pull/499?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere). Last update [14ea1b0...64e442e](https://codecov.io/gh/kubesphere/ks-devops/pull/499?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere).


Patch:
diff --git a/cmd/apiserver/app/options/options.go b/cmd/apiserver/app/options/options.go
index 67a58e2..cd12573 100644
--- a/cmd/apiserver/app/options/options.go
+++ b/cmd/apiserver/app/options/options.go
@@ -22,6 +22,7 @@ import (
 	"fmt"
 	v1 "k8s.io/api/core/v1"
 	"kubesphere.io/devops/pkg/client/cache"
+	"kubesphere.io/devops/pkg/client/devops/jclient"
 	"kubesphere.io/devops/pkg/client/sonarqube"
 	"sigs.k8s.io/controller-runtime/pkg/manager"
 
@@ -37,7 +38,6 @@ import (
 	"net/http"
 	"strings"
 
-	"kubesphere.io/devops/pkg/client/devops/jenkins"
 	"kubesphere.io/devops/pkg/client/k8s"
 	"kubesphere.io/devops/pkg/client/s3"
 	fakes3 "kubesphere.io/devops/pkg/client/s3/fake"
@@ -111,7 +111,7 @@ func (s *ServerRunOptions) NewAPIServer(stopCh <-chan struct{}) (*apiserver.APIS
 	}
 
 	if s.JenkinsOptions.Host != "" {
-		devopsClient, err := jenkins.NewDevopsClient(s.JenkinsOptions)
+		devopsClient, err := jclient.NewJenkinsClient(s.JenkinsOptions)
 		if err != nil {
 			return nil, fmt.Errorf("failed to connect to jenkins, please check jenkins status, error: %v", err)
 		}
diff --git a/controllers/jenkins/pipelinerun/utils.go b/controllers/jenkins/pipelinerun/utils.go
index 0908b0b..2ad7524 100644
--- a/controllers/jenkins/pipelinerun/utils.go
+++ b/controllers/jenkins/pipelinerun/utils.go
@@ -151,7 +151,7 @@ type parameterConverter struct {
 }
 
 func (converter parameterConverter) convert() []job.Parameter {
-	var params []job.Parameter
+	params := make([]job.Parameter, 0, len(converter.parameters))
 	for _, param := range converter.parameters {
 		params = append(params, job.Parameter{
 			Name:  param.Name,
diff --git a/pkg/client/devops/jclient/jenkins.go b/pkg/client/devops/jclient/jenkins.go
index adacb32..555cf4a 100644
--- a/pkg/client/devops/jclient/jenkins.go
+++ b/pkg/client/devops/jclient/jenkins.go
@@ -26,7 +26,7 @@ import (
 // JenkinsClient represents a client of Jenkins
 type JenkinsClient struct {
 	Core    core.JenkinsCore
-	jenkins devops.Interface // For refactor purpose only
+	jenkins *jenkins.Jenkins // For refactor purpose only
 }
 
 // ApplyNewSource apply a new source
diff --git a/pkg/client/devops/jenkins/devops.go b/pkg/client/devops/jenkins/devops.go
index 45ce415..064c595 100644
--- a/pkg/client/devops/jenkins/devops.go
+++ b/pkg/client/devops/jenkins/devops.go
@@ -14,11 +14,10 @@ limitations under the License.
 package jenkins
 
 import (
-	"kubesphere.io/devops/pkg/client/devops"
 	"net/http"
 )
 
-func NewDevopsClient(options *Options) (devops.Interface, error) {
+func NewDevopsClient(options *Options) (*Jenkins, error) {
 	// we have to create http client with no redirection
 	client := &http.Client{
 		CheckRedirect: func(req *http.Request, via []*http.Request) error {


Test patch:
diff --git a/controllers/jenkins/pipelinerun/utils_test.go b/controllers/jenkins/pipelinerun/utils_test.go
index 9289ee9..b7dd649 100644
--- a/controllers/jenkins/pipelinerun/utils_test.go
+++ b/controllers/jenkins/pipelinerun/utils_test.go
@@ -344,11 +344,11 @@ func Test_parameterConverter_convert(t *testing.T) {
 		fields fields
 		want   []job.Parameter
 	}{{
-		name: "Nil parameters",
+		name: "Should return empty parameters when 'parameters' is nil",
 		fields: fields{
 			parameters: nil,
 		},
-		want: nil,
+		want: []job.Parameter{},
 	}, {
 		name: "Single parameter",
 		fields: fields{