Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api/pipeline): correct type for path query param + add testing #1100

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions api/pipeline/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package pipeline
import (
"fmt"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/compiler"
Expand Down Expand Up @@ -127,19 +126,14 @@ func prepareRuleData(c *gin.Context) *pipeline.RuleData {
tag := c.Query("tag")
// capture the target parameter
target := c.Query("target")

var pathSet []string
// capture the path parameter
path := c.Query("path")
if len(path) > 0 {
pathSet = strings.Split(path, ",")
}
pathSet := c.QueryArray("path")

// if any ruledata query params were provided, create ruledata struct
if len(branch) > 0 ||
len(comment) > 0 ||
len(event) > 0 ||
len(path) > 0 ||
len(pathSet) > 0 ||
len(ruleDataRepo) > 0 ||
len(status) > 0 ||
len(tag) > 0 ||
Expand Down
93 changes: 93 additions & 0 deletions api/pipeline/compile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: Apache-2.0

package pipeline

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/gin-gonic/gin"
"github.com/go-vela/types/pipeline"
"github.com/google/go-cmp/cmp"
)

// TestPrepareRuleData tests the prepareRuleData function.
func TestPrepareRuleData(t *testing.T) {
gin.SetMode(gin.TestMode)

tests := []struct {
name string
parameters map[string]string
want *pipeline.RuleData
}{
{
name: "all params provided",
parameters: map[string]string{
"branch": "main",
"comment": "Test comment",
"event": "push",
"repo": "my-repo",
"status": "success",
"tag": "v1.0.0",
"target": "production",
"path": "README.md",
},
want: &pipeline.RuleData{
Branch: "main",
Comment: "Test comment",
Event: "push",
Repo: "my-repo",
Status: "success",
Tag: "v1.0.0",
Target: "production",
Path: []string{"README.md"},
},
},
{
name: "multiple path params",
parameters: map[string]string{
"path": "README.md",
"branch": "main",
},
want: &pipeline.RuleData{
Branch: "main",
Path: []string{"README.md", "src/main.go"},
},
},
{
name: "no params provided",
parameters: map[string]string{},
want: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)

q := req.URL.Query()
for key, value := range tt.parameters {
q.Add(key, value)
}

// add additional path parameter for multiple path test
if strings.EqualFold(tt.name, "multiple path params") {
q.Add("path", "src/main.go")
}

req.URL.RawQuery = q.Encode()

ctx, _ := gin.CreateTestContext(w)
ctx.Request = req

got := prepareRuleData(ctx)

if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("prepareRuleData() mismatch (-want +got):\n%s", diff)
}
})
}
}
Loading