-
Notifications
You must be signed in to change notification settings - Fork 114
/
git.go
123 lines (114 loc) · 2.93 KB
/
git.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
package warehouses
import (
"context"
"strings"
"github.com/pkg/errors"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
"github.com/akuity/kargo/internal/controller/git"
"github.com/akuity/kargo/internal/credentials"
"github.com/akuity/kargo/internal/logging"
)
type gitMeta struct {
Commit string
Message string
Author string
}
func (r *reconciler) getLatestCommits(
ctx context.Context,
namespace string,
subs []kargoapi.RepoSubscription,
) ([]kargoapi.GitCommit, error) {
latestCommits := make([]kargoapi.GitCommit, 0, len(subs))
for _, s := range subs {
if s.Git == nil {
continue
}
sub := s.Git
logger := logging.LoggerFromContext(ctx).WithField("repo", sub.RepoURL)
creds, ok, err :=
r.credentialsDB.Get(ctx, namespace, credentials.TypeGit, sub.RepoURL)
if err != nil {
return nil, errors.Wrapf(
err,
"error obtaining credentials for git repo %q",
sub.RepoURL,
)
}
var repoCreds *git.RepoCredentials
if ok {
repoCreds = &git.RepoCredentials{
Username: creds.Username,
Password: creds.Password,
SSHPrivateKey: creds.SSHPrivateKey,
}
logger.Debug("obtained credentials for git repo")
} else {
logger.Debug("found no credentials for git repo")
}
gm, err := r.getLatestCommitMetaFn(ctx, sub.RepoURL, sub.Branch, repoCreds)
if err != nil {
return nil, errors.Wrapf(
err,
"error determining latest commit ID of git repo %q",
sub.RepoURL,
)
}
logger.WithField("commit", gm.Commit).
Debug("found latest commit from repo")
latestCommits = append(
latestCommits,
kargoapi.GitCommit{
RepoURL: sub.RepoURL,
ID: gm.Commit,
Branch: sub.Branch,
Message: gm.Message,
},
)
}
return latestCommits, nil
}
func getLatestCommitMeta(
ctx context.Context,
repoURL string,
branch string,
creds *git.RepoCredentials,
) (*gitMeta, error) {
logger := logging.LoggerFromContext(ctx).WithField("repo", repoURL)
if creds == nil {
creds = &git.RepoCredentials{}
}
repo, err := git.Clone(repoURL, *creds)
if err != nil {
return nil, errors.Wrapf(err, "error cloning git repo %q", repoURL)
}
if branch != "" {
if err = repo.Checkout(branch); err != nil {
return nil, errors.Wrapf(
err,
"error checking out branch %q from git repo",
repoURL,
)
}
}
var gm gitMeta
gm.Commit, err = repo.LastCommitID()
if err != nil {
return nil, errors.Wrapf(
err,
"error determining last commit ID from git repo %q (branch: %q)",
repoURL,
branch,
)
}
msg, err := repo.CommitMessage(gm.Commit)
// Since we currently store commit messages in Stage status, we only capture
// the first line of the commit message for brevity
gm.Message = strings.Split(strings.TrimSpace(msg), "\n")[0]
if err != nil {
// This is best effort, so just log the error
logger.Warnf("failed to get message from commit %q: %v", gm.Commit, err)
}
// TODO: support git author
//gm.Author, err = repo.Author(gm.Commit)
return &gm, nil
}