-
Notifications
You must be signed in to change notification settings - Fork 2
/
put.go
183 lines (148 loc) · 4.17 KB
/
put.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package resource
import (
"log"
"os"
"path/filepath"
"time"
"github.com/digitalocean/concourse-resource-library/artifactory"
"github.com/digitalocean/concourse-resource-library/git"
rlog "github.com/digitalocean/concourse-resource-library/log"
meta "github.com/digitalocean/concourse-resource-library/metadata"
"github.com/jfrog/jfrog-client-go/artifactory/buildinfo"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
)
// Put performs the Put operation for the resource
func Put(req PutRequest, dir string) (GetResponse, error) {
res := GetResponse{
Version: Version{},
Metadata: meta.Metadata{},
}
c, err := newClient(req.Source)
if err != nil {
log.Println(err)
return res, err
}
log.Println("working directory:", dir)
log.Printf("put parameters: %+v", req.Params)
if req.Params.Params != "" {
err = req.Params.Parse()
if err != nil {
rlog.StdErr("failed to parse params file", err)
}
}
b := buildInfo(req.Params, dir)
props := properties(b)
if req.Params.Properties != "" {
err = props.FromFile(filepath.Join(dir, req.Params.Properties))
if err != nil {
rlog.StdErr("failed to read properties file", err)
}
}
pattern := filepath.Join(dir, req.Params.Pattern)
rlog.StdErr("pattern", pattern)
rlog.StdErr("artifact properties", props)
digest, err := c.PushImage(dir, req.OCIRepository(), req.Params.Pattern, req.Params.Tags)
if err != nil {
log.Println(err)
return res, err
}
v := Version{
Digest: digest,
Image: req.Params.Image,
Repo: req.Params.Target,
Tag: req.Params.Tags[0],
}
item, err := c.SearchItem(v.ArtifactoryPath())
if err != nil {
rlog.StdErr("failed to search", err)
log.Println(err)
return res, err
}
// override to set modified time
v, err = processItem(item)
if err != nil {
rlog.StdErr("failed to process result", err)
log.Println(err)
return res, err
}
res = GetResponse{
Version: v,
Metadata: metadata(
artifactory.Artifact{
File: utils.FileInfo{ArtifactoryPath: v.ArtifactoryPath()},
Item: item,
}),
}
return res, nil
}
func properties(b buildinfo.BuildInfo) artifactory.Properties {
props := artifactory.Properties{
artifactory.Property{Name: "build.name", Value: b.Name},
artifactory.Property{Name: "build.number", Value: b.Number},
artifactory.Property{Name: "build.started", Value: b.Started},
}
if b.Vcs != nil {
props = append(props, artifactory.Property{Name: "vcs.revision", Value: b.Vcs.Revision})
props = append(props, artifactory.Property{Name: "vcs.url", Value: b.Vcs.Url})
}
return props
}
func buildInfo(params PutParameters, dir string) buildinfo.BuildInfo {
b := buildinfo.BuildInfo{
Name: os.Getenv("BUILD_TEAM_NAME") + "-" + os.Getenv("BUILD_PIPELINE_NAME") + "-" + os.Getenv("BUILD_JOB_NAME"),
Number: os.Getenv("BUILD_ID"),
Started: time.Now().Format("2006-01-02T15:04:05.000-0700"),
Agent: &buildinfo.Agent{Name: "Concourse"},
BuildAgent: &buildinfo.Agent{Name: "digitalocean/artifactory-resource"},
BuildUrl: os.Getenv("ATC_EXTERNAL_URL") + "/builds/" + os.Getenv("BUILD_ID"),
}
if params.BuildEnv != "" {
p := artifactory.Properties{}
err := p.FromFile(filepath.Join(dir, params.BuildEnv))
if err != nil {
rlog.StdErr("failed to read build environment file", err)
}
log.Println("build env:", p)
b.Properties = p.Env()
}
if params.RepositoryPath != "" {
b.Vcs = vcsInfo(filepath.Join(dir, params.RepositoryPath), params.Repository)
}
return b
}
func vcsInfo(path, repo string) *buildinfo.Vcs {
vcs := buildinfo.Vcs{}
log.Println("vcs path:", path)
g := git.Client{}
r, err := g.Open(path)
if err != nil {
rlog.StdErr("failed to open repository", err)
return &vcs
}
rev, err := r.Head()
if err != nil {
rlog.StdErr("failed to read vcs revision", err)
return &vcs
}
vcs.Revision = rev.Hash().String()
if repo != "" {
vcs.Url = repo
return &vcs
}
remotes, err := r.Remotes()
if err != nil {
rlog.StdErr("failed to read vcs info", err)
return &vcs
}
if len(remotes) > 0 && len(remotes[0].Config().URLs) > 0 {
url := remotes[0].Config().URLs[0]
vcs.Url = url
}
return &vcs
}
func moduleID(m, b string) string {
if m != "" {
return m
}
return b
}