-
Notifications
You must be signed in to change notification settings - Fork 247
/
main.go
365 lines (333 loc) · 9.06 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/transport"
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
"github.com/brigadecore/brigade-foundations/retries"
"github.com/brigadecore/brigade-foundations/version"
"github.com/brigadecore/brigade/sdk/v2/core"
)
const (
workspace = "/var/vcs"
maxRetryCount = 5
maxBackoff = 5 * time.Second
)
func main() {
log.Printf(
"Starting Brigade Git Initializer -- version %s -- commit %s",
version.Version(),
version.Commit(),
)
if err := gitCheckout(); err != nil {
fmt.Printf("\n%s\n\n", err)
os.Exit(1)
}
}
// nolint: gocyclo
func gitCheckout() error {
eventPath := "/var/event/event.json"
data, err := ioutil.ReadFile(eventPath)
if err != nil {
return errors.Wrapf(err, "unable read the event file %q", eventPath)
}
var event struct {
Project struct {
Secrets map[string]string `json:"secrets"`
} `json:"project"`
Worker struct {
Git *core.GitConfig `json:"git"`
} `json:"worker"`
}
err = json.Unmarshal(data, &event)
if err != nil {
return errors.Wrap(err, "error unmarshaling the event")
}
// Extract git config
gitConfig := event.Worker.Git
if gitConfig == nil {
return fmt.Errorf("git config from %q is empty", eventPath)
}
// Setup Auth
var auth transport.AuthMethod
// TODO: What about token-based auth?
// (see v1 askpass.sh/BRIGADE_REPO_AUTH_TOKEN)
// TODO: Check for SSH Cert
// (see https://github.com/brigadecore/brigade/pull/1008)
// Check for SSH Key
privateKey, ok := event.Project.Secrets["gitSSHKey"]
if ok {
var publicKeys *gitssh.PublicKeys
publicKeys, err = gitssh.NewPublicKeys(
"git",
[]byte(privateKey),
event.Project.Secrets["gitSSHKeyPassword"],
)
if err != nil {
return errors.Wrapf(
err,
"error configuring authentication for remote with URL %s",
event.Worker.Git.CloneURL,
)
}
// The following avoids:
// "unable to find any valid known_hosts file,
// set SSH_KNOWN_HOSTS env variable"
publicKeys.HostKeyCallback = ssh.InsecureIgnoreHostKey() // nolint: gosec
auth = publicKeys
}
var worktree *git.Worktree
if event.Worker.Git.Commit == "" && event.Worker.Git.Ref == "" {
worktree, err = simpleCheckout(gitConfig.CloneURL, auth)
} else {
worktree, err = complexCheckout(
gitConfig.CloneURL,
event.Worker.Git.Commit,
event.Worker.Git.Ref,
auth,
)
}
if err != nil {
return err
}
// Initialize submodules if configured to do so
if event.Worker.Git.InitSubmodules {
err = initSubmodules(worktree)
}
return err
}
func simpleCheckout(
cloneURL string,
auth transport.AuthMethod,
) (*git.Worktree, error) {
// This will reliably get whichever branch is designated as the default.
repo, err := git.PlainClone(
workspace,
false,
&git.CloneOptions{
URL: cloneURL,
Auth: auth,
Progress: os.Stdout,
},
)
if err != nil {
return nil, errors.Wrapf(err, "error cloning repository at %s", cloneURL)
}
worktree, err := repo.Worktree()
return worktree, errors.Wrapf(
err,
"error getting worktree from repository cloned from %s",
cloneURL,
)
}
func complexCheckout(
cloneURL string,
commit string,
ref string,
auth transport.AuthMethod,
) (*git.Worktree, error) {
// Prioritize using Commit; alternatively try Ref
commitRef := commit
if commitRef == "" {
commitRef = ref // This will never be empty
}
fullRef := plumbing.NewReferenceFromStrings(commitRef, commitRef)
refSpec := config.RefSpec(
fmt.Sprintf("+%s:%s", fullRef.Name(), fullRef.Name()))
// Initialize an empty repository with an empty working tree
gitStorage := filesystem.NewStorage(
osfs.New(filepath.Join(workspace, ".git")),
cache.NewObjectLRUDefault(),
)
repo, err := git.Init(gitStorage, osfs.New(workspace))
if err != nil {
return nil, errors.Wrapf(
err,
"error initializing git repository at %s",
workspace,
)
}
const remoteName = "origin"
// If we're not dealing with an exact commit, and we don't already have a
// full reference, list the remote refs to build out a full, updated refspec
//
// For example, we might be supplied with the tag "v0.1.0", but if we use
// this directly, we'll get: couldn't find remote ref "v0.1.0"
// So we need to find the full remote ref; in this case, "refs/tags/v0.1.0"
if commit == "" && !isFullReference(fullRef.Name()) {
// Create a new remote for the purposes of listing remote refs and finding
// the full ref we want
remoteConfig := &config.RemoteConfig{
Name: remoteName,
URLs: []string{cloneURL},
Fetch: []config.RefSpec{refSpec},
}
rem := git.NewRemote(gitStorage, remoteConfig)
// List remote refs
var refs []*plumbing.Reference
refs, err = rem.List(&git.ListOptions{Auth: auth})
if err != nil {
return nil, errors.Wrap(err, "error listing remotes")
}
// Filter the list of refs and only keep the full ref matching our commitRef
matches := make([]*plumbing.Reference, 0)
for _, ref := range refs {
// Ignore the HEAD symbolic reference
// e.g. [HEAD ref: refs/heads/main]
if ref.Type() == plumbing.SymbolicReference {
continue
}
// Compare the short names of both refs,
// where the short name of e.g. '/refs/heads/main' is 'main'
// Alternatively, match on ref hash
if ref.Name().Short() == fullRef.Name().Short() ||
ref.Hash() == fullRef.Hash() {
matches = append(matches, ref)
}
}
if len(matches) == 0 {
return nil, fmt.Errorf(
"reference %q not found in repo %q",
fullRef.Name(),
cloneURL,
)
}
if len(matches) > 1 {
return nil, fmt.Errorf(
"found more than one match for reference %q: %+v",
fullRef.Name(),
matches,
)
}
fullRef = matches[0]
// Create refspec with the updated ref
refSpec = config.RefSpec(fmt.Sprintf("+%s:%s",
fullRef.Name(), fullRef.Name()))
}
// Create the remote that we'll use to fetch, using the updated/full refspec
remoteConfig := &config.RemoteConfig{
Name: remoteName,
URLs: []string{cloneURL},
Fetch: []config.RefSpec{refSpec},
}
remote, err := repo.CreateRemote(remoteConfig)
if err != nil {
return nil, errors.Wrap(err, "error creating remote")
}
// Create a FETCH_HEAD reference pointing to our ref hash
// The go-git library doesn't appear to support adding this, though the
// git CLI does. This is for parity with v1 functionality.
//
// From https://git-scm.com/docs/gitrevisions:
// "FETCH_HEAD records the branch which you fetched from a remote repository
// with your last git fetch invocation."
newRef := plumbing.NewReferenceFromStrings("FETCH_HEAD",
fullRef.Hash().String())
err = repo.Storer.SetReference(newRef)
if err != nil {
return nil, errors.Wrap(err, "unable to set ref")
}
// Fetch the ref specs we are interested in
fetchOpts := &git.FetchOptions{
RemoteName: remoteName,
RefSpecs: []config.RefSpec{refSpec},
Force: true,
Auth: auth,
Progress: os.Stdout,
}
if retryErr := retries.ManageRetries(
context.Background(),
"git fetch",
maxRetryCount,
maxBackoff,
func() (bool, error) {
err = remote.Fetch(fetchOpts)
if err != nil {
return true, errors.Wrap(err, "error fetching refs from the remote")
}
return false, nil
},
); retryErr != nil {
return nil, retryErr
}
// Get the repository's working tree
worktree, err := repo.Worktree()
if err != nil {
return nil, errors.Wrap(err, "unable to access the repo worktree")
}
// Check out whatever we're interested in into the working tree
if retryErr := retries.ManageRetries(
context.Background(),
"git checkout",
maxRetryCount,
maxBackoff,
func() (bool, error) {
err := worktree.Checkout(
&git.CheckoutOptions{
Branch: fullRef.Name(),
Force: true,
},
)
if err != nil {
return true, errors.Wrapf(
err,
"unable to checkout using %q",
commitRef,
)
}
return false, nil
},
); retryErr != nil {
return nil, retryErr
}
return worktree, nil
}
func initSubmodules(worktree *git.Worktree) error {
submodules, err := worktree.Submodules()
if err != nil {
return errors.Wrap(err, "error retrieving submodules: %s")
}
if retryErr := retries.ManageRetries(
context.Background(),
"update submodules",
maxRetryCount,
maxBackoff,
func() (bool, error) {
for _, submodule := range submodules {
if err = submodule.Update(
&git.SubmoduleUpdateOptions{
Init: true,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
},
); err != nil {
return true, errors.Wrapf(
err,
"error updating submodule %q",
submodule.Config().Name,
)
}
}
return false, nil
},
); retryErr != nil {
return retryErr
}
return nil
}
func isFullReference(name plumbing.ReferenceName) bool {
return name.IsBranch() || name.IsNote() || name.IsRemote() || name.IsTag()
}