-
Notifications
You must be signed in to change notification settings - Fork 511
/
client.go
343 lines (282 loc) · 9.44 KB
/
client.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2022 OpenSSF Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package gitlabrepo implements clients.RepoClient for GitLab.
package gitlabrepo
import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"time"
gitlab "gitlab.com/gitlab-org/api/client-go"
"github.com/ossf/scorecard/v5/clients"
sce "github.com/ossf/scorecard/v5/errors"
)
var (
_ clients.RepoClient = &Client{}
errInputRepoType = errors.New("input repo should be of type repoURL")
)
type Client struct {
repourl *Repo
repo *gitlab.Project
glClient *gitlab.Client
contributors *contributorsHandler
branches *branchesHandler
releases *releasesHandler
workflows *workflowsHandler
checkruns *checkrunsHandler
commits *commitsHandler
issues *issuesHandler
project *projectHandler
statuses *statusesHandler
search *searchHandler
searchCommits *searchCommitsHandler
webhook *webhookHandler
languages *languagesHandler
licenses *licensesHandler
tarball *tarballHandler
graphql *graphqlHandler
ctx context.Context
commitDepth int
}
var errRepoAccess = errors.New("repo inaccessible")
// Raise an error if repository access level is private or disabled.
func checkRepoInaccessible(repo *gitlab.Project) error {
if repo.RepositoryAccessLevel == gitlab.DisabledAccessControl {
return fmt.Errorf("%w: %s access level %s",
errRepoAccess, repo.PathWithNamespace, string(repo.RepositoryAccessLevel),
)
}
return nil
}
// InitRepo sets up the GitLab project in local storage for improving performance and GitLab token usage efficiency.
func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitDepth int) error {
glRepo, ok := inputRepo.(*Repo)
if !ok {
return fmt.Errorf("%w: %v", errInputRepoType, inputRepo)
}
// Sanity check.
proj := fmt.Sprintf("%s/%s", glRepo.owner, glRepo.project)
license := true // Get project license information. Used for licenses client.
repo, _, err := client.glClient.Projects.GetProject(proj, &gitlab.GetProjectOptions{License: &license})
if err != nil {
return sce.WithMessage(sce.ErrRepoUnreachable, proj+"\t"+err.Error())
}
if err = checkRepoInaccessible(repo); err != nil {
return sce.WithMessage(sce.ErrRepoUnreachable, err.Error())
}
if commitDepth <= 0 {
client.commitDepth = 30 // default
} else {
client.commitDepth = commitDepth
}
client.repo = repo
client.repourl = &Repo{
scheme: glRepo.scheme,
host: glRepo.host,
owner: glRepo.owner,
project: glRepo.project,
projectID: fmt.Sprint(repo.ID),
defaultBranch: repo.DefaultBranch,
commitSHA: commitSHA,
}
if repo.Owner != nil {
client.repourl.owner = repo.Owner.Username
}
// Init contributorsHandler
client.contributors.init(client.repourl)
// Init commitsHandler
client.commits.init(client.repourl, client.commitDepth)
// Init branchesHandler
client.branches.init(client.repourl)
// Init releasesHandler
client.releases.init(client.repourl)
// Init issuesHandler
client.issues.init(client.repourl)
// Init projectHandler
client.project.init(client.repourl)
// Init workflowsHandler
client.workflows.init(client.repourl)
// Init checkrunsHandler
client.checkruns.init(client.repourl)
// Init statusesHandler
client.statuses.init(client.repourl)
// Init searchHandler
client.search.init(client.repourl)
// Init searchCommitsHandler
client.searchCommits.init(client.repourl)
// Init webhookHandler
client.webhook.init(client.repourl)
// Init languagesHandler
client.languages.init(client.repourl)
// Init languagesHandler
client.licenses.init(client.repourl, repo)
// Init tarballHandler
client.tarball.init(client.ctx, client.repourl, repo, commitSHA)
// Init graphqlHandler
client.graphql.init(client.ctx, client.repourl)
return nil
}
func (client *Client) URI() string {
return fmt.Sprintf("%s/%s/%s", client.repourl.host, client.repourl.owner, client.repourl.project)
}
func (client *Client) LocalPath() (string, error) {
return "", nil
}
func (client *Client) ListFiles(predicate func(string) (bool, error)) ([]string, error) {
return client.tarball.listFiles(predicate)
}
func (client *Client) GetFileReader(filename string) (io.ReadCloser, error) {
return client.tarball.getFile(filename)
}
func (client *Client) ListCommits() ([]clients.Commit, error) {
// Get commits from REST API
commitsRaw, err := client.commits.listRawCommits()
if err != nil {
return []clients.Commit{}, err
}
if len(commitsRaw) < 1 {
return []clients.Commit{}, nil
}
before := commitsRaw[0].CommittedDate
// Get merge request details from GraphQL
// GitLab REST API doesn't provide a way to link Merge Requests and Commits that
// are within them without making a REST call for each commit (~30 by default)
// Making 1 GraphQL query to combine the results of 2 REST calls, we avoid this
// TODO(#3193): Fix the way graphql retrieves merge details to more closely
// line up with commits from listRawCommits
mrDetails, err := client.graphql.getMergeRequestsDetail(before)
if err != nil {
return []clients.Commit{}, err
}
return client.commits.zip(commitsRaw, mrDetails), nil
}
func (client *Client) ListIssues() ([]clients.Issue, error) {
return client.issues.listIssues()
}
func (client *Client) ListReleases() ([]clients.Release, error) {
return client.releases.getReleases()
}
func (client *Client) ListContributors() ([]clients.User, error) {
return client.contributors.getContributors()
}
func (client *Client) IsArchived() (bool, error) {
return client.project.isArchived()
}
func (client *Client) GetDefaultBranch() (*clients.BranchRef, error) {
return client.branches.getDefaultBranch()
}
func (client *Client) GetDefaultBranchName() (string, error) {
return client.repourl.defaultBranch, nil
}
func (client *Client) GetBranch(branch string) (*clients.BranchRef, error) {
return client.branches.getBranch(branch)
}
func (client *Client) GetCreatedAt() (time.Time, error) {
return client.project.getCreatedAt()
}
func (client *Client) GetOrgRepoClient(ctx context.Context) (clients.RepoClient, error) {
return nil, fmt.Errorf("GetOrgRepoClient (GitLab): %w", clients.ErrUnsupportedFeature)
}
func (client *Client) ListWebhooks() ([]clients.Webhook, error) {
return client.webhook.listWebhooks()
}
func (client *Client) ListSuccessfulWorkflowRuns(filename string) ([]clients.WorkflowRun, error) {
return client.workflows.listSuccessfulWorkflowRuns(filename)
}
func (client *Client) ListCheckRunsForRef(ref string) ([]clients.CheckRun, error) {
return client.checkruns.listCheckRunsForRef(ref)
}
func (client *Client) ListStatuses(ref string) ([]clients.Status, error) {
return client.statuses.listStatuses(ref)
}
func (client *Client) ListProgrammingLanguages() ([]clients.Language, error) {
return client.languages.listProgrammingLanguages()
}
// ListLicenses implements RepoClient.ListLicenses.
func (client *Client) ListLicenses() ([]clients.License, error) {
return client.licenses.listLicenses()
}
func (client *Client) Search(request clients.SearchRequest) (clients.SearchResponse, error) {
return client.search.search(request)
}
func (client *Client) SearchCommits(request clients.SearchCommitsOptions) ([]clients.Commit, error) {
return client.searchCommits.search(request)
}
func (client *Client) Close() error {
return nil
}
func CreateGitlabClient(ctx context.Context, host string) (clients.RepoClient, error) {
token := os.Getenv("GITLAB_AUTH_TOKEN")
return CreateGitlabClientWithToken(ctx, token, host)
}
func CreateGitlabClientWithToken(ctx context.Context, token, host string) (clients.RepoClient, error) {
url := "https://" + host
client, err := gitlab.NewClient(token, gitlab.WithBaseURL(url))
if err != nil {
return nil, fmt.Errorf("could not create gitlab client with error: %w", err)
}
return &Client{
ctx: ctx,
glClient: client,
contributors: &contributorsHandler{
glClient: client,
},
branches: &branchesHandler{
glClient: client,
},
releases: &releasesHandler{
glClient: client,
},
workflows: &workflowsHandler{
glClient: client,
},
checkruns: &checkrunsHandler{
glClient: client,
},
commits: &commitsHandler{
glClient: client,
},
issues: &issuesHandler{
glClient: client,
},
project: &projectHandler{
glClient: client,
},
statuses: &statusesHandler{
glClient: client,
},
search: &searchHandler{
glClient: client,
},
searchCommits: &searchCommitsHandler{
glClient: client,
},
webhook: &webhookHandler{
glClient: client,
},
languages: &languagesHandler{
glClient: client,
},
licenses: &licensesHandler{},
tarball: &tarballHandler{},
graphql: &graphqlHandler{},
}, nil
}
// TODO(#2266): implement CreateOssFuzzRepoClient.
func CreateOssFuzzRepoClient(ctx context.Context, logger *log.Logger) (clients.RepoClient, error) {
return nil, fmt.Errorf("%w, oss fuzz currently only supported for github repos", clients.ErrUnsupportedFeature)
}