|
1 | | -/* |
2 | | -Package git holds every implemention needed |
3 | | -to do various git operations. |
4 | | -This is a interface-first implemention. |
5 | | -*/ |
6 | 1 | package git |
7 | 2 |
|
8 | 3 | import ( |
9 | 4 | "net/url" |
10 | 5 | "os" |
11 | | - "strings" |
12 | 6 |
|
13 | 7 | "github.com/go-git/go-git/v5" |
14 | 8 | "github.com/go-git/go-git/v5/config" |
15 | 9 | "github.com/go-git/go-git/v5/plumbing" |
16 | | - "github.com/go-git/go-git/v5/plumbing/transport" |
17 | | - "github.com/go-git/go-git/v5/plumbing/transport/ssh" |
18 | 10 | "github.com/go-logr/logr" |
19 | | - gossh "golang.org/x/crypto/ssh" |
20 | 11 | ) |
21 | 12 |
|
22 | 13 | // GitOperator defines the interface for git operations. |
23 | 14 | type GitOperator interface { |
24 | | - Clone(repoURL string, secretRef []byte, directory string, logger logr.Logger) (err error) |
25 | | - Poll(repo string, secretRef []byte, branch string, directory string, logger logr.Logger) (changes bool, err error) |
26 | | - Hash(repo string, secretRef []byte, branch string, logger logr.Logger) (hash string, err error) |
| 15 | + Clone(repoURL string, secretRef []byte, directory string, logger logr.Logger) error |
| 16 | + Poll(repoURL string, secretRef []byte, branch string, directory string, logger logr.Logger) (bool, error) |
| 17 | + Hash(repo string, secretRef []byte, branch string, logger logr.Logger) (string, error) |
27 | 18 | } |
28 | 19 |
|
29 | 20 | // GitImplementer implements the GitOperator interface. |
30 | 21 | type GitImplementer struct{} |
31 | 22 |
|
32 | | -// Clone clones the given repository to a local directory. |
33 | | -func (g *GitImplementer) Clone(repoURL string, secretRef []byte, directory string, logger logr.Logger) (err error) { |
34 | | - var auth transport.AuthMethod |
| 23 | +// getSSHAuth is a helper function to create an ssh.PublicKeys AuthMethod. |
| 24 | +func getSSHAuth(secretRef []byte) (transport.AuthMethod, error) { |
| 25 | + if len(secretRef) == 0 { |
| 26 | + return nil, fmt.Errorf("SSH secret reference is empty") |
| 27 | + } |
35 | 28 |
|
36 | | - logger.Info("Creating directory") |
37 | | - err = os.Mkdir(directory, 0755) |
| 29 | + auth, err := ssh.NewPublicKeys("git", secretRef, "") |
38 | 30 | if err != nil { |
39 | | - logger.Error(err, "Failed to create directory", "directory", directory) |
40 | | - |
41 | | - return err |
| 31 | + return nil, fmt.Errorf("failed to create public keys from secret: %w", err) |
42 | 32 | } |
43 | 33 |
|
44 | | - // Check if repo and directory are empty. |
45 | | - logger.Info("Checking if repo and or directory is empty") |
46 | | - if empty(repoURL, directory) { |
47 | | - logger.Error(err, "repo and or directory is empty", "repoURL", repoURL, "directory", directory) |
| 34 | + // Insecurely ignore host key |
| 35 | + // ToDO: Use a proper host key verification callback. |
| 36 | + auth.HostKeyCallback = gossh.InsecureIgnoreHostKey() |
48 | 37 |
|
49 | | - return err |
| 38 | + return auth, nil |
| 39 | +} |
| 40 | + |
| 41 | +// Clone clones the given repository to a local directory. |
| 42 | +func (g *GitImplementer) Clone(repoURL string, secretRef []byte, directory string, logger logr.Logger) error { |
| 43 | + if repoURL == "" || directory == "" { |
| 44 | + return fmt.Errorf("repoURL and directory must not be empty") |
50 | 45 | } |
51 | 46 |
|
52 | | - logger.Info("Retrieving auth Token") |
53 | | - auth, err = ssh.NewPublicKeys("git", secretRef, "") |
54 | | - if err != nil { |
55 | | - logger.Error(err, "Failed on retrieve the token from the tokenContent") |
| 47 | + logger.Info("Creating directory", "directory", directory) |
| 48 | + if err := os.MkdirAll(directory, 0755); err != nil { |
| 49 | + return fmt.Errorf("failed to create directory %s: %w", directory, err) |
| 50 | + } |
56 | 51 |
|
| 52 | + logger.Info("Retrieving auth method") |
| 53 | + auth, err := getSSHAuth(secretRef) |
| 54 | + if err != nil { |
57 | 55 | return err |
58 | 56 | } |
59 | 57 |
|
60 | | - logger.Info("Plain Cloning Repo") |
| 58 | + logger.Info("Cloning repository", "repoURL", repoURL) |
61 | 59 | _, err = git.PlainClone(directory, false, &git.CloneOptions{ |
62 | | - URL: repoURL, |
63 | | - Auth: auth, |
64 | | - Depth: 1, |
| 60 | + URL: repoURL, |
| 61 | + Auth: auth, |
| 62 | + Progress: os.Stdout, // Optional: show progress |
| 63 | + Depth: 1, |
65 | 64 | }) |
66 | 65 | if err != nil { |
67 | | - logger.Error(err, "Failed to clone repo", "repo", repoURL) |
68 | | - |
69 | | - return err |
| 66 | + return fmt.Errorf("failed to clone repo %s: %w", repoURL, err) |
70 | 67 | } |
71 | 68 |
|
72 | | - return err |
| 69 | + return nil |
73 | 70 | } |
74 | 71 |
|
75 | | -// Poll polls for changes for the given remote git repository. Returns true, if current local commit hash and remote hash are not equal. |
76 | | -func (g *GitImplementer) Poll(repo string, secretRef []byte, branch string, directory string, logger logr.Logger) (changes bool, err error) { |
77 | | - // Defaults to false. We only change to true if there is a difference between the hashes. |
78 | | - changes = false |
79 | | - |
80 | | - // Check if repo and directory are empty. |
81 | | - if empty(repo, directory) { |
82 | | - logger.Error(err, "repo and or directory is empty", "repo", repo, "directory", directory) |
83 | | - |
84 | | - return changes, err |
| 72 | +// Poll polls for changes for the given remote git repository. |
| 73 | +// Returns true if current local commit hash and remote hash are different. |
| 74 | +func (g *GitImplementer) Poll(repoURL string, secretRef []byte, branch string, directory string, logger logr.Logger) (bool, error) { |
| 75 | + if repoURL == "" || directory == "" { |
| 76 | + return false, fmt.Errorf("repoURL and directory must not be empty") |
85 | 77 | } |
86 | 78 |
|
87 | 79 | // Get hash from local repo. |
88 | | - localHash, err := g.Hash(directory, secretRef, branch, logger) |
| 80 | + localHash, err := g.Hash(directory, nil, "", logger) // No secret or branch needed for local HEAD |
89 | 81 | if err != nil { |
90 | | - logger.Error(err, "Failed to get hash", "repo", repo, "directory", directory) |
91 | | - |
92 | | - return changes, err |
| 82 | + return false, fmt.Errorf("failed to get local hash from %s: %w", directory, err) |
93 | 83 | } |
94 | 84 |
|
95 | 85 | // Get Hash from remote repo |
96 | | - remoteHash, err := g.Hash(repo, secretRef, branch, logger) |
| 86 | + remoteHash, err := g.Hash(repoURL, secretRef, branch, logger) |
97 | 87 | if err != nil { |
98 | | - logger.Error(err, "Failed to get hash", "repo", repo, "directory", directory) |
99 | | - |
100 | | - return changes, err |
| 88 | + return false, fmt.Errorf("failed to get remote hash for %s: %w", repoURL, err) |
101 | 89 | } |
102 | 90 |
|
103 | | - if localHash != remoteHash { |
104 | | - changes = true |
105 | | - } |
| 91 | + changes := localHash != remoteHash |
| 92 | + logger.Info("Polling result", "localHash", localHash, "remoteHash", remoteHash, "changes", changes) |
106 | 93 |
|
107 | | - return changes, err |
| 94 | + return changes, nil |
108 | 95 | } |
109 | 96 |
|
110 | 97 | // Hash retrieves the hash of the given repository. |
111 | | -func (g *GitImplementer) Hash(repo string, secretRef []byte, branch string, logger logr.Logger) (hash string, err error) { |
112 | | - var auth transport.AuthMethod |
| 98 | +func (g *GitImplementer) Hash(repo string, secretRef []byte, branch string, logger logr.Logger) (string, error) { |
| 99 | + if isURL(repo) { |
| 100 | + return g.remoteHash(repo, secretRef, branch, logger) |
| 101 | + } |
| 102 | + return g.localHash(repo, logger) |
| 103 | +} |
113 | 104 |
|
114 | | - auth, err = ssh.NewPublicKeys("git", secretRef, "") |
| 105 | +// localHash retrieves the HEAD commit hash from a local repository. |
| 106 | +func (g *GitImplementer) localHash(path string, logger logr.Logger) (string, error) { |
| 107 | + localRepo, err := git.PlainOpen(path) |
115 | 108 | if err != nil { |
116 | | - logger.Error(err, "Failed to retrieve the token from the tokenContent") |
117 | | - |
118 | | - return hash, err |
| 109 | + return "", fmt.Errorf("failed to open local repo at %s: %w", path, err) |
119 | 110 | } |
120 | 111 |
|
121 | | - pkAuth, ok := auth.(*ssh.PublicKeys) |
122 | | - if !ok { |
123 | | - logger.Error(err, "pkAuth error") |
| 112 | + headRef, err := localRepo.Head() |
| 113 | + if err != nil { |
| 114 | + return "", fmt.Errorf("failed to get HEAD for local repo at %s: %w", path, err) |
124 | 115 | } |
125 | 116 |
|
126 | | - pkAuth.HostKeyCallback = gossh.InsecureIgnoreHostKey() |
127 | | - |
128 | | - switch { |
129 | | - case isURL(repo): |
130 | | - remoterepo := git.NewRemote(nil, &config.RemoteConfig{ |
131 | | - URLs: []string{repo}, |
132 | | - Name: "origin", |
133 | | - }) |
134 | | - |
135 | | - refs, err := remoterepo.List(&git.ListOptions{ |
136 | | - Auth: auth, |
137 | | - }) |
138 | | - if err != nil { |
139 | | - logger.Error(err, "Failed to list remote repo", "repo", repo) |
140 | | - |
141 | | - return hash, err |
142 | | - } |
143 | | - |
144 | | - refName := plumbing.NewBranchReferenceName(branch) |
145 | | - for _, ref := range refs { |
146 | | - if ref.Name() == refName { |
147 | | - return ref.Hash().String(), err |
148 | | - } |
149 | | - } |
150 | | - |
151 | | - return hash, err |
152 | | - case !isURL(repo): |
153 | | - localRepo, err := git.PlainOpen(repo) |
154 | | - if err != nil { |
155 | | - logger.Error(err, "Failed to open local repo", "repo", repo) |
| 117 | + return headRef.Hash().String(), nil |
| 118 | +} |
156 | 119 |
|
157 | | - return hash, err |
158 | | - } |
| 120 | +// remoteHash retrieves the commit hash for a specific branch from a remote repository. |
| 121 | +func (g *GitImplementer) remoteHash(repoURL string, secretRef []byte, branch string, logger logr.Logger) (string, error) { |
| 122 | + if branch == "" { |
| 123 | + return "", fmt.Errorf("branch must be specified for remote repository") |
| 124 | + } |
159 | 125 |
|
160 | | - headRef, err := localRepo.Head() |
161 | | - if err != nil { |
162 | | - logger.Error(err, "failed to get head for local git repo", "repo", repo) |
| 126 | + auth, err := getSSHAuth(secretRef) |
| 127 | + if err != nil { |
| 128 | + return "", err |
| 129 | + } |
163 | 130 |
|
164 | | - return hash, err |
165 | | - } |
| 131 | + remoterepo := git.NewRemote(nil, &config.RemoteConfig{ |
| 132 | + URLs: []string{repoURL}, |
| 133 | + }) |
166 | 134 |
|
167 | | - hash = headRef.Hash().String() |
168 | | - if hash == "" { |
169 | | - logger.Error(err, "failed to get hash for local git repo", "repo", repo) |
| 135 | + refs, err := remoterepo.List(&git.ListOptions{ |
| 136 | + Auth: auth, |
| 137 | + }) |
| 138 | + if err != nil { |
| 139 | + return "", fmt.Errorf("failed to list remote repo %s: %w", repoURL, err) |
| 140 | + } |
170 | 141 |
|
171 | | - return hash, err |
| 142 | + refName := plumbing.NewBranchReferenceName(branch) |
| 143 | + for _, ref := range refs { |
| 144 | + if ref.Name() == refName { |
| 145 | + return ref.Hash().String(), nil |
172 | 146 | } |
173 | | - |
174 | | - return hash, err |
175 | 147 | } |
176 | 148 |
|
177 | | - return hash, err |
| 149 | + return "", fmt.Errorf("branch '%s' not found in remote repository %s", branch, repoURL) |
178 | 150 | } |
179 | 151 |
|
180 | | -// isURL checks if the given string is a valid URL. |
181 | | -func isURL(repo string) bool { |
182 | | - if repo == "" { |
183 | | - return false |
184 | | - } |
185 | | - parsedURL, err := url.ParseRequestURI(repo) |
186 | | - if err == nil && parsedURL.Scheme != "" { |
| 152 | +// isUrl checks if the given string is a valid URL. |
| 153 | +func isURL(str string) bool { |
| 154 | + // A simple check for SCP-like syntax or a scheme. |
| 155 | + // https://en.wikipedia.org/wiki/Secure_copy_protocol :-) |
| 156 | + if strings.Contains(str, "@") && strings.Contains(str, ":") { |
187 | 157 | return true |
188 | 158 | } |
189 | | - |
190 | | - if strings.Contains(repo, "@") && strings.Contains(repo, ":") { |
191 | | - return true |
192 | | - } |
193 | | - // if parsedURL.Scheme != "" { |
194 | | - // return true |
195 | | - // } else { |
196 | | - // return false |
197 | | - // } |
198 | | - return false |
| 159 | + u, err := url.ParseRequestURI(str) |
| 160 | + return err == nil && u.Scheme != "" && u.Host != "" |
199 | 161 | } |
200 | 162 |
|
201 | 163 | // empty checks if the repo and directory strings are empty. |
|
0 commit comments