Repository: kubesphere/ks-devops
Pull request #747 (2022-07-22 05:29:31)

Fix jenkins edit mode
<!-- 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 bug

### What this PR does / why we need it:
This is the missing part of https://github.com/kubesphere/ks-devops/pull/746

### 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:

- **[pkg/models/devops/OWNERS](https://github.com/kubesphere/ks-devops/blob/master/pkg/models/devops/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/747?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) Report
> Merging [#747](https://codecov.io/gh/kubesphere/ks-devops/pull/747?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (37d3eb2) into [master](https://codecov.io/gh/kubesphere/ks-devops/commit/865043b212d8e417b6564e45324e877aabb526b6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (865043b) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master     #747      +/-   ##
==========================================
+ Coverage   78.25%   78.27%   +0.02%     
==========================================
  Files         157      157              
  Lines       23809    23811       +2     
==========================================
+ Hits        18631    18638       +7     
+ Misses       4924     4921       -3     
+ Partials      254      252       -2     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `78.27% <100.00%> (+0.02%)` | :arrow_up: |

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/747?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) | Coverage Δ | |
|---|---|---|
| [pkg/models/devops/devops.go](https://codecov.io/gh/kubesphere/ks-devops/pull/747/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-cGtnL21vZGVscy9kZXZvcHMvZGV2b3BzLmdv) | `24.42% <100.00%> (+0.31%)` | :arrow_up: |
| [...ns/devopscredential/devopscredential\_controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/747/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: |
| [.../jenkins/devopsproject/devopsproject\_controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/747/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvamVua2lucy9kZXZvcHNwcm9qZWN0L2Rldm9wc3Byb2plY3RfY29udHJvbGxlci5nbw==) | `43.22% <0.00%> (+2.60%)` | :arrow_up: |
| [...ontrollers/jenkins/pipeline/pipeline\_controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/747/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvamVua2lucy9waXBlbGluZS9waXBlbGluZV9jb250cm9sbGVyLmdv) | `43.18% <0.00%> (+3.78%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kubesphere/ks-devops/pull/747?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/747?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere). Last update [865043b...37d3eb2](https://codecov.io/gh/kubesphere/ks-devops/pull/747?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/pkg/models/devops/devops.go b/pkg/models/devops/devops.go
index c862c64..81693f6 100644
--- a/pkg/models/devops/devops.go
+++ b/pkg/models/devops/devops.go
@@ -317,15 +317,18 @@ func (d devopsOperator) UpdateJenkinsfile(projectName, pipelineName, mode, jenki
 	if pipeline.Annotations == nil {
 		pipeline.Annotations = map[string]string{}
 	}
+	pipeline.Annotations[devopsv1alpha3.PipelineJenkinsfileEditModeAnnoKey] = mode
+
 	switch mode {
 	case devopsv1alpha3.PipelineJenkinsfileEditModeJSON:
-		pipeline.Annotations[devopsv1alpha3.PipelineJenkinsfileEditModeAnnoKey] = devopsv1alpha3.PipelineJenkinsfileEditModeJSON
 		pipeline.Annotations[devopsv1alpha3.PipelineJenkinsfileValueAnnoKey] = jenkinsfile
-	default:
-		pipeline.Annotations[devopsv1alpha3.PipelineJenkinsfileEditModeAnnoKey] = ""
+	case devopsv1alpha3.PipelineJenkinsfileEditModeRaw:
 		if pipeline.Spec.Pipeline != nil {
 			pipeline.Spec.Pipeline.Jenkinsfile = jenkinsfile
 		}
+	default:
+		err = fmt.Errorf("invalid edit mode: %s", mode)
+		return
 	}
 	_, err = d.ksclient.DevopsV1alpha3().Pipelines(projectName).Update(d.context, pipeline, metav1.UpdateOptions{})
 	return


Test patch:
diff --git a/pkg/models/devops/devops_test.go b/pkg/models/devops/devops_test.go
index 1f4eb60..d4dce68 100644
--- a/pkg/models/devops/devops_test.go
+++ b/pkg/models/devops/devops_test.go
@@ -397,6 +397,27 @@ func Test_devopsOperator_UpdateJenkinsfile(t *testing.T) {
 			mode:         "",
 			jenkinsfile:  "jenkinsfile",
 		},
+		wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
+			assert.NotNil(t, err)
+			return true
+		},
+		verify: func(t *testing.T, ksclient versioned.Interface) {
+			pip, err := ksclient.DevopsV1alpha3().Pipelines("ns").Get(context.Background(), "fake", metav1.GetOptions{})
+			assert.Nil(t, err)
+			assert.Equal(t, "", pip.Spec.Pipeline.Jenkinsfile)
+			assert.Equal(t, "", pip.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey])
+		},
+	}, {
+		name: "mode value is raw",
+		fields: fields{
+			ksclient: fakeclientset.NewSimpleClientset(pipeline.DeepCopy()),
+		},
+		args: args{
+			projectName:  "ns",
+			pipelineName: "fake",
+			mode:         "raw",
+			jenkinsfile:  "jenkinsfile",
+		},
 		wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
 			assert.Nil(t, err)
 			return true
@@ -405,7 +426,7 @@ func Test_devopsOperator_UpdateJenkinsfile(t *testing.T) {
 			pip, err := ksclient.DevopsV1alpha3().Pipelines("ns").Get(context.Background(), "fake", metav1.GetOptions{})
 			assert.Nil(t, err)
 			assert.Equal(t, "jenkinsfile", pip.Spec.Pipeline.Jenkinsfile)
-			assert.Equal(t, "", pip.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey])
+			assert.Equal(t, "raw", pip.Annotations[v1alpha3.PipelineJenkinsfileEditModeAnnoKey])
 		},
 	}}
 	for _, tt := range tests {