diff --git a/sonarqube/qualitygates/qualitygates_gen.go b/sonarqube/qualitygates/qualitygates_gen.go
index 5586623..8ed5021 100644
--- a/sonarqube/qualitygates/qualitygates_gen.go
+++ b/sonarqube/qualitygates/qualitygates_gen.go
@@ -37,7 +37,7 @@ type CreateResponse struct {
type CreateConditionRequest struct {
Error string `form:"error"` // Condition error threshold
GateName string `form:"gateName"` // Name of the quality gate
- Metric string `form:"metric"` // Condition metric.
Only metric of the following types are allowed:
- INT
- MILLISEC
- RATING
- WORK_DUR
- FLOAT
- PERCENT
- LEVEL
Following metrics are forbidden:- security_hotspots
- alert_status
- new_security_hotspots
+ Metric string `form:"metric"` // Condition metric.
Only metric of the following types are allowed:- INT
- MILLISEC
- RATING
- WORK_DUR
- FLOAT
- PERCENT
- LEVEL
Following metrics are forbidden:- new_security_hotspots
- security_hotspots
- alert_status
Op string `form:"op,omitempty"` // Condition operator:
- LT = is lower than
- GT = is greater than
}
@@ -281,6 +281,6 @@ type ShowResponse struct {
type UpdateConditionRequest struct {
Error string `form:"error"` // Condition error threshold
Id string `form:"id"` // Condition ID
- Metric string `form:"metric"` // Condition metric.
Only metric of the following types are allowed:- INT
- MILLISEC
- RATING
- WORK_DUR
- FLOAT
- PERCENT
- LEVEL
Following metrics are forbidden:- security_hotspots
- alert_status
- new_security_hotspots
+ Metric string `form:"metric"` // Condition metric.
Only metric of the following types are allowed:- INT
- MILLISEC
- RATING
- WORK_DUR
- FLOAT
- PERCENT
- LEVEL
Following metrics are forbidden:- new_security_hotspots
- security_hotspots
- alert_status
Op string `form:"op,omitempty"` // Condition operator:
- LT = is lower than
- GT = is greater than
}
diff --git a/sonarqube/sonarqube.go b/sonarqube/sonarqube.go
index 2e2d58a..72dc7aa 100644
--- a/sonarqube/sonarqube.go
+++ b/sonarqube/sonarqube.go
@@ -5,13 +5,12 @@ import (
"encoding/json"
"fmt"
"github.com/go-playground/form/v4"
+ "github.com/google/go-querystring/query"
"io"
"net/http"
+ "net/url"
"reflect"
- "strconv"
"strings"
-
- "github.com/iancoleman/strcase"
)
const (
@@ -168,7 +167,7 @@ func (c *Client) handleAuth(req *http.Request) {
}
}
-func (c *Client) NewRequest(ctx context.Context, method, url string, body io.Reader, params ...string) (*http.Request, error) {
+func (c *Client) NewRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, err
@@ -177,17 +176,6 @@ func (c *Client) NewRequest(ctx context.Context, method, url string, body io.Rea
// 认证处理
c.handleAuth(req)
- // 如果是GET请求且有参数,设置参数
- if method == "GET" && len(params) > 0 {
- q := req.URL.Query()
-
- for i := 0; i < len(params); i++ {
- q.Add(params[i], params[i+1])
- i++
- }
- req.URL.RawQuery = q.Encode()
- }
-
// 设置通用请求头
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
@@ -216,9 +204,17 @@ func (c *Client) Call(ctx context.Context, method string, u string, v interface{
u = fmt.Sprintf("%s/%s", c.host, u)
var req *http.Request
var err error
+
if method == http.MethodGet {
- params := paramsFrom(opt...)
- req, err = c.NewRequest(ctx, "GET", u, nil, params...)
+ for _, o := range opt {
+ urlStr, err := addOptions(u, o)
+ if err != nil {
+ return nil, err
+ }
+ u = urlStr
+ }
+
+ req, err = c.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, fmt.Errorf("could not create request: %v", err)
}
@@ -265,42 +261,30 @@ func (c *Client) Call(ctx context.Context, method string, u string, v interface{
return resp, err
}
-// paramsFrom creates a slice with interleaving param and value entries, i.e. ["key1", "value1", "key2, "value2"]
-func paramsFrom(items ...interface{}) []string {
- allParams := make([]string, 0)
-
- for _, item := range items {
- v := reflect.ValueOf(item)
- t := v.Type()
+func addOptions(s string, opt interface{}) (string, error) {
+ v := reflect.ValueOf(opt)
- params := make([]string, 2*v.NumField())
-
- for i := 0; i < v.NumField(); i++ {
- j := i * 2
- k := j + 1
+ if v.Kind() == reflect.Ptr && v.IsNil() {
+ return s, nil
+ }
- if v.Field(i).IsZero() {
- continue
- }
+ origURL, err := url.Parse(s)
+ if err != nil {
+ return s, err
+ }
- // Convert some basic types to strings for convenience.
- // Note: other types should not be used as parameter values.
- fieldValue := ""
- switch t.Field(i).Type.Name() {
- case "int":
- fieldValue = strconv.Itoa(v.Field(i).Interface().(int))
- case "string":
- fieldValue = v.Field(i).Interface().(string)
- case "bool":
- fieldValue = strconv.FormatBool(v.Field(i).Interface().(bool))
- }
+ origValues := origURL.Query()
- params[j] = strcase.ToLowerCamel(t.Field(i).Name)
- params[k] = fieldValue
- }
+ newValues, err := query.Values(opt)
+ if err != nil {
+ return s, err
+ }
- allParams = append(allParams, params...)
+ for k, v := range newValues {
+ origValues[k] = v
}
- return allParams
+ origURL.RawQuery = origValues.Encode()
+
+ return origURL.String(), nil
}