-
Notifications
You must be signed in to change notification settings - Fork 247
/
main.go
78 lines (68 loc) · 1.71 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
//go:build !testUnit && !lint
// +build !testUnit,!lint
// We exclude this file from unit tests and linting because it cannot be
// compiled without CGO and a specific version of libgit2 pre-installed. To keep
// our linting and unit tests lightweight, those are complications we'd like to
// avoid. We'll live without the linting and test this well with integration
// tests.
package main
import (
"log"
"github.com/brigadecore/brigade-foundations/version"
git "github.com/libgit2/git2go/v32"
)
func main() {
log.Printf(
"Starting Brigade Git Initializer -- version %s -- commit %s",
version.Version(),
version.Commit(),
)
event, err := getEvent("/var/event/event.json")
if err != nil {
log.Fatal(err)
}
gitConfig := event.Worker.Git
if gitConfig == nil {
log.Fatal("event has no git config")
}
credentialsCallback, err := getCredentialsCallback(event.Project.Secrets)
if err != nil {
log.Fatal(err)
}
const workspacePath = "/var/vcs"
log.Printf("cloning repository from %q into %q",
gitConfig.CloneURL,
workspacePath,
)
repo, err := git.Clone(
event.Worker.Git.CloneURL,
workspacePath,
&git.CloneOptions{
FetchOptions: git.FetchOptions{
RemoteCallbacks: git.RemoteCallbacks{
CertificateCheckCallback: func(*git.Certificate, bool, string) error {
return nil
},
CredentialsCallback: credentialsCallback,
},
},
},
)
if err != nil {
log.Fatalf(
"error cloning repository from %q into %q: %s",
gitConfig.CloneURL,
workspacePath,
err,
)
}
defer repo.Free()
if err = checkout(repo, gitConfig.Commit, gitConfig.Ref); err != nil {
log.Fatal(err)
}
if gitConfig.InitSubmodules {
if err = initSubmodules(repo); err != nil {
log.Fatal(err)
}
}
}