forked from jfrog/jfrog-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev'
- Loading branch information
Showing
22 changed files
with
874 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package services | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/jfrog/jfrog-client-go/artifactory/services/utils" | ||
"github.com/jfrog/jfrog-client-go/auth" | ||
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient" | ||
utils2 "github.com/jfrog/jfrog-client-go/utils" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"github.com/jfrog/jfrog-client-go/utils/io/fileutils" | ||
"github.com/jfrog/jfrog-client-go/utils/log" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
conflictErrorMessage = "Bundle already exists" | ||
ReleaseBundleImportRestApiEndpoint = "api/release/import/" | ||
octetStream = "application/octet-stream" | ||
) | ||
|
||
type releaseService struct { | ||
client *jfroghttpclient.JfrogHttpClient | ||
ArtDetails auth.ServiceDetails | ||
} | ||
|
||
type ErrorResponseWithMessage struct { | ||
Errors []ErrorDetail `json:"errors"` | ||
} | ||
|
||
type ErrorDetail struct { | ||
Status int `json:"status"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func NewReleaseService(artDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) *releaseService { | ||
return &releaseService{client: client, ArtDetails: artDetails} | ||
} | ||
|
||
func (rs *releaseService) GetJfrogHttpClient() *jfroghttpclient.JfrogHttpClient { | ||
return rs.client | ||
} | ||
|
||
func (rs *releaseService) ImportReleaseBundle(filePath string) (err error) { | ||
// Load desired file | ||
content, err := fileutils.ReadFile(filePath) | ||
if err != nil { | ||
return | ||
} | ||
// Upload file | ||
httpClientsDetails := rs.ArtDetails.CreateHttpClientDetails() | ||
|
||
url := utils2.AddTrailingSlashIfNeeded(rs.ArtDetails.GetUrl() + ReleaseBundleImportRestApiEndpoint) | ||
|
||
utils.SetContentType(octetStream, &httpClientsDetails.Headers) | ||
var resp *http.Response | ||
var body []byte | ||
log.Info("Uploading archive...") | ||
if resp, body, err = rs.client.SendPost(url, content, &httpClientsDetails); err != nil { | ||
return | ||
} | ||
// When a release bundle already exists, the API returns 400. | ||
// Check the error message, and if it's a conflict, don't fail the operation. | ||
if resp.StatusCode == http.StatusBadRequest { | ||
response := ErrorResponseWithMessage{} | ||
if err = json.Unmarshal(body, &response); err != nil { | ||
return | ||
} | ||
if response.Errors[0].Message == conflictErrorMessage { | ||
log.Warn("Bundle already exists, did not upload a new bundle") | ||
return | ||
} | ||
} | ||
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusAccepted); err != nil { | ||
return | ||
} | ||
log.Info("Release Bundle Imported Successfully") | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package tests | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/artifactory" | ||
artifactoryAuth "github.com/jfrog/jfrog-client-go/artifactory/auth" | ||
"github.com/jfrog/jfrog-client-go/artifactory/services" | ||
"github.com/jfrog/jfrog-client-go/config" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestImportReleaseBundle(t *testing.T) { | ||
mockServer, rbService := createMockServer(t, func(w http.ResponseWriter, r *http.Request) { | ||
if r.RequestURI == "/"+services.ReleaseBundleImportRestApiEndpoint { | ||
w.WriteHeader(http.StatusBadRequest) | ||
_, err := w.Write([]byte(` | ||
{ | ||
"errors" : [ { | ||
"status" : 400, | ||
"message" : "Bundle already exists" | ||
} ] | ||
} | ||
`)) | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
defer mockServer.Close() | ||
err := rbService.ImportReleaseBundle("releasebundle_test.go") | ||
assert.NoError(t, err) | ||
} | ||
|
||
func createMockServer(t *testing.T, testHandler http.HandlerFunc) (*httptest.Server, artifactory.ArtifactoryServicesManager) { | ||
testServer := httptest.NewServer(testHandler) | ||
|
||
rtDetails := artifactoryAuth.NewArtifactoryDetails() | ||
rtDetails.SetUrl(testServer.URL + "/") | ||
|
||
serviceConfig, err := config.NewConfigBuilder(). | ||
SetServiceDetails(rtDetails). | ||
SetDryRun(false). | ||
Build() | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
artService, err := artifactory.New(serviceConfig) | ||
assert.NoError(t, err) | ||
return testServer, artService | ||
} |
Oops, something went wrong.