-
Notifications
You must be signed in to change notification settings - Fork 6
APP-1823 - Support dry run in application CLI #62
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
Changes from all commits
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,5 @@ | ||
| package http | ||
|
|
||
| func IsSuccessStatusCode(statusCode int) bool { | ||
| return statusCode >= 200 && statusCode < 300 | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,14 +7,15 @@ import ( | |
| "net/http" | ||
| "strconv" | ||
|
|
||
| apphttp "github.com/jfrog/jfrog-cli-application/apptrust/http" | ||
| "github.com/jfrog/jfrog-cli-application/apptrust/service" | ||
| "github.com/jfrog/jfrog-client-go/utils/log" | ||
|
|
||
| "github.com/jfrog/jfrog-cli-application/apptrust/model" | ||
| ) | ||
|
|
||
| type VersionService interface { | ||
| CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest, sync bool) error | ||
| CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest, sync, dryRun bool) error | ||
| PromoteAppVersion(ctx service.Context, applicationKey string, version string, payload *model.PromoteAppVersionRequest, sync bool) error | ||
| ReleaseAppVersion(ctx service.Context, applicationKey string, version string, request *model.ReleaseAppVersionRequest, sync bool) error | ||
| RollbackAppVersion(ctx service.Context, applicationKey string, version string, request *model.RollbackAppVersionRequest, sync bool) error | ||
|
|
@@ -29,24 +30,20 @@ func NewVersionService() VersionService { | |
| return &versionService{} | ||
| } | ||
|
|
||
| func (vs *versionService) CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest, sync bool) error { | ||
| func (vs *versionService) CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest, sync, dryRun bool) error { | ||
| endpoint := fmt.Sprintf("/v1/applications/%s/versions/", request.ApplicationKey) | ||
| response, responseBody, err := ctx.GetHttpClient().Post(endpoint, request, map[string]string{"async": strconv.FormatBool(!sync)}) | ||
| response, responseBody, err := ctx.GetHttpClient().Post(endpoint, request, | ||
| map[string]string{"async": strconv.FormatBool(!sync), "dry_run": strconv.FormatBool(dryRun)}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| expectedStatusCode := http.StatusCreated | ||
| if !sync { | ||
| expectedStatusCode = http.StatusAccepted | ||
| } | ||
|
|
||
| if response.StatusCode != expectedStatusCode { | ||
| if !apphttp.IsSuccessStatusCode(response.StatusCode) { | ||
|
Collaborator
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. I'm ok with this approach, but it'd be better to change it for all commands (or use the previous approach 🙃)
Collaborator
Author
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. I changed other instances in the same file to use the new approach, which is a bit more readable |
||
| return fmt.Errorf("failed to create app version. Status code: %d. \n%s", | ||
| response.StatusCode, responseBody) | ||
| } | ||
|
|
||
| log.Info("Application version created successfully.") | ||
| logSuccessMessage(sync, request, dryRun) | ||
| log.Output(string(responseBody)) | ||
| return nil | ||
| } | ||
|
|
@@ -58,7 +55,7 @@ func (vs *versionService) PromoteAppVersion(ctx service.Context, applicationKey, | |
| return err | ||
| } | ||
|
|
||
| if response.StatusCode >= http.StatusBadRequest { | ||
| if !apphttp.IsSuccessStatusCode(response.StatusCode) { | ||
| return fmt.Errorf("failed to promote app version. Status code: %d. \n%s", | ||
| response.StatusCode, responseBody) | ||
| } | ||
|
|
@@ -74,7 +71,7 @@ func (vs *versionService) ReleaseAppVersion(ctx service.Context, applicationKey, | |
| return err | ||
| } | ||
|
|
||
| if response.StatusCode >= http.StatusBadRequest { | ||
| if !apphttp.IsSuccessStatusCode(response.StatusCode) { | ||
| return fmt.Errorf("failed to release app version. Status code: %d. \n%s", | ||
| response.StatusCode, responseBody) | ||
| } | ||
|
|
@@ -90,13 +87,7 @@ func (vs *versionService) RollbackAppVersion(ctx service.Context, applicationKey | |
| return err | ||
| } | ||
|
|
||
| // Validate status code based on sync mode | ||
| expectedStatusCode := http.StatusAccepted | ||
| if sync { | ||
| expectedStatusCode = http.StatusOK | ||
| } | ||
|
|
||
| if response.StatusCode != expectedStatusCode { | ||
| if !apphttp.IsSuccessStatusCode(response.StatusCode) { | ||
| return fmt.Errorf("failed to rollback app version. Status code: %d. \n%s", | ||
| response.StatusCode, responseBody) | ||
| } | ||
|
|
@@ -165,3 +156,13 @@ func (vs *versionService) UpdateAppVersionSources(ctx service.Context, applicati | |
| log.Output(string(responseBody)) | ||
| return nil | ||
| } | ||
|
|
||
| func logSuccessMessage(sync bool, request *model.CreateAppVersionRequest, dryRun bool) { | ||
| if !sync { | ||
| log.Info(fmt.Sprintf("Application version creation initiated: %s:%s", request.ApplicationKey, request.Version)) | ||
| } else if dryRun { | ||
| log.Info(fmt.Sprintf("Dry run successful for application version: %s:%s", request.ApplicationKey, request.Version)) | ||
| } else { | ||
| log.Info(fmt.Sprintf("Application version created successfully: %s:%s", request.ApplicationKey, request.Version)) | ||
| } | ||
| } | ||
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.
Let's add E2E test