-
Notifications
You must be signed in to change notification settings - Fork 144
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
Introducing Workspaces into pipelines #793
Changes from all commits
12e0c2a
f3cf6a8
6791fdd
97ccb38
58275f3
4d7f442
0c5f7c2
e818580
1bb516f
07f9650
be4afde
9d51140
a77a8f0
a2208e2
0a8b07d
9e0fb7a
7f1065a
9f2dfce
6aee4dc
6429692
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package services | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
Check failure on line 5 in pipelines/services/validate.go GitHub Actions / Go-Sec
Check failure on line 5 in pipelines/services/validate.go GitHub Actions / Static-Check
|
||
"github.com/gookit/color" | ||
"github.com/jfrog/jfrog-client-go/auth" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"github.com/jfrog/jfrog-client-go/utils/io/httputils" | ||
"github.com/jfrog/jfrog-client-go/utils/log" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
) | ||
|
||
const ( | ||
validatePipResourcePath = "api/v1/validateYaml" | ||
) | ||
|
||
type ValidateService struct { | ||
client *jfroghttpclient.JfrogHttpClient | ||
auth.ServiceDetails | ||
} | ||
|
||
func NewValidateService(client *jfroghttpclient.JfrogHttpClient) *ValidateService { | ||
return &ValidateService{client: client} | ||
} | ||
|
||
func (vs *ValidateService) getHttpDetails() httputils.HttpClientDetails { | ||
httpDetails := vs.CreateHttpClientDetails() | ||
return httpDetails | ||
} | ||
|
||
func (vs *ValidateService) ValidatePipeline(data []byte) error { | ||
var err error | ||
httpDetails := vs.getHttpDetails() | ||
|
||
// Query params | ||
m := make(map[string]string, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is "m"? |
||
|
||
// URL Construction | ||
uri := vs.constructValidateAPIURL(m, validatePipResourcePath) | ||
|
||
// Headers | ||
headers := make(map[string]string) | ||
utils.SetContentType("application/json", &headers) | ||
Check failure on line 47 in pipelines/services/validate.go GitHub Actions / Go-Sec
Check failure on line 47 in pipelines/services/validate.go GitHub Actions / Static-Check
|
||
httpDetails.Headers = headers | ||
|
||
// Send post request | ||
resp, body, err := vs.client.SendPost(uri, data, &httpDetails) | ||
if err != nil { | ||
return err | ||
} | ||
if err := errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil { | ||
return err | ||
} | ||
|
||
// Process response | ||
if nil != resp && resp.StatusCode == http.StatusOK { | ||
log.Info("Processing resources yaml response ") | ||
log.Info(string(body)) | ||
err = processValidatePipResourceResponse(body) | ||
if err != nil { | ||
log.Error("Failed to process resources validation response") | ||
} | ||
} | ||
Comment on lines
+60
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You already check the status code in CheckResponseStatusWithBody. Please also consider nil before "CheckResponseStatusWithBody". |
||
return nil | ||
} | ||
|
||
// processValidatePipResourceResponse process validate pipeline resource response | ||
func processValidatePipResourceResponse(resp []byte) error { | ||
validationResponse := make(map[string]ValidationResponse) | ||
err := json.Unmarshal(resp, &validationResponse) | ||
if err != nil { | ||
return errorutils.CheckError(err) | ||
} | ||
if len(validationResponse) == 0 { | ||
return errorutils.CheckErrorf("pipelines not found") | ||
} | ||
for k, v := range validationResponse { | ||
if v.IsValid != nil && *v.IsValid { | ||
log.Info("Validation of pipeline resources completed successfully") | ||
msg := color.Green.Sprintf("Validation completed") | ||
log.Info(msg) | ||
} else { | ||
fileName := color.Red.Sprintf("%s", k) | ||
log.Error(fileName) | ||
validationErrors := v.Errors | ||
for _, validationError := range validationErrors { | ||
lineNum := validationError.LineNumber | ||
errorMessage := color.Red.Sprintf("%s", validationError.Text+":"+strconv.Itoa(lineNum)) | ||
log.Error(errorMessage) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// constructPipelinesURL creates URL with all required details to make api call | ||
// like headers, queryParams, apiPath | ||
func (vs *ValidateService) constructValidateAPIURL(qParams map[string]string, apiPath string) string { | ||
uri, err := url.Parse(vs.ServiceDetails.GetUrl() + apiPath) | ||
if err != nil { | ||
log.Error("Failed to parse pipelines fetch run status url") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please return an error here |
||
} | ||
|
||
queryString := uri.Query() | ||
for k, v := range qParams { | ||
queryString.Set(k, v) | ||
} | ||
uri.RawQuery = queryString.Encode() | ||
|
||
return uri.String() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this login be inside workspaceService.GetWorkspacePipelines? Let's try to keep the manager clean.