-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathclient.go
More file actions
232 lines (196 loc) · 4.73 KB
/
client.go
File metadata and controls
232 lines (196 loc) · 4.73 KB
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
package local
import (
"context"
"errors"
"fmt"
"github.com/boostsecurityio/poutine/analyze"
"github.com/boostsecurityio/poutine/providers/gitops"
"github.com/rs/zerolog/log"
"net/url"
"strings"
)
func NewGitSCMClient(ctx context.Context, repoPath string, gitCommand *gitops.GitCommand) (*ScmClient, error) {
client := gitops.NewGitClient(gitCommand)
return &ScmClient{
gitClient: client,
repoPath: repoPath,
}, nil
}
type ScmClient struct {
analyze.ScmClient
gitClient *gitops.GitClient
repoPath string
}
func (s *ScmClient) GetOrgRepos(ctx context.Context, org string) <-chan analyze.RepoBatch {
return nil
}
func (s *ScmClient) GetRepo(ctx context.Context, org string, name string) (analyze.Repository, error) {
org, repo, err := s.ParseRepoAndOrg("")
if err != nil {
return nil, err
}
baseUrl, err := s.GetBaseURL()
if err != nil {
var gitErr gitops.GitError
if errors.As(err, &gitErr) {
baseUrl = "localrepo"
}
}
return Repo{
BaseUrl: baseUrl,
Org: org,
Name: repo,
}, nil
}
func (s *ScmClient) GetToken() string {
return ""
}
func (s *ScmClient) GetProviderName() string {
providerBaseURL, err := s.GetBaseURL()
if err != nil {
var gitErr gitops.GitError
if errors.As(err, &gitErr) {
return "provider"
}
return ""
}
return providerBaseURL
}
func (s *ScmClient) GetProviderVersion(ctx context.Context) (string, error) {
return "", nil
}
func (s *ScmClient) GetProviderBaseURL() string {
baseURL, err := s.GetBaseURL()
if err != nil {
var gitErr gitops.GitError
if errors.As(err, &gitErr) {
return s.repoPath
}
return ""
}
return baseURL
}
func (s *ScmClient) GetBaseURL() (string, error) {
remote, err := s.gitClient.GetRemoteOriginURL(context.Background(), s.repoPath)
if err != nil {
log.Debug().Err(err).Msg("failed to get remote url for local repo")
return "", err
}
if strings.HasPrefix(remote, "git@") {
return extractHostnameFromSSHURL(remote), nil
}
parsedURL, err := url.Parse(remote)
if err != nil {
log.Error().Err(err).Msg("failed to parse remote url of local repo")
return "", err
}
if parsedURL.Hostname() == "" {
log.Error().Msg("repo remote url does not have a hostname")
return "", errors.New("repo remote url does not have a hostname")
}
return parsedURL.Hostname(), nil
}
func (s *ScmClient) ParseRepoAndOrg(repoString string) (string, string, error) {
remoteURL, err := s.gitClient.GetRemoteOriginURL(context.Background(), s.repoPath)
if err != nil {
var gitErr gitops.GitError
if errors.As(err, &gitErr) {
return "", "local", nil
}
return "", "", err
}
if strings.Contains(remoteURL, "git@") {
remoteURL = strings.Replace(remoteURL, ":", "/", 1)
}
parsedURL, err := url.Parse(remoteURL)
if err != nil {
return "", "", err
}
pathParts := strings.Split(strings.Trim(parsedURL.Path, "/"), "/")
if len(pathParts) < 2 {
return "", "", errors.New("git remote URL path does not contain organization and repository information")
}
org := pathParts[len(pathParts)-2]
repo := strings.TrimSuffix(pathParts[len(pathParts)-1], ".git")
return org, repo, nil
}
type Repo struct {
analyze.Repository
BaseUrl string
Org string
Name string
}
func (gl Repo) GetProviderName() string {
if gl.BaseUrl == "github.com" || gl.BaseUrl == "gitlab.com" {
return gl.BaseUrl[:len(gl.BaseUrl)-4]
}
return gl.BaseUrl
}
func (gl Repo) GetRepoIdentifier() string {
if gl.BaseUrl == "github.com" || gl.BaseUrl == "gitlab.com" {
return fmt.Sprintf("%s/%s", gl.Org, gl.Name)
}
return fmt.Sprintf("%s/%s/%s", gl.BaseUrl, gl.Org, gl.Name)
}
func (gl Repo) BuildGitURL(baseURL string) string {
return ""
}
func (gl Repo) GetIsFork() bool {
return false
}
func (gl Repo) GetHasIssues() bool {
return false
}
func (gl Repo) GetHasWiki() bool {
return false
}
func (gl Repo) GetHasDiscussion() bool {
return false
}
func (gl Repo) GetPrimaryLanguage() string {
return ""
}
func (gl Repo) GetSize() int {
return 1337
}
func (gl Repo) GetDefaultBranch() string {
return "main"
}
func (gl Repo) GetLicense() string {
return ""
}
func (gl Repo) GetIsTemplate() bool {
return false
}
func (gl Repo) GetOrganizationID() int {
return 1337
}
func (gl Repo) GetRepositoryID() int {
return 1337
}
func (gl Repo) GetForksCount() int {
return 0
}
func (gl Repo) GetStarsCount() int {
return 0
}
func (gl Repo) GetOpenIssuesCount() int {
return 0
}
func (gl Repo) GetIsEmpty() bool {
return false
}
func extractHostnameFromSSHURL(sshURL string) string {
parts := strings.Split(sshURL, "@")
if len(parts) != 2 {
log.Error().Msg("invalid SSH URL format")
return ""
}
hostPart := parts[1]
hostnameParts := strings.SplitN(hostPart, ":", 2)
if len(hostnameParts) != 2 {
log.Error().Msg("invalid SSH URL format")
return ""
}
return hostnameParts[0]
}