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

WKS-1193 - Use dynamic actions #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ehl-jf
Copy link
Collaborator

@ehl-jf ehl-jf commented Nov 26, 2024

  • The pull request is targeting the main branch.
  • The code has been validated to compile successfully by running go vet ./....
  • The code has been formatted properly using go fmt ./....
  • All static analysis checks passed.
  • All tests have passed. If this feature is not already covered by the tests, new tests have been added.
  • All integration tests have passed locally as they cannot be automated yet.
  • All changes are detailed at the description. if not already covered at JFrog Documentation, new documentation have been added.

@@ -36,6 +36,9 @@ jobs:
go-version-file: go.mod

- name: Run Gosec Security Scanner
uses: securego/[email protected]
uses: securego/[email protected]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't gosec part of golangci-lint?
See https://golangci-lint.run/usage/linters/#gosec

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, we copied it as is from other plugins. We can definitely improve.

return names
}

func (c ActionsMetadata) FindAction(actionName string, service ...string) (*model.ActionMetadata, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an "option" pattern instead of service ...string to create optional parameters.

Example:

am.FindAction("my-action", WithApplication("my-app"))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is over engineering for just one option. We wont have others.

return apiError(http.StatusInternalServerError, "%+v", err)
}

apiEndpoint := fmt.Sprintf("%sworker/api/v%d/%s", utils.AddTrailingSlashIfNeeded(params.ServerUrl), params.ApiVersion+1, strings.Join(params.Path, "/"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params.ApiVersion+1 seems rather awkward.
I suggest to use ApiVersion == 0 to mean default (1 now, but may change in the future) and to use any other value "as is".

Copy link
Collaborator Author

@ehl-jf ehl-jf Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an package internal type declared using the go iota thing.
So ApiVersion == 0 is already the default.
But we can set it to 1 explicitely so that we dont have to use +1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is internal, no worries. It is just weird...

return apiError(http.StatusInternalServerError, "unexpected error: %+v", err)
}

if slices.Index(params.OkStatuses, res.StatusCode) == -1 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we provide meaningful default value for params.OkStatuses?


defer CloseQuietly(res.Body)

if res.ContentLength > 0 || res.ContentLength == -1 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: invert the condition

Suggested change
if res.ContentLength > 0 || res.ContentLength == -1 {
if res.ContentLength == 0 {
// discard
} else {
//consume
}

)

// ReadManifest reads a manifest from the working directory or from the directory provided as argument.
func ReadManifest(dir ...string) (*model.Manifest, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using a variadic function to implement optional arg

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, used only for tests.

tsFieldAccessPattern = regexp.MustCompile(`\W([A-Z][a-zA-Z0-9]+)\.[a-zA-Z0-9]+`)
tsTypeInTypeParameters = regexp.MustCompile(`<([A-Z][a-zA-Z0-9]+)>`)
tsExcludeTypes = []string{"PlatformContext"}
tsUnexportedType = regexp.MustCompile(`^(class|type|interface|enum|const)\s+[A-Za-z_$][0-9A-Za-z_$]*`)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accept whitespace at the beginning

Suggested change
tsUnexportedType = regexp.MustCompile(`^(class|type|interface|enum|const)\s+[A-Za-z_$][0-9A-Za-z_$]*`)
tsUnexportedType = regexp.MustCompile(`^\s*(class|type|interface|enum|const)\s+[A-Za-z_$][0-9A-Za-z_$]*`)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each line is actually trimmed before matching.

}

return callWorkerApiWithOutput(c, server.GetUrl(), server.GetAccessToken(), http.MethodGet, nil, http.StatusOK, queryParams, "actions")
return common.Print("%s", strings.Join(actions, ", "))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return common.Print("%s", strings.Join(actions, ", "))
return common.Print(strings.Join(actions, ", "))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it causes a govet lint error, that the reason is like this.

type ActionMetadata struct {
Action Action `json:"action"`
Description string `json:"description"`
SamplePayload string `json:"samplePayload"`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be snake_case instead of camelCase?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backend uses this case.

model/flags.go Outdated
func GetApplicationFlag() components.StringFlag {
return components.NewStringFlag(
FlagApplication,
"The application that provides the event. If omitted worker will try to guess it and will raised an error if it could not.",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"The application that provides the event. If omitted worker will try to guess it and will raised an error if it could not.",
"The application that provides the event. If omitted, worker will try to guess it and will raise an error if it cannot.",

timeout, err := model.GetTimeoutParameter(c)
if err != nil {
return apiError(http.StatusInternalServerError, "%+v", err)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt it confusing to return an http error when we didnt call the server ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status is for internal use only.
Only err.Error() meaning the message is showed to the end user.


apiEndpoint := fmt.Sprintf("%sworker/api/v%d/%s", utils.AddTrailingSlashIfNeeded(params.ServerUrl), params.ApiVersion+1, strings.Join(params.Path, "/"))

if params.Query != nil || params.ProjectKey != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there isnt a built in way to generate query params ? with url apis

}
}

reqCtx, cancelReq := context.WithTimeout(context.Background(), timeout)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the context should be a parameter

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context is only used for request timeout.
I want to avoid duplicating context creation and cancellation on all the command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants