jfrog-client-go
Branch |
Status |
master |
 |
dev |
 |
Table of Contents
General
jfrog-client-go is a library which provides Go APIs to performs actions on JFrog Artifactory or Bintray from your Go application.
The project is still relatively new, and its APIs may therefore change frequently between releases.
The library can be used as a go-module, which should be added to your project's go.mod file. As a reference you may look at JFrog CLI's go.mod file, which uses this library as a dependency.
Pull Requests
We welcome pull requests from the community.
Guidelines
- If the existing tests do not already cover your changes, please add tests.
- Pull requests should be created on the dev branch.
- Please use gofmt for formatting the code before submitting the pull request.
Tests
To run tests on the source code, you'll need a running JFrog Artifactory Pro instance.
Use the following command with the below options to run the tests.
go test -v github.com/jfrog/jfrog-client-go/tests
Optional flags:
Flag |
Description |
-rt.url |
[Default: http://localhost:8081/artifactory] Artifactory URL. |
-rt.user |
[Default: admin] Artifactory username. |
-rt.password |
[Default: password] Artifactory password. |
-ds.url |
[Optional] JFrog Distribution URL. |
-xr.url |
[Optional] JFrog Xray URL. |
-rt.apikey |
[Optional] Artifactory API key. |
-rt.sshKeyPath |
[Optional] Ssh key file path. Should be used only if the Artifactory URL format is ssh://[domain]:port |
-rt.sshPassphrase |
[Optional] Ssh key passphrase. |
-rt.accessToken |
[Optional] Artifactory access token. |
-log-level |
[Default: INFO] Sets the log level. |
- The tests create an Artifactory repository named jfrog-client-tests-repo1.
Once the tests are completed, the content of this repository is deleted.
General APIs
Setting the Logger
var file *os.File
...
log.SetLogger(log.NewLogger(log.INFO, file))
Setting the Temp Dir
The default temp dir used is 'os.TempDir()'. Use the following API to set a new temp dir:
fileutils.SetTempDirBase(filepath.Join("my", "temp", "path"))
Artifactory APIs
Creating Artifactory Service Manager
Creating Artifactory Details
rtDetails := auth.NewArtifactoryDetails()
rtDetails.SetUrl("http://localhost:8081/artifactory")
rtDetails.SetSshKeysPath("path/to/.ssh/")
rtDetails.SetApiKey("apikey")
rtDetails.SetUser("user")
rtDetails.SetPassword("password")
rtDetails.SetAccessToken("accesstoken")
// if client certificates are required
rtDetails.SetClientCertPath("path/to/.cer")
rtDetails.SetClientCertKeyPath("path/to/.key")
Creating Artifactory Service Config
serviceConfig, err := config.NewConfigBuilder().
SetServiceDetails(rtDetails).
SetCertificatesPath(certPath).
SetThreads(threads).
SetDryRun(false).
// Add [Context](https://golang.org/pkg/context/)
SetContext(ctx).
Build()
Creating New Artifactory Service Manager
rtManager, err := artifactory.New(&rtDetails, serviceConfig)
Using Artifactory Services
Uploading Files to Artifactory
Using the UploadFiles()
function, we can upload files and get the general statistics of the action (The actual number of successful and failed uploads), and the error value if it occurred.
params := services.NewUploadParams()
params.Pattern = "repo/*/*.zip"
params.Target = "repo/path/"
// Attach properties to the uploaded files.
params.TargetProps = "key1=val1;key2=val2"
params.AddVcsProps = false
params.BuildProps = "build.name=buildName;build.number=17;build.timestamp=1600856623553"
params.Recursive = true
params.Regexp = false
params.IncludeDirs = false
params.Flat = true
params.Explode = false
params.Archive = "zip"
params.Deb = ""
params.Symlink = false
// Retries default value: 3
params.Retries = 5
// MinChecksumDeploy default value: 10400
params.MinChecksumDeploy = 15360
totalUploaded, totalFailed, err := rtManager.UploadFiles(params)
Downloading Files from Artifactory
Using the DownloadFiles()
function, we can download files and get the general statistics of the action (The actual number of files downloaded, and the number of files we expected to download), and the error value if it occurred.
params := services.NewDownloadParams()
params.Pattern = "repo/*/*.zip"
params.Target = "target/path/"
// Filter the downloaded files by properties.
params.Props = "key1=val1;key2=val2"
params.Recursive = true
params.IncludeDirs = false
params.Flat = false
params.Explode = false
params.Symlink = true
params.ValidateSymlink = false
// Retries default value: 3
params.Retries = 5
// SplitCount default value: 3
params.SplitCount = 2
// MinSplitSize default value: 5120
params.MinSplitSize = 7168
totalDownloaded, totalExpected, err := rtManager.DownloadFiles(params)
Uploading and Downloading Files with Summary
The methods UploadFilesWithSummary()
and DownloadFilesWithSummary()
are similar to UploadFlies()
and DownloadFlies()
, but return an OperationSummary struct, which allows iterating over the details of the uploaded/downloaded files.
The OperationSummary struct contains:
- TotalSucceeded - the number of successful uploads/downloads
- TotalFailed - the number of failed uploads/downloads
- TransferDetailsReader - a ContentReader of FileTransferDetails structs, with a struct for each successful transfer of file
- ArtifactsDetailsReader - a ContentReader of ArtifactDetails structs, with a struct for each artifact in Artifactory that was uploaded/downloaded successfully
The ContentReaders can be closed separately by calling Close()
on each of them, or they both can be closed at once by calling Close()
on the OperationSummary struct.
params := services.NewUploadParams()
params.Pattern = "repo/*/*.zip"
params.Target = "repo/path/"
summary, err := rtManager.UploadFilesWithSummary(params)
defer summary.Close()
Read more about ContentReader.
Copying Files in Artifactory
params := services.NewMoveCopyParams()
params.Pattern = "repo/*/*.zip"
params.Target = "target/path/"
// Filter the files by properties.
params.Props = "key1=val1;key2=val2"
params.Recursive = true
params.Flat = false
rtManager.Copy(params)
Moving Files in Artifactory
params := services.NewMoveCopyParams()
params.Pattern = "repo/*/*.zip"
params.Target = "target/path/"
// Filter the files by properties.
params.Props = "key1=val1;key2=val2"
params.Recursive = true
params.Flat = false
rtManager.Move(params)
Deleting Files from Artifactory
params := services.NewDeleteParams()
params.Pattern = "repo/*/*.zip"
// Filter the files by properties.
params.Props = "key1=val1;key2=val2"
params.Recursive = true
pathsToDelete, err := rtManager.GetPathsToDelete(params)
if err != nil {
return err
}
defer pathsToDelete.Close()
rtManager.DeleteFiles(pathsToDelete)
Read more about ContentReader.
Searching Files in Artifactory
params := services.NewSearchParams()
params.Pattern = "repo/*/*.zip"
// Filter the files by properties.
params.Props = "key1=val1;key2=val2"
params.Recursive = true
reader, err := rtManager.SearchFiles(params)
if err != nil {
return err
}
defer reader.Close()
Read more about ContentReader.
Setting Properties on Files in Artifactory
searchParams = services.NewSearchParams()
searchParams.Recursive = true
searchParams.IncludeDirs = false
reader, err = rtManager.SearchFiles(searchParams)
if err != nil {
return err
}
defer reader.Close()
propsParams = services.NewPropsParams()
propsParams.Pattern = "repo/*/*.zip"
propsParams.Reader = reader
// Filter the files by properties.
propsParams.Props = "key=value"
rtManager.SetProps(propsParams)
Read more about ContentReader.
Deleting Properties from Files in Artifactory
searchParams = services.NewSearchParams()
searchParams.Recursive = true
searchParams.IncludeDirs = false
resultItems, err = rtManager.SearchFiles(searchParams)
if err != nil {
return err
}
defer reader.Close()
propsParams = services.NewPropsParams()
propsParams.Pattern = "repo/*/*.zip"
propsParams.Reader = reader
// Filter the files by properties.
propsParams.Props = "key=value"
rtManager.DeleteProps(propsParams)
Read more about ContentReader.
Publishing Build Info to Artifactory
buildInfo := &buildinfo.BuildInfo{}
// Optional Artifactory project key
projectKey := "my-project-key"
...
rtManager.PublishBuildInfo(buildInfo, projectKey)
Fetching Build Info from Artifactory
buildInfoParams := services.NewBuildInfoParams{}
buildInfoParams.BuildName = "buildName"
buildInfoParams.BuildNumber = "LATEST"
// Optional Artifactory project key
buildInfoParams.ProjectKey = "my-project-key"
rtManager.GetBuildInfo(buildInfoParams)
params := services.NewPromotionParams()
params.BuildName = "buildName"
params.BuildNumber = "10"
params.TargetRepo = "target-repo"
params.Status = "status"
params.Comment = "comment"
params.Copy = true
params.IncludeDependencies = false
params.SourceRepo = "source-repo"
// Optional Artifactory project key
params.ProjectKey = "my-project-key"
rtManager.PromoteBuild(params)
sourceDockerImage := "hello-world"
sourceRepo := "docker-local-1"
targetRepo := "docker-local-2"
params := services.NewDockerPromoteParams(sourceDockerImage, sourceRepo, targetRepo)
// Optional parameters:
params.TargetDockerImage = "target-docker-image"
params.SourceTag = "42"
params.TargetTag = "43"
params.Copy = true
rtManager.PromoteDocker(params)
Distributing Published Builds to JFrog Bintray
params := services.NewBuildDistributionParams()
params.SourceRepos = "source-repo"
params.TargetRepo = "target-repo"
params.GpgPassphrase = "GpgPassphrase"
params.Publish = false
params.OverrideExistingFiles = false
params.Async = true
params.BuildName = "buildName"
params.BuildNumber = "10"
params.Pattern = "repo/*/*.zip"
rtManager.DistributeBuild(params)
Triggering Build Scanning with JFrog Xray
params := services.NewXrayScanParams()
params.BuildName = buildName
params.BuildNumber = buildNumber
rtManager.XrayScanBuild(params)
Discarding Old Builds
params := services.NewDiscardBuildsParams()
params.BuildName = "buildName"
params.MaxDays = "max-days"
params.MaxBuilds = "max-builds"
params.ExcludeBuilds = "1,2"
params.DeleteArtifacts = false
params.Async = false
// Optional Artifactory project key
projectKey := "my-project-key"
rtManager.DiscardBuilds(params)
Cleaning Unreferenced Git LFS Files from Artifactory
params := services.NewGitLfsCleanParams()
params.Refs = "refs/remotes/*"
params.Repo = "my-project-lfs"
params.GitPath = "path/to/git"
reader,err := rtManager.GetUnreferencedGitLfsFiles(params)
defer reader.Close()
rtManager.DeleteFiles(reader)
Executing AQLs
rtManager.Aql(aql string)
Reading Files in Artifactory
rtManager.ReadRemoteFile(FilePath string)
Creating an Access Token
params := services.NewCreateTokenParams()
params.Scope = "api:* member-of-groups:readers"
params.Username = "user"
params.ExpiresIn = 3600 // default -1 (use server default)
params.GrantType = "client_credentials"
params.Refreshable = true
params.Audience = "jfrt@<serviceID1> jfrt@<serviceID2>"
results, err := rtManager.CreateToken(params)
Fetching Access Tokens
results, err := rtManager.GetTokens()
Refreshing an Access Token
params := services.NewRefreshTokenParams()
params.AccessToken = "<access token>"
params.RefreshToken = "<refresh token>"
params.Token.Scope = "api:*"
params.Token.ExpiresIn = 3600
results, err := rtManager.RefreshToken(params)
Revoking an Access Token
params := services.NewRevokeTokenParams()
// Provide either TokenId or Token
params.TokenId = "<token id>"
// params.Token = "access token"
err := rtManager.RevokeToken(params)
Regenerate API Key
apiKey, err := rtManager.RegenerateAPIKey()
Creating and Updating Local Repository
You can create and update a local repository for the following package types:
Maven, Gradle, Ivy, Sbt, Helm, Cocoapods, Opkg, Rpm, Nuget, Cran, Gems, Npm, Bower, Debian, Composer, Pypi, Docker,
Vagrant, Gitlfs, Go, Yum, Conan, Chef, Puppet and Generic.
Each package type has it's own parameters struct, can be created using the method
New<packageType>LocalRepositoryParams()
.
Example for creating local Generic repository:
params := services.NewGenericLocalRepositoryParams()
params.Key = "generic-repo"
params.Description = "This is a public description for generic-repo"
params.Notes = "These are internal notes for generic-repo"
params.RepoLayoutRef = "simple-default"
params.ArchiveBrowsingEnabled = true
params.XrayIndex = true
params.IncludesPattern = "**/*"
params.ExcludesPattern = "excludedDir/*"
params.DownloadRedirect = true
err = servicesManager.CreateLocalRepository().Generic(params)
Updating local Generic repository:
err = servicesManager.UpdateLocalRepository().Generic(params)
Creating and Updating Remote Repository
You can create and update a remote repository for the following package types:
Maven, Gradle, Ivy, Sbt, Helm, Cocoapods, Opkg, Rpm, Nuget, Cran, Gems, Npm, Bower, Debian, Composer, Pypi, Docker,
Gitlfs, Go, Yum, Conan, Chef, Puppet, Conda, P2, Vcs and Generic.
Each package type has it's own parameters struct, can be created using the method
New<packageType>RemoteRepositoryParams()
.
Example for creating remote Maven repository:
params := services.NewMavenRemoteRepositoryParams()
params.Key = "jcenter-remote"
params.Url = "http://jcenter.bintray.com"
params.RepoLayoutRef = "maven-2-default"
params.Description = "A caching proxy repository for a JFrog's jcenter"
params.HandleSnapshot = false
params.HandleReleases = true
params.FetchJarsEagerly = true
params.XrayIndex = true
params.AssumedOfflinePeriodSecs = 600
params.SuppressPomConsistencyChecks = true
params.RemoteRepoChecksumPolicyType = "pass-thru"
err = servicesManager.CreateRemoteRepository().Maven(params)
Updating remote Maven repository:
err = servicesManager.UpdateRemoteRepository().Maven(params)
Creating and Updating Virtual Repository
You can create and update a virtual repository for the following package types:
Maven, Gradle, Ivy, Sbt, Helm, Rpm, Nuget, Cran, Gems, Npm, Bower, Debian, Pypi, Docker, Gitlfs, Go, Yum, Conan,
Chef, Puppet, Conda, P2 and Generic
Each package type has it's own parameters struct, can be created using the method
New<packageType>VirtualRepositoryParams()
.
Example for creating virtual Go repository:
params := services.NewGoVirtualRepositoryParams()
params.Description = "This is an aggregated repository for several go repositories"
params.RepoLayoutRef = "go-default"
params.Repositories = {"gocenter-remote", "go-local"}
params.DefaultDeploymentRepo = "go-local"
params.ExternalDependenciesEnabled = true
params.ExternalDependenciesPatterns = {"**/github.com/**", "**/golang.org/**", "**/gopkg.in/**"}
params.ArtifactoryRequestsCanRetrieveRemoteArtifacts = true
err = servicesManager.CreateVirtualRepository().Go(params)
Updating remote Maven repository:
err = servicesManager.UpdateVirtualRepository().Go(params)
Removing a Repository
You can remove a repository from Artifactory using its key:
servicesManager.DeleteRepository("generic-repo")
Getting Repository Details
You can get repository details from Artifactory using its key:
servicesManager.GetRepository("generic-repo")
Getting All Repositories
You can get all repositories from Artifactory:
servicesManager.GetAllRepositories()
Creating and Updating Repository Replications
Example of creating a repository replication:
params := services.NewCreateReplicationParams()
// Source replication repository.
params.RepoKey = "my-repository"
params.CronExp = "0 0 12 * * ?"
params.Username = "admin"
params.Password = "password"
params.Url = "http://localhost:8081/artifactory/remote-repo"
params.Enabled = true
params.SocketTimeoutMillis = 15000
params.EnableEventReplication = true
params.SyncDeletes = true
params.SyncProperties = true
params.SyncStatistics = true
params.PathPrefix = "/path/to/repo"
err = servicesManager.CreateReplication(params)
Example of updating a local repository replication:
params := services.NewUpdateReplicationParams()
// Source replication repository.
params.RepoKey = "my-repository"
params.CronExp = "0 0 12 * * ?"
params.Enabled = true
params.SocketTimeoutMillis = 15000
params.EnableEventReplication = true
params.SyncDeletes = true
params.SyncProperties = true
params.SyncStatistics = true
params.PathPrefix = "/path/to/repo"
err = servicesManager.UpdateReplication(params)
Getting a Repository Replication
You can get a repository replication configuration from Artifactory using its key:
replicationConfiguration, err := servicesManager.GetReplication("my-repository")
Removing a Repository Replication
You can remove a repository replication configuration from Artifactory using its key:
err := servicesManager.DeleteReplication("my-repository")
Creating and Updating Permission Targets
You can create or update a permission target in Artifactory.
Permissions are set according to the following conventions:
read, write, annotate, delete, manage, managedXrayMeta, distribute
For repositories You can specify the name "ANY"
in order to apply to all repositories, "ANY REMOTE"
for all remote repositories or "ANY LOCAL"
for all local repositories.
Creating a new permission target :
params := services.NewPermissionTargetParams()
params.Name = "java-developers"
params.Repo.Repositories = []string{"ANY REMOTE", "local-repo1", "local-repo2"}
params.Repo.ExcludePatterns = []string{"dir/*"}
params.Repo.Actions.Users = map[string][]string {
"user1" : {"read", "write"},
"user2" : {"write","annotate", "read"},
}
params.Repo.Actions.Groups = map[string][]string {
"group1" : {"manage","read","annotate"},
}
// This is the default value that cannot be changed
params.Build.Repositories = []string{"artifactory-build-info"}
params.Build.Actions.Groups = map[string][]string {
"group1" : {"manage","read","write","annotate","delete"},
"group2" : {"read"},
}
err = servicesManager.CreatePermissionTarget(params)
Updating an existing permission target :
err = servicesManager.UpdatePermissionTarget(params)
Removing a Permission Target
You can remove a permission target from Artifactory using its name:
err = servicesManager.DeletePermissionTarget("java-developers")
Fetching a Permission Target
You can fetch a permission target from Artifactory using its name:
permissionTargetParams, err = servicesManager.GetPermissionTarget("java-developers")
Fetching Artifactory's Version
version, err := servicesManager.GetVersion()
Fetching Artifactory's Service ID
serviceId, err := servicesManager.GetServiceId()
Fetching Users Details
params := services.NewUserParams()
params.UserDetails.Name = "myUserName"
user, err := serviceManager.GetUser(params)
Fetching All Users Details
You can get all users from Artifactory:
users, err := servicesManager.GetAllUsers()
Creating and Updating a User
params := services.NewUserParams()
params.UserDetails.Name = "myUserName"
params.UserDetails.Email = "[email protected]"
params.UserDetails.Password = "Password1"
params.UserDetails.Admin = false
params.UserDetails.Realm= "internal"
params.UserDetails.ProfileUpdatable = true
params.UserDetails.DisableUIAccess = false
params.UserDetails.InternalPasswordDisabled = false
params.UserDetails.groups = [2]string{"GroupA", "GroupB"}
// Set to true in order to replace exist user with the same name
params.ReplaceIfExists = false
err := serviceManager.CreateUser(params)
params.UserDetails.groups = [3]string{"GroupA", "GroupB", "GroupC"}
err := serviceManager.UpdateUser(params)
Deleting a User
err := serviceManager.DeleteUser("myUserName")
Fetching Group Details
params := services.NewGroupParams()
params.GroupDetails.Name = "myGroupName"
// Set this param to true to receive the user names associated with this group
params.IncludeUsers = true
group, err := serviceManager.GetGroup(params)
Creating and Updating a Group
params := services.NewGroupParams()
params.GroupDetails.Name = "myGroupName"
params.GroupDetails.Description = "Description"
params.GroupDetails.AutoJoin = false
params.GroupDetails.AdminPrivileges = true
params.GroupDetails.Realm = "internal"
params.GroupDetails.UsersNames = [2]string{"UserA", "UserB"}
// Set to true in order to replace exist group with the same name
params.ReplaceIfExists = false
err := serviceManager.CreateGroup(params)
params.GroupDetails.Description = "Newer Description"
// Will add UserC to the group (in addition to existing UserA and UserB)
params.GroupDetails.UsersNames = [1]string{"UserC"}
err := serviceManager.UpdateGroup(params)
Deleting a Group
err := serviceManager.DeleteGroup("myGroupName")
Distribution APIs
Creating Distribution Service Manager
Creating Distribution Details
distDetails := auth.NewDistributionDetails()
distDetails.SetUrl("http://localhost:8081/distribution")
distDetails.SetSshKeysPath("path/to/.ssh/")
distDetails.SetApiKey("apikey")
distDetails.SetUser("user")
distDetails.SetPassword("password")
distDetails.SetAccessToken("accesstoken")
// if client certificates are required
distDetails.SetClientCertPath("path/to/.cer")
distDetails.SetClientCertKeyPath("path/to/.key")
Creating Distribution Service Config
serviceConfig, err := config.NewConfigBuilder().
SetServiceDetails(rtDetails).
SetCertificatesPath(certPath).
SetThreads(threads).
SetDryRun(false).
// Add [Context](https://golang.org/pkg/context/)
SetContext(ctx).
Build()
Creating New Distribution Service Manager
distManager, err := distribution.New(&distDetails, serviceConfig)
Using Distribution Services
Setting Distribution Signing Key
params := services.NewSetSigningKeyParams("private-gpg-key", "public-gpg-key")
err := distManager.SetSigningKey(params)
Creating a Release Bundle
params := services.NewCreateReleaseBundleParams("bundle-name", "1")
params.SpecFiles = []*utils.ArtifactoryCommonParams{{Pattern: "repo/*/*.zip"}}
params.Description = "Description"
params.ReleaseNotes = "Release notes"
params.ReleaseNotesSyntax = "plain_text"
params.TargetProps = "key1=val1;key2=val2,val3"
// Be default, artifacts that are distributed as part of a release bundle, have the same path in their destination server
// (the edge node) as the path they had on the distributing Artifactory server.
// You have however the option for modifying the target path on edge node. You do this by defining the Target property as shown below.
// The Pattern property is a wildcard based pattern. Any wildcards enclosed in parenthesis in the pattern (source)
// path can be matched with a corresponding placeholder in the target path, to determine the path and name
// of the artifact, once distributed to the edge node.
// In the following example, the path in the edge node is similar to the path in the source Artifactory server, except for the additional "dir" level at the root of the repository.
// Pattern: my-repo/(*)/a.zip
// Target: my-repo/dir/{1}/a.zip
pathMappingSpec := &utils.ArtifactoryCommonParams{Pattern: "source-repo/(a)/(*.zip)", Target: "target-repo/{1}-{2}"}
params.SpecFiles = append(params.SpecFiles, pathMappingSpec)
err := distManager.CreateReleaseBundle(params)
Updating a Release Bundle
params := services.NewUpdateReleaseBundleParams("bundle-name", "1")
params.SpecFiles = []*utils.ArtifactoryCommonParams{{Pattern: "repo/*/*.zip"}}
params.Description = "New Description"
params.ReleaseNotes = "New Release notes"
params.ReleaseNotesSyntax = "plain_text"
params.TargetProps = "key1=val1;key2=val2,val3"
// The Target property defines the target path in the edge node, and can include replaceable in the form of {1}, {2}, ...
// Read more about it in the above "Creating a Release Bundle" section.
pathMappingSpec := &utils.ArtifactoryCommonParams{Pattern: "source-repo/(a)/(*.zip)", Target: "target-repo/{1}-{2}"}
params.SpecFiles = append(params.SpecFiles, pathMappingSpec)
err := distManager.CreateReleaseBundle(params)
Signing a Release Bundle
params := services.NewSignBundleParams("bundle-name", "1")
params.GpgPassphrase = "123456"
err := distManager.SignReleaseBundle(params)
Async Distributing a Release Bundle
params := services.NewDistributeReleaseBundleParams("bundle-name", "1")
distributionRules := utils.DistributionCommonParams{SiteName: "Swamp-1", "CityName": "Tel-Aviv", "CountryCodes": []string{"123"}}}
params.DistributionRules = []*utils.DistributionCommonParams{distributionRules}
err := distManager.DistributeReleaseBundle(params)
Sync Distributing a Release Bundle
params := services.NewDistributeReleaseBundleParams("bundle-name", "1")
distributionRules := utils.DistributionCommonParams{SiteName: "Swamp-1", "CityName": "Tel-Aviv", "CountryCodes": []string{"123"}}}
params.DistributionRules = []*utils.DistributionCommonParams{distributionRules}
// Wait up to 120 minutes for the release bundle distribution
err := distManager.DistributeReleaseBundleSync(params, 120)
Getting Distribution Status
params := services.NewDistributionStatusParams()
// Optional parameters:
// If missing, get status for all distributions
params.Name = "bundle-name"
// If missing, get status for all versions of "bundle-name"
params.Version = "1"
// If missing, get status for all "bundle-name" with version "1"
params.TrackerId = "123456789"
status, err := distributeBundleService.GetStatus(params)
Deleting a Remote Release Bundle
params := services.NewDeleteReleaseBundleParams("bundle-name", "1")
params.DeleteFromDistribution = true
distributionRules := utils.DistributionCommonParams{SiteName: "Swamp-1", "CityName": "Tel-Aviv", "CountryCodes": []string{"123"}}}
params.DistributionRules = []*utils.DistributionCommonParams{distributionRules}
err := distManager.DeleteReleaseBundle(params)
Deleting a Local Release Bundle
params := services.NewDeleteReleaseBundleParams("bundle-name", "1")
err := distManager.DeleteLocalReleaseBundle(params)
Bintray APIs
Creating Bintray Details
btDetails := auth.NewBintrayDetails()
btDetails.SetUser("user")
btDetails.SetKey("key")
btDetails.SetDefPackageLicense("Apache 2.0")
Creating Bintray Service Manager
serviceConfig := bintray.NewConfigBuilder().
SetBintrayDetails(btDetails).
SetDryRun(false).
SetThreads(threads).
Build()
btManager, err := bintray.New(serviceConfig)
Using Bintray Services
Uploading a Single File to Bintray
params := services.NewUploadParams()
params.Pattern = "*/*.zip"
params.Path = versions.CreatePath("subject/repo/pkg/version")
params.TargetPath = "path/to/files"
params.Deb = "distribution/component/architecture"
params.Recursive = true
params.Flat = true
params.Publish = false
params.Override = false
params.Explode = false
params.UseRegExp = false
params.ShowInDownloadList = false
btManager.UploadFiles(params)
Downloading a Single File from Bintray
params := services.NewDownloadFileParams()
params.Flat = false
params.IncludeUnpublished = false
params.PathDetails = "path/to/file"
params.TargetPath = "target/path/"
// SplitCount default value: 3
params.SplitCount = 2
// MinSplitSize default value: 5120
params.MinSplitSize = 7168
btManager.DownloadFile(params)
Downloading Version Files from Bintray
params := services.NewDownloadVersionParams()
params.Path, err = versions.CreatePath("subject/repo/pkg/version")
params.IncludeUnpublished = false
params.TargetPath = "target/path/"
btManager.DownloadVersion(params)
Showing and Deleting a Bintray Package
pkgPath, err := packages.CreatePath("subject/repo/pkg")
btManager.ShowPackage(pkgPath)
btManager.DeletePackage(pkgPath)
Creating and Updating a Bintray Package
params := packages.NewPackageParams()
params.Path, err = packages.CreatePath("subject/repo/pkg")
params.Desc = "description"
params.Labels = "labels"
params.Licenses = "licenses"
params.CustomLicenses = "custom-licenses"
params.VcsUrl = "https://github.com/jfrog/jfrog-cli-go"
params.WebsiteUrl = "https://jfrog.com"
params.IssueTrackerUrl = "https://github.com/bintray/bintray-client-java/issues"
params.GithubRepo = "bintray/bintray-client-java"
params.GithubReleaseNotesFile = "RELEASE_1.2.3.txt" "github-rel-notes"
params.PublicDownloadNumbers = "true"
params.PublicStats = "true"
btManager.CreatePackage(params)
btManager.UpdatePackage(params)
Showing and Deleting a Bintray Version
versionPath, err := versions.CreatePath("subject/repo/pkg/version")
btManager.ShowVersion(versionPath)
btManager.DeleteVersion(versionPath)
Creating and Updating a Bintray Version
params := versions.NewVersionParams()
params.Path, err = versions.CreatePath("subject/repo/pkg/version")
params.Desc = "description"
params.VcsTag = "1.1.5"
params.Released = "true"
params.GithubReleaseNotesFile = "RELEASE_1.2.3.txt"
params.GithubUseTagReleaseNotes = "false"
btManager.CreateVersion(params)
btManager.UpdateVersion(params)
Creating and Updating Entitlements
params := entitlements.NewEntitlementsParams()
params.VersionPath, err = versions.CreatePath("subject/repo/pkg/version")
params.Path = "a/b/c"
params.Access = "rw"
params.Keys = "keys"
btManager.CreateEntitlement(params)
params.Id = "entitlementID"
btManager.UpdateEntitlement(params)
Showing and Deleting Entitlements
versionPath, err := versions.CreatePath("subject/repo/pkg/version")
btManager.ShowAllEntitlements(versionPath)
btManager.ShowEntitlement("entitelmentID", versionPath)
btManager.DeleteEntitlement("entitelmentID", versionPath)
Creating and Updating Access Keys
params := accesskeys.NewAccessKeysParams()
params.Password = "password"
params.Org = "org"
params.Expiry = time.Now() + time.Hour * 10
params.ExistenceCheckUrl = "http://callbacks.myci.org/username=:username,password=:password"
params.ExistenceCheckCache = 60
params.WhiteCidrs = "127.0.0.1/22,193.5.0.1/92"
params.BlackCidrs = "127.0.0.1/22,193.5.0.1/92"
params.ApiOnly = true
btManager.CreateAccessKey(params)
params.Id = "KeyID"
btManager.UpdateAccessKey(params)
Showing and Deleting Access Keys
btManager.ShowAllAccessKeys("org")
btManager.ShowAccessKey("org", "KeyID")
btManager.DeleteAccessKey("org", "KeyID")
Signing a URL
params := url.NewURLParams()
params.PathDetails, err = utils.CreatePathDetails("subject/repository/file-path")
// Check for errors
params.Expiry = time.Now() + time.Hour * 10
params.ValidFor = 60
params.CallbackId = "callback-id"
params.CallbackEmail = "callback-email"
params.CallbackUrl = "callback-url"
params.CallbackMethod = "callback-method"
btManager.SignUrl(params)
GPG Signing a File
path, err := utils.CreatePathDetails("subject/repository/file-path")
btManager.GpgSignFile(path, "passphrase")
GPG Signing Version Files
path, err := versions.CreatePath("subject/repo/pkg/version")
btManager.GpgSignVersion(path, "passphrase")
Listing Logs
path, err := versions.CreatePath("subject/repo/pkg/version")
btManager.LogsList(versionPath)
Downloading Logs
path, err := versions.CreatePath("subject/repo/pkg/version")
btManager.DownloadLog(path, "logName")
Syncing Content To Maven Central
params := mavensync.NewParams("user","password", false)
path, err = versions.CreatePath("subject/repo/pkg/version")
btManager.MavenCentralContentSync(params, path)
Using ContentReader
Some APIs return a content.ContentReader
struct, which allows reading the API's output. content.ContentReader
provides access to large amounts of data safely, without loading all of it into the memory.
Here's an example for how content.ContentReader
should be used:
reader, err := servicesManager.SearchFiles(searchParams)
if err != nil {
return err
}
// Remove the data file used by the reader.
defer func() {
if reader != nil {
err = reader.Close()
}
}()
// Iterate over the results.
for currentResult := new(ResultItem); reader.NextRecord(currentResult) == nil; currentResult = new(ResultItem) {
fmt.Printf("Found artifact: %s of type: %s\n", searchResult.Name, searchResult.Type)
}
if err := resultReader.GetError(); err != nil {
return err
}
// Resets the reader pointer back to the beginning of the output. Make sure not to call this method after the reader had been closed using ```reader.Close()```
reader.Reset()
-
reader.NextRecord(currentResult)
reads the next record from the reader into currentResult
of type ResultItem
.
-
reader.Close()
removes the file used by the reader after it is used (preferably using defer
).
-
reader.GetError()
returns any error that might have occurd during NextRecord()
.
-
reader.Reset()
resets the reader back to the beginning of the output.
Xray APIs
Creating Xray Service Manager
Creating Xray Details
xrayDetails := auth.NewXrayDetails()
xrayDetails.SetUrl("http://localhost:8081/xray")
xrayDetails.SetSshKeysPath("path/to/.ssh/")
xrayDetails.SetApiKey("apikey")
xrayDetails.SetUser("user")
xrayDetails.SetPassword("password")
xrayDetails.SetAccessToken("accesstoken")
// if client certificates are required
xrayDetails.SetClientCertPath("path/to/.cer")
xrayDetails.SetClientCertKeyPath("path/to/.key")
Creating Xray Service Config
serviceConfig, err := config.NewConfigBuilder().
SetServiceDetails(xrayDetails).
SetCertificatesPath(certPath).
Build()
Creating New Xray Service Manager
xrayManager, err := xray.New(&xrayDetails, serviceConfig)
Using Xray Services
Fetching Xray's Version
version, err := xrayManager.GetVersion()
Creating an Xray Watch
This uses API version 2.
You are able to configure repositories and builds on a watch.
However, bundles are not supported.
params := utils.NewWatchParams()
params.Name = "example-watch-all"
params.Description = "All Repos"
params.Active = true
params.Repositories.Type = utils.WatchRepositoriesAll
params.Repositories.All.Filters.PackageTypes = []string{"Npm", "maven"}
params.Repositories.ExcludePatterns = []string{"excludePath1", "excludePath2"}
params.Repositories.IncludePatterns = []string{"includePath1", "includePath2"}
params.Builds.Type = utils.WatchBuildAll
params.Builds.All.Bin_Mgr_ID = "default"
params.Policies = []utils.AssignedPolicy{
{
Name: policy1Name,
Type: "security",
},
{
Name: policy2Name,
Type: "security",
},
}
err := xrayManager.CreateWatch(*params)
Get an Xray Watch
watch, err := xrayManager.GetWatch("example-watch-all")
Update an Xray Watch
watch, err := xrayManager.GetWatch("example-watch-all")
watch.Description = "Updated description"
err := xrayManager.UpdateWatch(*watch)
Delete an Xray Watch
err := xrayManager.DeleteWatch("example-watch-all")
Creating a Security Xray Policy
params := utils.NewPolicyParams()
params.Name = "example-security-policy"
params.Type = utils.Security
params.Description = "Security policy with 2 rules"
params.Rules = []utils.PolicyRule{
{
Name: "min-severity-rule",
Criteria: *utils.CreateSeverityPolicyCriteria(utils.Low),
Priority: 1,
},
{
Name: "cvss-range-rule",
Criteria: *utils.CreateCvssRangePolicyCriteria(5.7, 8.9),
Priority: 2,
Actions: &utils.PolicyAction{
Webhooks: []string{"sec_webhook"},
BlockDownload: utils.PolicyBlockDownload{
Active: true,
Unscanned: false,
},
BlockReleaseBundleDistribution: false,
FailBuild: true,
NotifyDeployer: false,
NotifyWatchRecipients: true,
CustomSeverity: utils.Medium,
},
},
}
err := xrayManager.CreatePolicy(params)
Creating a License Xray Policy
params := utils.NewPolicyParams()
params.Name = "example-licence-policy"
params.Type = utils.License
params.Description = "License policy with 2 rules"
params.Rules = []utils.PolicyRule{
{
Name: "allowed-licenses",
Criteria: *utils.CreateLicensePolicyCriteria(true, true, false, "MIT", "Apache-2.0"),
Priority: 1,
},
{
Name: "baned-licenses",
Criteria: *utils.CreateLicensePolicyCriteria(false, true, false, "GPL"),
Priority: 2,
},
}
err := xrayManager.CreatePolicy(params)
Get an Xray Policy
policy, err := xrayManager.GetPolicy("example-policy")
Update an Xray Policy
policy, err := xrayManager.GetPolicy("example-policy")
policy.Description = "Updated description"
err := xrayManager.UpdatePolicy(*policy)
Delete an Xray Policy
err := xrayManager.DeletePolicy("example-policy")
Add builds to indexing configuration
buildsToIndex := []string{"buildName1", "buildName2"}
err := xrayManager.AddBuildsToIndexing(buildsToIndex)