Repository: kubesphere/ks-devops
Pull request #242 (2021-09-17 14:30:37)

Filter nil items in result list
### What this PR dose

Filter nil items in result list

### Why we need it

![23647d85ed61ee1c9de57cc52c808f1](https://user-images.githubusercontent.com/16865714/133800115-ef89a654-44d2-4d22-aa96-7bf8becc0457.png)

### Step to test

Docker image for test: `johnniang/devops-apiserver:pipeline-search-bug`

/kind bug

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/242#" title="Author self-approved">JohnNiang</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/242?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) Report
> Merging [#242](https://codecov.io/gh/kubesphere/ks-devops/pull/242?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (1a5082b) into [master](https://codecov.io/gh/kubesphere/ks-devops/commit/e65d92fe8af8769b2c7e362ce2ffce71a0c9b9d5?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) (e65d92f) will **decrease** coverage by `0.01%`.
> The diff coverage is `22.22%`.

[![Impacted file tree graph](https://codecov.io/gh/kubesphere/ks-devops/pull/242/graphs/tree.svg?width=650&height=150&src=pr&token=qSK4RBbyNg&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere)](https://codecov.io/gh/kubesphere/ks-devops/pull/242?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere)

```diff
@@            Coverage Diff            @@
##           master    #242      +/-   ##
=========================================
- Coverage    7.22%   7.20%   -0.02%     
=========================================
  Files          83      83              
  Lines       21112   21112              
=========================================
- Hits         1525    1521       -4     
- Misses      19461   19465       +4     
  Partials      126     126              
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `7.20% <22.22%> (-0.02%)` | :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/242?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere) | Coverage Δ | |
|---|---|---|
| [pkg/kapis/devops/v1alpha2/devops.go](https://codecov.io/gh/kubesphere/ks-devops/pull/242/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-cGtnL2thcGlzL2Rldm9wcy92MWFscGhhMi9kZXZvcHMuZ28=) | `2.17% <0.00%> (+<0.01%)` | :arrow_up: |
| [pkg/models/resources/v1alpha3/interface.go](https://codecov.io/gh/kubesphere/ks-devops/pull/242/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-cGtnL21vZGVscy9yZXNvdXJjZXMvdjFhbHBoYTMvaW50ZXJmYWNlLmdv) | `75.59% <66.66%> (+0.39%)` | :arrow_up: |
| [...devops/v1alpha3/pipelinerun/backwardlisthandler.go](https://codecov.io/gh/kubesphere/ks-devops/pull/242/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-cGtnL2thcGlzL2Rldm9wcy92MWFscGhhMy9waXBlbGluZXJ1bi9iYWNrd2FyZGxpc3RoYW5kbGVyLmdv) | `91.66% <0.00%> (-4.17%)` | :arrow_down: |
| [controllers/pipeline/pipeline\_controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/242/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvcGlwZWxpbmUvcGlwZWxpbmVfY29udHJvbGxlci5nbw==) | `38.80% <0.00%> (-3.74%)` | :arrow_down: |
| [...trollers/devopsproject/devopsproject\_controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/242/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvZGV2b3BzcHJvamVjdC9kZXZvcHNwcm9qZWN0X2NvbnRyb2xsZXIuZ28=) | `40.41% <0.00%> (-2.60%)` | :arrow_down: |
| [...rs/devopscredential/devopscredential\_controller.go](https://codecov.io/gh/kubesphere/ks-devops/pull/242/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere#diff-Y29udHJvbGxlcnMvZGV2b3BzY3JlZGVudGlhbC9kZXZvcHNjcmVkZW50aWFsX2NvbnRyb2xsZXIuZ28=) | `36.48% <0.00%> (+3.37%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kubesphere/ks-devops/pull/242?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/242?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kubesphere). Last update [e65d92f...1a5082b](https://codecov.io/gh/kubesphere/ks-devops/pull/242?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/kapis/devops/v1alpha2/devops.go b/pkg/kapis/devops/v1alpha2/devops.go
index d921328..249c040 100644
--- a/pkg/kapis/devops/v1alpha2/devops.go
+++ b/pkg/kapis/devops/v1alpha2/devops.go
@@ -127,18 +127,16 @@ func (h *ProjectPipelineHandler) ListPipelines(req *restful.Request, resp *restf
 	// get all pipelines which come from ks
 	pipelineList := &clientDevOps.PipelineList{
 		Total: objs.TotalItems,
-		Items: make([]clientDevOps.Pipeline, len(objs.Items)),
+		Items: make([]clientDevOps.Pipeline, 0, len(objs.Items)),
 	}
-	pipelineMap := make(map[string]int, pipelineList.Total)
-	for i, _ := range objs.Items {
-		if pipeline, ok := objs.Items[i].(*v1alpha3.Pipeline); !ok {
-			continue
-		} else {
+	pipelineMap := make(map[string]int)
+	for i := range objs.Items {
+		if pipeline, ok := objs.Items[i].(*v1alpha3.Pipeline); ok {
 			pipelineMap[pipeline.Name] = i
-			pipelineList.Items[i] = clientDevOps.Pipeline{
+			pipelineList.Items = append(pipelineList.Items, clientDevOps.Pipeline{
 				Name:        pipeline.Name,
 				Annotations: pipeline.Annotations,
-			}
+			})
 		}
 	}
 
@@ -163,8 +161,7 @@ func (h *ProjectPipelineHandler) ListPipelines(req *restful.Request, resp *restf
 		}
 	}
 
-	resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
-	resp.WriteAsJson(pipelineList)
+	_ = resp.WriteEntity(pipelineList)
 }
 
 func (h *ProjectPipelineHandler) GetPipelineRun(req *restful.Request, resp *restful.Response) {
diff --git a/pkg/models/resources/v1alpha3/interface.go b/pkg/models/resources/v1alpha3/interface.go
index a2f5cdb..4421afc 100644
--- a/pkg/models/resources/v1alpha3/interface.go
+++ b/pkg/models/resources/v1alpha3/interface.go
@@ -98,6 +98,9 @@ func DefaultList(objects []runtime.Object, q *query.Query, compareFunc CompareFu
 	var filtered []runtime.Object
 	// filter objects
 	for _, object := range objects {
+		if object == nil || reflect.ValueOf(object).IsNil() {
+			continue
+		}
 		selected := true
 		for field, value := range q.Filters {
 			if filterFunc != nil && !filterFunc(object, query.Filter{Field: field, Value: value}) {
@@ -168,7 +171,7 @@ func DefaultFilter() FilterFunc {
 	return func(obj runtime.Object, filter query.Filter) bool {
 		oma, ok := obj.(metav1.ObjectMetaAccessor)
 		if !ok || oma == nil || reflect.ValueOf(oma).IsNil() {
-			return true
+			return false
 		}
 		return DefaultObjectMetaFilter(oma.GetObjectMeta(), filter)
 	}


Test patch:
diff --git a/pkg/models/resources/v1alpha3/interface_test.go b/pkg/models/resources/v1alpha3/interface_test.go
index 8d23ba1..7b33c05 100644
--- a/pkg/models/resources/v1alpha3/interface_test.go
+++ b/pkg/models/resources/v1alpha3/interface_test.go
@@ -405,6 +405,39 @@ func TestDefaultList(t *testing.T) {
 				},
 			},
 		},
+	}, {
+		name: "Filter nil items",
+		items: []runtime.Object{
+			&v1alpha3.Pipeline{
+				ObjectMeta: v1.ObjectMeta{
+					Name: "pipeline-abcd",
+				},
+			},
+			nil,
+			&v1alpha3.Pipeline{
+				ObjectMeta: v1.ObjectMeta{
+					Name: "pipeline-cdef",
+				},
+			},
+			nil,
+			nil,
+		},
+		filterFunc: DefaultFilter(),
+		expectedResult: api.ListResult{
+			TotalItems: 2,
+			Items: []interface{}{
+				&v1alpha3.Pipeline{
+					ObjectMeta: v1.ObjectMeta{
+						Name: "pipeline-abcd",
+					},
+				},
+				&v1alpha3.Pipeline{
+					ObjectMeta: v1.ObjectMeta{
+						Name: "pipeline-cdef",
+					},
+				},
+			},
+		},
 	}}
 	for _, item := range table {
 		t.Run(item.name, func(t *testing.T) {