-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from AlexeiVainshtein/supoprtGoNewApi
Support Artifactory Go API
- Loading branch information
Showing
7 changed files
with
263 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package _go | ||
|
||
import ( | ||
"encoding/base64" | ||
"github.com/jfrog/jfrog-client-go/artifactory/services/utils" | ||
"github.com/jfrog/jfrog-client-go/utils/io/httputils" | ||
"strings" | ||
) | ||
|
||
func addHeaders(params GoParams, clientDetails *httputils.HttpClientDetails) { | ||
utils.AddHeader("X-GO-MODULE-VERSION", params.GetVersion(), &clientDetails.Headers) | ||
utils.AddHeader("X-GO-MODULE-CONTENT", base64.StdEncoding.EncodeToString(params.GetModContent()), &clientDetails.Headers) | ||
} | ||
|
||
func createUrlPath(moduleId, version, props, extension string, url *string) error { | ||
*url = strings.Join([]string{*url, moduleId, "@v", version + extension}, "/") | ||
properties, err := utils.ParseProperties(props, utils.JoinCommas) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*url = strings.Join([]string{*url, properties.ToEncodedString()}, ";") | ||
if strings.HasSuffix(*url, ";") { | ||
tempUrl := *url | ||
tempUrl = tempUrl[:len(tempUrl)-1] | ||
*url = tempUrl | ||
} | ||
return nil | ||
} |
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,27 @@ | ||
package _go | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/artifactory/auth" | ||
"github.com/jfrog/jfrog-client-go/httpclient" | ||
) | ||
|
||
var publishers []PublishGoPackage | ||
|
||
type PublishGoPackage interface { | ||
isCompatible(artifactoryVersion string) bool | ||
PublishPackage(params GoParams, client *httpclient.HttpClient, ArtDetails auth.ArtifactoryDetails) error | ||
} | ||
|
||
func register(publishApi PublishGoPackage) { | ||
publishers = append(publishers, publishApi) | ||
} | ||
|
||
// Returns the compatible publisher to Artifactory | ||
func GetCompatiblePublisher(artifactoryVersion string) PublishGoPackage { | ||
for _, publisher := range publishers { | ||
if publisher.isCompatible(artifactoryVersion) { | ||
return publisher | ||
} | ||
} | ||
return nil | ||
} |
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,65 @@ | ||
package _go | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/artifactory/auth" | ||
"github.com/jfrog/jfrog-client-go/artifactory/services/utils" | ||
"github.com/jfrog/jfrog-client-go/errors/httperrors" | ||
"github.com/jfrog/jfrog-client-go/httpclient" | ||
"github.com/jfrog/jfrog-client-go/utils/version" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
func init() { | ||
register(&publishWithoutApi{}) | ||
} | ||
|
||
// Support for Artifactory 6.6.0 and above API | ||
type publishWithoutApi struct { | ||
} | ||
|
||
func (pwa *publishWithoutApi) isCompatible(artifactoryVersion string) bool { | ||
propertiesApi := "6.6.0" | ||
if version.Compare(artifactoryVersion, propertiesApi) < 0 && artifactoryVersion != "development" { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (pwa *publishWithoutApi) PublishPackage(params GoParams, client *httpclient.HttpClient, ArtDetails auth.ArtifactoryDetails) error { | ||
url, err := utils.BuildArtifactoryUrl(ArtDetails.GetUrl(), params.GetTargetRepo(), make(map[string]string)) | ||
if err != nil { | ||
return err | ||
} | ||
zipUrl := url | ||
moduleId := strings.Split(params.GetModuleId(), ":") | ||
err = createUrlPath(moduleId[0], params.GetVersion(), params.GetProps(), ".zip", &zipUrl) | ||
if err != nil { | ||
return err | ||
} | ||
clientDetails := ArtDetails.CreateHttpClientDetails() | ||
|
||
addGoVersion(params, &zipUrl) | ||
resp, body, err := client.UploadFile(params.GetZipPath(), zipUrl, clientDetails, 0) | ||
if err != nil { | ||
return err | ||
} | ||
err = httperrors.CheckResponseStatus(resp, body, 201) | ||
if err != nil { | ||
return err | ||
} | ||
err = createUrlPath(moduleId[0], params.GetVersion(), params.GetProps(), ".mod", &url) | ||
if err != nil { | ||
return err | ||
} | ||
addGoVersion(params, &url) | ||
resp, body, err = client.UploadFile(params.GetModPath(), url, clientDetails, 0) | ||
if err != nil { | ||
return err | ||
} | ||
return httperrors.CheckResponseStatus(resp, body, 201) | ||
} | ||
|
||
func addGoVersion(params GoParams, urlPath *string) { | ||
*urlPath += ";go.version=" + url.QueryEscape(params.GetVersion()) | ||
} |
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,49 @@ | ||
package _go | ||
|
||
import ( | ||
"github.com/jfrog/jfrog-client-go/artifactory/auth" | ||
"github.com/jfrog/jfrog-client-go/artifactory/services/utils" | ||
"github.com/jfrog/jfrog-client-go/errors/httperrors" | ||
"github.com/jfrog/jfrog-client-go/httpclient" | ||
"github.com/jfrog/jfrog-client-go/utils/version" | ||
) | ||
|
||
func init() { | ||
register(&publishWithHeader{}) | ||
} | ||
|
||
// Support for Artifactory older then 6.5.0 API | ||
type publishWithHeader struct { | ||
} | ||
|
||
func (pwh *publishWithHeader) isCompatible(artifactoryVersion string) bool { | ||
propertiesApi := "6.5.0" | ||
if version.Compare(artifactoryVersion, propertiesApi) < 0 { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (pwh *publishWithHeader) PublishPackage(params GoParams, client *httpclient.HttpClient, ArtDetails auth.ArtifactoryDetails) error { | ||
url, err := utils.BuildArtifactoryUrl(ArtDetails.GetUrl(), "api/go/"+params.GetTargetRepo(), make(map[string]string)) | ||
clientDetails := ArtDetails.CreateHttpClientDetails() | ||
addHeaders(params, &clientDetails) | ||
addPropertiesHeaders(params.GetProps(), &clientDetails.Headers) | ||
resp, body, err := client.UploadFile(params.GetZipPath(), url, clientDetails, 0) | ||
if err != nil { | ||
return err | ||
} | ||
return httperrors.CheckResponseStatus(resp, body, 201) | ||
} | ||
|
||
func addPropertiesHeaders(props string, headers *map[string]string) error { | ||
properties, err := utils.ParseProperties(props, utils.JoinCommas) | ||
if err != nil { | ||
return err | ||
} | ||
headersMap := properties.ToHeadersMap() | ||
for k, v := range headersMap { | ||
utils.AddHeader("X-ARTIFACTORY-PROPERTY-"+k, v, headers) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.