Skip to content

Commit

Permalink
Merge pull request #4 from shijl0925/create-pull-request/patch
Browse files Browse the repository at this point in the history
Changes by create-pull-request action
  • Loading branch information
shijl0925 authored Jul 8, 2024
2 parents b362e12 + 793446f commit 86eba2f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 50 deletions.
4 changes: 2 additions & 2 deletions sonarqube/qualitygates/qualitygates_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/> Only metric of the following types are allowed:<ul><li>INT</li><li>MILLISEC</li><li>RATING</li><li>WORK_DUR</li><li>FLOAT</li><li>PERCENT</li><li>LEVEL</li></ul>Following metrics are forbidden:<ul><li>security_hotspots</li><li>alert_status</li><li>new_security_hotspots</li></ul>
Metric string `form:"metric"` // Condition metric.<br/> Only metric of the following types are allowed:<ul><li>INT</li><li>MILLISEC</li><li>RATING</li><li>WORK_DUR</li><li>FLOAT</li><li>PERCENT</li><li>LEVEL</li></ul>Following metrics are forbidden:<ul><li>new_security_hotspots</li><li>security_hotspots</li><li>alert_status</li></ul>
Op string `form:"op,omitempty"` // Condition operator:<br/><ul><li>LT = is lower than</li><li>GT = is greater than</li></ul>
}

Expand Down Expand Up @@ -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.<br/> Only metric of the following types are allowed:<ul><li>INT</li><li>MILLISEC</li><li>RATING</li><li>WORK_DUR</li><li>FLOAT</li><li>PERCENT</li><li>LEVEL</li></ul>Following metrics are forbidden:<ul><li>security_hotspots</li><li>alert_status</li><li>new_security_hotspots</li></ul>
Metric string `form:"metric"` // Condition metric.<br/> Only metric of the following types are allowed:<ul><li>INT</li><li>MILLISEC</li><li>RATING</li><li>WORK_DUR</li><li>FLOAT</li><li>PERCENT</li><li>LEVEL</li></ul>Following metrics are forbidden:<ul><li>new_security_hotspots</li><li>security_hotspots</li><li>alert_status</li></ul>
Op string `form:"op,omitempty"` // Condition operator:<br/><ul><li>LT = is lower than</li><li>GT = is greater than</li></ul>
}
80 changes: 32 additions & 48 deletions sonarqube/sonarqube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

0 comments on commit 86eba2f

Please sign in to comment.