|
9 | 9 | "github.com/jfrog/jfrog-cli-application/apptrust/service" |
10 | 10 |
|
11 | 11 | "github.com/jfrog/jfrog-cli-application/apptrust/model" |
| 12 | + "github.com/jfrog/jfrog-client-go/utils/log" |
12 | 13 | ) |
13 | 14 |
|
14 | 15 | type VersionService interface { |
@@ -39,46 +40,67 @@ func (vs *versionService) CreateAppVersion(ctx service.Context, request *model.C |
39 | 40 | } |
40 | 41 |
|
41 | 42 | func (vs *versionService) PromoteAppVersion(ctx service.Context, applicationKey, version string, request *model.PromoteAppVersionRequest, sync bool) error { |
42 | | - endpoint := fmt.Sprintf("/v1/applications/%s/versions/%s/promote", applicationKey, version) |
43 | | - response, responseBody, err := ctx.GetHttpClient().Post(endpoint, request, map[string]string{"async": strconv.FormatBool(!sync)}) |
44 | | - if err != nil { |
45 | | - return err |
46 | | - } |
47 | | - |
48 | | - if response.StatusCode >= 400 { |
49 | | - return fmt.Errorf("failed to promote app version. Status code: %d. \n%s", |
50 | | - response.StatusCode, responseBody) |
51 | | - } |
52 | | - |
53 | | - return nil |
| 43 | + return vs.requestOperation(ctx, applicationKey, version, "promote", request, sync) |
54 | 44 | } |
55 | 45 |
|
56 | 46 | func (vs *versionService) ReleaseAppVersion(ctx service.Context, applicationKey, version string, request *model.ReleaseAppVersionRequest, sync bool) error { |
57 | | - endpoint := fmt.Sprintf("/v1/applications/%s/versions/%s/release", applicationKey, version) |
58 | | - response, responseBody, err := ctx.GetHttpClient().Post(endpoint, request, map[string]string{"async": strconv.FormatBool(!sync)}) |
| 47 | + return vs.requestOperation(ctx, applicationKey, version, "release", request, sync) |
| 48 | +} |
| 49 | + |
| 50 | +func (vs *versionService) DeleteAppVersion(ctx service.Context, applicationKey, version string) error { |
| 51 | + url := fmt.Sprintf("/v1/applications/%s/versions/%s", applicationKey, version) |
| 52 | + response, responseBody, err := ctx.GetHttpClient().Delete(url, nil) |
59 | 53 | if err != nil { |
60 | 54 | return err |
61 | 55 | } |
62 | 56 |
|
63 | | - if response.StatusCode >= 400 { |
64 | | - return fmt.Errorf("failed to release app version. Status code: %d. \n%s", |
| 57 | + if response.StatusCode != 204 { |
| 58 | + return fmt.Errorf("failed to delete app version. Status code: %d.\n%s", |
65 | 59 | response.StatusCode, responseBody) |
66 | 60 | } |
67 | 61 |
|
68 | 62 | return nil |
69 | 63 | } |
70 | 64 |
|
71 | | -func (vs *versionService) DeleteAppVersion(ctx service.Context, applicationKey, version string) error { |
72 | | - url := fmt.Sprintf("/v1/applications/%s/versions/%s", applicationKey, version) |
73 | | - response, responseBody, err := ctx.GetHttpClient().Delete(url, nil) |
| 65 | +// Common methods |
| 66 | + |
| 67 | +// requestOperation is a generic method for performing version operations via POST requests |
| 68 | +// It handles both promote and release operations |
| 69 | +func (vs *versionService) requestOperation(ctx service.Context, applicationKey, version, operation string, payload interface{}, sync bool) error { |
| 70 | + endpoint := fmt.Sprintf(VersionEndpointPattern, applicationKey, version, operation) |
| 71 | + |
| 72 | + // Log operation details |
| 73 | + log.Debug(fmt.Sprintf("Performing %s operation", operation), |
| 74 | + "applicationKey", applicationKey, |
| 75 | + "version", version, |
| 76 | + "sync", sync, |
| 77 | + "endpoint", endpoint) |
| 78 | + |
| 79 | + response, responseBody, err := ctx.GetHttpClient().Post(endpoint, payload, map[string]string{"async": strconv.FormatBool(!sync)}) |
74 | 80 | if err != nil { |
75 | 81 | return err |
76 | 82 | } |
77 | 83 |
|
78 | | - if response.StatusCode != 204 { |
79 | | - return fmt.Errorf("failed to delete app version. Status code: %d.\n%s", |
80 | | - response.StatusCode, responseBody) |
| 84 | + if response.StatusCode >= 400 { |
| 85 | + // Get appropriate error message based on operation |
| 86 | + var errorMessage string |
| 87 | + switch operation { |
| 88 | + case "promote": |
| 89 | + errorMessage = ErrorPromoteAppVersion |
| 90 | + case "release": |
| 91 | + errorMessage = ErrorReleaseAppVersion |
| 92 | + default: |
| 93 | + errorMessage = fmt.Sprintf("failed to %s app version", operation) |
| 94 | + } |
| 95 | + |
| 96 | + return fmt.Errorf("%s. Status code: %d. \n%s", |
| 97 | + errorMessage, response.StatusCode, responseBody) |
81 | 98 | } |
82 | 99 |
|
| 100 | + log.Debug(fmt.Sprintf("%s operation successful", operation), |
| 101 | + "applicationKey", applicationKey, |
| 102 | + "version", version, |
| 103 | + "statusCode", response.StatusCode) |
| 104 | + |
83 | 105 | return nil |
84 | 106 | } |
0 commit comments