Skip to content

Commit

Permalink
Handles both string and bool type for the approval field in the updat…
Browse files Browse the repository at this point in the history
…e api

Signed-off-by: PuneetPunamiya <ppunamiy@redhat.com>
  • Loading branch information
PuneetPunamiya authored and vdemeester committed Nov 23, 2023
1 parent 036b291 commit ace95fb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
23 changes: 20 additions & 3 deletions pkg/handlers/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package app

import (
"fmt"
"k8s.io/apimachinery/pkg/runtime/schema"
"strings"
)

const (
Expand All @@ -34,10 +36,25 @@ var (
}
)

type BoolValue string

func (bv *BoolValue) UnmarshalJSON(data []byte) error {
strVal := strings.Trim(string(data), "\"")
switch strVal {
case "true":
*bv = BoolValue("true")
case "false":
*bv = BoolValue("false")
default:
return fmt.Errorf("Invalid value: %s", strVal)
}
return nil
}

type ApprovalTask struct {
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
Approved string `json:"approved"`
Name string `json:"name"`
Namespace string `json:"namespace,omitempty"`
Approved BoolValue `json:"approved"`
}

type ApprovalTaskList struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ListApprovalTask(res http.ResponseWriter, req *http.Request, dynamicClient
approvalTaskList := make([]app.ApprovalTask, 0)
for _, cr := range customResourceList.Items {
approved := cr.Object["spec"].(map[string]interface{})["approved"].(string)
approvalTaskList = append(approvalTaskList, app.ApprovalTask{Name: cr.GetName(), Namespace: cr.GetNamespace(), Approved: approved})
approvalTaskList = append(approvalTaskList, app.ApprovalTask{Name: cr.GetName(), Namespace: cr.GetNamespace(), Approved: app.BoolValue(approved)})
}

approvalTask := app.ApprovalTaskList{
Expand Down
8 changes: 3 additions & 5 deletions pkg/handlers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package handlers
import (
"context"
"encoding/json"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/openshift-pipelines/manual-approval-gate/pkg/apis/approvaltask/v1alpha1"
"github.com/openshift-pipelines/manual-approval-gate/pkg/handlers/app"
Expand All @@ -29,6 +27,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"net/http"
)

func UpdateApprovalTask(res http.ResponseWriter, req *http.Request, dynamicClient dynamic.Interface) {
Expand Down Expand Up @@ -85,12 +84,11 @@ func UpdateApprovalTask(res http.ResponseWriter, req *http.Request, dynamicClien
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
at.Spec.Approved = requestBody.Approved

at.Spec.Approved = string(requestBody.Approved)
var approvalTaskStatus = &app.ApprovalTaskResult{
Data: app.ApprovalTask{
Name: at.Name,
Approved: at.Spec.Approved,
Approved: app.BoolValue(at.Spec.Approved),
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/handlers/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestUpdateApprovalTask(t *testing.T) {
ts := httptest.NewServer(r)
defer ts.Close()

data := `{"approved":"true", "namespace":"default"}`
data := `{"approved":true, "namespace":"default"}`
resp, err := http.Post(ts.URL+"/approvaltask/example-task", "application/json", strings.NewReader(data))
assert.NoError(t, err)

Expand All @@ -90,7 +90,7 @@ func TestUpdateApprovalTask(t *testing.T) {
err = json.NewDecoder(resp.Body).Decode(&approvalTask)
assert.NoError(t, err)

assert.Equal(t, "true", approvalTask.Data.Approved)
assert.Equal(t, "true", string(approvalTask.Data.Approved))
}

func TestUpdateApprovalTaskNotFound(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestApproveManualApprovalTask(t *testing.T) {
err = json.NewDecoder(resp.Body).Decode(&approvalTask)
assert.NoError(t, err)

assert.Equal(t, "true", approvalTask.Data.Approved)
assert.Equal(t, "true", string(approvalTask.Data.Approved))
})

}
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestDisApproveManualApprovalTask(t *testing.T) {
err = json.NewDecoder(resp.Body).Decode(&approvalTask)
assert.NoError(t, err)

assert.Equal(t, "false", approvalTask.Data.Approved)
assert.Equal(t, "false", string(approvalTask.Data.Approved))
})

}
Expand Down

0 comments on commit ace95fb

Please sign in to comment.