-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add support for requesting Curation waivers to the CLI #280
base: dev
Are you sure you want to change the base?
Conversation
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.
LGTM, please:
- expend the description in the PR, what is that waiver? can you attach some ScreenShot or snippet of the changes that the user will be faced.
- Make sure your PM went over the Text and approve it.
- Please change the documentation if needed
} | ||
|
||
func (ca *CurationAuditCommand) sendWaiverRequests(pkgs []*PackageStatus, msg string, serverDetails *config.ServerDetails) (requestStatuses []WaiverResponse, err error) { | ||
fmt.Print("Submitting waiver request...\n\n") |
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.
fmt.Print("Submitting waiver request...\n\n") | |
log.Info("Submitting waiver request...\n\n") |
use the log from "github.com/jfrog/jfrog-client-go/utils/log"
(you can also use log.Output
)
_, body, _, err := rtManager.Client().SendGet(pkg.BlockedPackageUrl, true, &clientDetails) | ||
if err != nil { | ||
return nil, err | ||
} |
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.
_, body, _, err := rtManager.Client().SendGet(pkg.BlockedPackageUrl, true, &clientDetails) | |
if err != nil { | |
return nil, err | |
} | |
platformResponse, body, _, err := rtManager.Client().SendGet(pkg.BlockedPackageUrl, true, &clientDetails) | |
if err != nil { | |
return nil, errors.New("failed while attempting to get waiver:\n" + err.Error()) | |
} | |
if err = errorutils.CheckResponseStatusWithBody(platformResponse, body, http.StatusOK); err != nil { | |
return nil, errors.New("got unexpected server response while attempting to get waiver:\n" + err.Error()) | |
} |
I would also validate the response status.
Please also wrap error with text to help debugging
_, body, _, err := rtManager.Client().SendGet(pkg.BlockedPackageUrl, true, &clientDetails) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var resp struct { | ||
Errors []struct { | ||
Status int `json:"status"` | ||
Message string `json:"message"` | ||
} `json:"errors"` | ||
} | ||
if err := json.Unmarshal(body, &resp); err != nil { | ||
return nil, errors.New("failed decoding waiver request status") | ||
} | ||
|
||
var id, status, message string | ||
parts := strings.Split(resp.Errors[0].Message, "|") | ||
if len(parts) != 2 { | ||
return nil, errors.New("failed decoding waiver request status") | ||
} | ||
id, status = parts[0], parts[1] | ||
switch status { | ||
case "pending": | ||
message = WaiverRequestPending | ||
case "approved": | ||
message = WaiverRequestApproved | ||
case "forbidden": | ||
message = WaiverRequestForbidden | ||
} | ||
requestStatuses = append(requestStatuses, WaiverResponse{ | ||
PkgName: pkg.PackageName, | ||
Status: status, | ||
WaiverID: id, | ||
Explanation: message, | ||
}) |
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.
I would consider refactoring this into its own method for readability
if err := json.Unmarshal(body, &resp); err != nil { | ||
return nil, errors.New("failed decoding waiver request status") | ||
} |
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.
if err := json.Unmarshal(body, &resp); err != nil { | |
return nil, errors.New("failed decoding waiver request status") | |
} | |
if err := json.Unmarshal(body, &resp); err != nil { | |
return nil, fmt.Errorf("failed decoding waiver request status: " + err.Error()) | |
} |
don't ignore the actual error
var id, status, message string | ||
parts := strings.Split(resp.Errors[0].Message, "|") | ||
if len(parts) != 2 { | ||
return nil, errors.New("failed decoding waiver request status") |
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.
return nil, errors.New("failed decoding waiver request status") | |
return nil, fmt.Errorf("failed decoding waiver request status, got unexpected Error msg format while trying to split into parts (%d expected 2)", len(parts)) |
describe the error so it will be unique
if len(requestMsg) >= 5 && len(requestMsg) <= 300 { | ||
break | ||
} | ||
fmt.Print("The reason must be between 5 and 300 characters.\n\n") |
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.
fmt.Print("The reason must be between 5 and 300 characters.\n\n") | |
log.Output("The reason must be between 5 and 300 characters.\n\n") |
} | ||
|
||
var id, status, message string | ||
parts := strings.Split(resp.Errors[0].Message, "|") |
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.
why resp.Errors[0]
? what about the other errors? why only one (and is it always exists? could it be empty?)
@@ -461,6 +484,136 @@ func (ca *CurationAuditCommand) auditTree(tech techutils.Technology, results map | |||
return err | |||
} | |||
|
|||
func getSelectedPackages(requestedRows string, blockedPackages []*PackageStatus) (selectedPackages []*PackageStatus, ok bool) { | |||
validFormat := regexp.MustCompile(`^(all|(\d+(-\d+)?)(,\d+(-\d+)?)*$)`) |
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.
comment what is the valid format that the regex describes to help readability
func getSelectedPackages(requestedRows string, blockedPackages []*PackageStatus) (selectedPackages []*PackageStatus, ok bool) { | ||
validFormat := regexp.MustCompile(`^(all|(\d+(-\d+)?)(,\d+(-\d+)?)*$)`) | ||
if !validFormat.MatchString(requestedRows) { | ||
fmt.Print("Invalid request format.\n\n") |
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.
fmt.Print("Invalid request format.\n\n") | |
log.Output("Invalid request format.\n\n") |
use log.Output
, also maybe hint the user what is the valid format if it can be controlled (or add debug log if it coming from our platform for debugging when issue occur)
parts := strings.Split(requestedRows, ",") | ||
for _, part := range parts { | ||
if strings.Contains(part, "-") { | ||
rangeParts := strings.Split(part, "-") | ||
start, _ := strconv.Atoi(rangeParts[0]) | ||
end, _ := strconv.Atoi(rangeParts[1]) | ||
for i := start; i <= end; i++ { | ||
if slices.Contains(indices, i) { | ||
continue | ||
} | ||
indices = append(indices, i) | ||
} | ||
} else { | ||
index, err := strconv.Atoi(part) | ||
if err != nil || slices.Contains(indices, index) { | ||
continue | ||
} | ||
indices = append(indices, index) | ||
} | ||
} |
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.
parts := strings.Split(requestedRows, ",") | |
for _, part := range parts { | |
if strings.Contains(part, "-") { | |
rangeParts := strings.Split(part, "-") | |
start, _ := strconv.Atoi(rangeParts[0]) | |
end, _ := strconv.Atoi(rangeParts[1]) | |
for i := start; i <= end; i++ { | |
if slices.Contains(indices, i) { | |
continue | |
} | |
indices = append(indices, i) | |
} | |
} else { | |
index, err := strconv.Atoi(part) | |
if err != nil || slices.Contains(indices, index) { | |
continue | |
} | |
indices = append(indices, index) | |
} | |
} | |
rows := strings.Split(requestedRows, ",") | |
for _, rowStr := range parts { | |
if strings.Contains(rowStr, "-") { | |
// Range of rows requested | |
rangeParts := strings.Split(rowStr, "-") | |
startRow, _ := strconv.Atoi(rangeParts[0]) | |
endRow, _ := strconv.Atoi(rangeParts[1]) | |
for i := start; i <= end; i++ { | |
if slices.Contains(indices, i) { | |
continue | |
} | |
indices = append(indices, i) | |
} | |
} else { | |
// Single row requested | |
index, err := strconv.Atoi(rowStr) | |
if err != nil || slices.Contains(indices, index) { | |
continue | |
} | |
indices = append(indices, index) | |
} | |
} |
rename for readability, maybe also refactor this to a separated method
add some comments
dev
branch.go vet ./...
.go fmt ./...
.Adds support for requesting waivers from the CLI. The flow augments the
curation audit
command, and is effective only for interactive shells.