This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
/
repo_service.go
149 lines (116 loc) · 3.63 KB
/
repo_service.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
package services
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/util/db"
"github.com/argoproj/argo-cd/v2/util/git"
)
// RepositoryDB Is a lean facade for ArgoDB,
// Using a lean interface makes it more easy to test the functionality the git generator uses
type RepositoryDB interface {
GetRepository(ctx context.Context, url string) (*v1alpha1.Repository, error)
}
type argoCDService struct {
repositoriesDB RepositoryDB
}
type Repos interface {
// GetFiles returns content of files (not directories) within the target repo
GetFiles(ctx context.Context, repoURL string, revision string, pattern string) (map[string][]byte, error)
// GetDirectories returns a list of directories (not files) within the target repo
GetDirectories(ctx context.Context, repoURL string, revision string) ([]string, error)
}
func NewArgoCDService(db db.ArgoDB, repoServerAddress string) Repos {
return &argoCDService{
repositoriesDB: db.(RepositoryDB),
}
}
func (a *argoCDService) GetFiles(ctx context.Context, repoURL string, revision string, pattern string) (map[string][]byte, error) {
repo, err := a.repositoriesDB.GetRepository(ctx, repoURL)
if err != nil {
return nil, fmt.Errorf("Error in GetRepository: %w", err)
}
gitRepoClient, err := git.NewClient(repo.Repo, repo.GetGitCreds(), repo.IsInsecure(), repo.IsLFSEnabled(), repo.Proxy)
if err != nil {
return nil, err
}
err = checkoutRepo(gitRepoClient, revision)
if err != nil {
return nil, err
}
paths, err := gitRepoClient.LsFiles(pattern)
if err != nil {
return nil, fmt.Errorf("Error during listing files of local repo: %w", err)
}
res := map[string][]byte{}
for _, filePath := range paths {
bytes, err := os.ReadFile(filepath.Join(gitRepoClient.Root(), filePath))
if err != nil {
return nil, err
}
res[filePath] = bytes
}
return res, nil
}
func (a *argoCDService) GetDirectories(ctx context.Context, repoURL string, revision string) ([]string, error) {
repo, err := a.repositoriesDB.GetRepository(ctx, repoURL)
if err != nil {
return nil, fmt.Errorf("Error in GetRepository: %w", err)
}
gitRepoClient, err := git.NewClient(repo.Repo, repo.GetGitCreds(), repo.IsInsecure(), repo.IsLFSEnabled(), repo.Proxy)
if err != nil {
return nil, err
}
err = checkoutRepo(gitRepoClient, revision)
if err != nil {
return nil, err
}
filteredPaths := []string{}
repoRoot := gitRepoClient.Root()
if err := filepath.Walk(repoRoot, func(path string, info os.FileInfo, fnErr error) error {
if fnErr != nil {
return fnErr
}
if !info.IsDir() { // Skip files: directories only
return nil
}
fname := info.Name()
if strings.HasPrefix(fname, ".") { // Skip all folders starts with "."
return filepath.SkipDir
}
relativePath, err := filepath.Rel(repoRoot, path)
if err != nil {
return err
}
if relativePath == "." { // Exclude '.' from results
return nil
}
filteredPaths = append(filteredPaths, relativePath)
return nil
}); err != nil {
return nil, err
}
return filteredPaths, nil
}
func checkoutRepo(gitRepoClient git.Client, revision string) error {
err := gitRepoClient.Init()
if err != nil {
return fmt.Errorf("Error during initializing repo: %w", err)
}
err = gitRepoClient.Fetch(revision)
if err != nil {
return fmt.Errorf("Error during fetching repo: %w", err)
}
commitSHA, err := gitRepoClient.LsRemote(revision)
if err != nil {
return fmt.Errorf("Error during fetching commitSHA: %w", err)
}
err = gitRepoClient.Checkout(commitSHA)
if err != nil {
return fmt.Errorf("Error during repo checkout: %w", err)
}
return nil
}