-
Notifications
You must be signed in to change notification settings - Fork 56
/
repositoryutils.go
86 lines (74 loc) · 2.2 KB
/
repositoryutils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package utils
import (
"errors"
"net/http"
"github.com/buger/jsonparser"
"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/httpclient"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)
type RepoType int
const (
LOCAL RepoType = iota
REMOTE
VIRTUAL
)
var RepoTypes = []string{
"local",
"remote",
"virtual",
}
func (repoType RepoType) String() string {
return RepoTypes[repoType]
}
func GetRepositories(artDetails auth.ServiceDetails, repoType ...RepoType) ([]string, error) {
repos := []string{}
for _, v := range repoType {
r, err := execGetRepositories(artDetails, v)
if err != nil {
return repos, err
}
if len(r) > 0 {
repos = append(repos, r...)
}
}
return repos, nil
}
func execGetRepositories(artDetails auth.ServiceDetails, repoType RepoType) ([]string, error) {
repos := []string{}
artDetails.SetUrl(utils.AddTrailingSlashIfNeeded(artDetails.GetUrl()))
apiUrl := artDetails.GetUrl() + "api/repositories?type=" + repoType.String()
httpClientsDetails := artDetails.CreateHttpClientDetails()
client, err := httpclient.ClientBuilder().Build()
if err != nil {
return repos, err
}
resp, body, _, err := client.SendGet(apiUrl, true, httpClientsDetails)
if err != nil {
return repos, err
}
if resp.StatusCode != http.StatusOK {
return repos, errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + utils.IndentJson(body)))
}
jsonparser.ArrayEach(body, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
val, _, _, err := jsonparser.Get(value, "key")
if err == nil {
repos = append(repos, string(val))
}
})
return repos, nil
}
// Since we can't search dependencies in a remote repository, we will turn the search to the repository's cache.
// Local/Virtual repository name will be returned as is.
func GetRepoNameForDependenciesSearch(repoName string, serviceManager artifactory.ArtifactoryServicesManager) (string, error) {
repoDetails, err := serviceManager.GetRepository(repoName)
if err != nil {
return "", err
}
if repoDetails.Rclass == "remote" {
repoName += "-cache"
}
return repoName, err
}