Skip to content

Commit a86bf00

Browse files
authored
chore: enable wsl linter (#417)
See #331.
1 parent ffd0c64 commit a86bf00

File tree

10 files changed

+53
-3
lines changed

10 files changed

+53
-3
lines changed

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ linters:
100100
- wastedassign
101101
- whitespace
102102
# - wrapcheck
103-
# - wsl
103+
- wsl
104104

105105
# disable:
106106
# # Not useful

apiclient/apiclient.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ page:
142142
}
143143

144144
publishersResponse = &PublishersPaginated{}
145+
145146
err = json.NewDecoder(res.Body).Decode(&publishersResponse)
146147
if err != nil {
147148
return nil, fmt.Errorf("can't parse GET %s response: %w", reqURL, err)
@@ -175,6 +176,7 @@ page:
175176
if p.AlternativeID != "" {
176177
id = p.AlternativeID
177178
}
179+
178180
publishers = append(publishers, common.Publisher{
179181
ID: id,
180182
Name: fmt.Sprintf("%s %s", p.Description, p.Email),
@@ -259,6 +261,7 @@ func (clt APIClient) PostSoftware(url string, aliases []string, publiccodeYml st
259261
}
260262

261263
postSoftwareResponse := &Software{}
264+
262265
err = json.NewDecoder(res.Body).Decode(&postSoftwareResponse)
263266
if err != nil {
264267
return nil, fmt.Errorf("can't parse POST /software (for %s) response: %w", url, err)

common/publisher.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func LoadPublishers(path string) ([]Publisher, error) {
2525
}
2626

2727
var publishers []Publisher
28+
2829
err = yaml.Unmarshal(data, &publishers)
2930
if err != nil {
3031
return nil, fmt.Errorf("error in parsing `%s': %w", path, err)

crawler/crawler.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Crawler struct {
4545
// NewCrawler initializes a new Crawler object and connects to Elasticsearch (if dryRun == false).
4646
func NewCrawler(dryRun bool) *Crawler {
4747
var c Crawler
48+
4849
const channelSize = 1000
4950

5051
c.DryRun = dryRun
@@ -166,9 +167,11 @@ func (c *Crawler) CrawlPublishers(publishers []common.Publisher) error {
166167
// with a valid publiccode.yml to the repositories channel.
167168
func (c *Crawler) ScanPublisher(publisher common.Publisher) {
168169
log.Infof("Processing publisher: %s", publisher.Name)
170+
169171
defer c.publishersWg.Done()
170172

171173
var err error
174+
172175
for _, u := range publisher.Organizations {
173176
orgURL := (url.URL)(u)
174177

@@ -186,6 +189,7 @@ func (c *Crawler) ScanPublisher(publisher common.Publisher) {
186189
u.String(),
187190
)
188191
}
192+
189193
if err != nil {
190194
if errors.Is(err, scanner.ErrPubliccodeNotFound) {
191195
log.Warnf("[%s] %s", orgURL.String(), err.Error())
@@ -316,6 +320,7 @@ func (c *Crawler) ProcessRepo(repository common.Repository) { //nolint:maintidx
316320
}
317321

318322
var parser *publiccode.Parser
323+
319324
parser, err = publiccode.NewParser(publiccode.ParserConfig{Domain: domain})
320325
if err != nil {
321326
logEntries = append(
@@ -356,18 +361,22 @@ func (c *Crawler) ProcessRepo(repository common.Repository) { //nolint:maintidx
356361

357362
if !valid {
358363
logEntries = append(logEntries, fmt.Sprintf("[%s] BAD publiccode.yml: %+v\n", repository.Name, err))
364+
359365
metrics.GetCounter("repository_bad_publiccodeyml", c.Index).Inc()
360366

361367
return
362368
}
369+
363370
logEntries = append(logEntries, fmt.Sprintf("[%s] GOOD publiccode.yml\n", repository.Name))
371+
364372
metrics.GetCounter("repository_good_publiccodeyml", c.Index).Inc()
365373

366374
if c.DryRun {
367375
log.Infof("[%s]: Skipping other steps (--dry-run)", repository.Name)
368376
}
369377

370378
var aliases []string
379+
371380
url := repository.CanonicalURL.String()
372381

373382
// If the URL of the repo we have is different from the canonical URL
@@ -384,9 +393,10 @@ func (c *Crawler) ProcessRepo(repository common.Repository) { //nolint:maintidx
384393
return
385394
}
386395

387-
// New software to add
388396
if software == nil {
397+
// New software to add
389398
metrics.GetCounter("repository_new", c.Index).Inc()
399+
390400
if !c.DryRun {
391401
// Add the software even if publiccode.yml is invalid, setting active to
392402
// false so that we know about the new software and for example
@@ -399,21 +409,24 @@ func (c *Crawler) ProcessRepo(repository common.Repository) { //nolint:maintidx
399409
return
400410
}
401411
}
402-
// Known software
403412
} else {
413+
// Known software
404414
for _, alias := range software.Aliases {
405415
if !slices.Contains(aliases, alias) {
406416
aliases = append(aliases, alias)
407417
}
408418
}
409419

410420
metrics.GetCounter("repository_known", c.Index).Inc()
421+
411422
if !c.DryRun {
412423
_, err = c.apiClient.PatchSoftware(software.ID, url, aliases, string(publiccodeYml))
413424
}
414425
}
426+
415427
if err != nil {
416428
logEntries = append(logEntries, fmt.Sprintf("[%s]: %s", repository.Name, err.Error()))
429+
417430
metrics.GetCounter("repository_upsert_failures", c.Index).Inc()
418431
}
419432

@@ -429,6 +442,7 @@ func (c *Crawler) ProcessRepo(repository common.Repository) { //nolint:maintidx
429442
if viper.IsSet("ACTIVITY_DAYS") {
430443
activityDays = viper.GetInt("ACTIVITY_DAYS")
431444
}
445+
432446
activityIndex, _, err := git.CalculateRepoActivity(repository, activityDays)
433447
if err != nil {
434448
logEntries = append(
@@ -458,6 +472,7 @@ func (c *Crawler) crawl() error {
458472
// Process the repositories in order to retrieve the files.
459473
for i := range numCPUs {
460474
c.repositoriesWg.Add(1)
475+
461476
go func(id int) {
462477
log.Debugf("Starting ProcessRepositories() goroutine (#%d)", id)
463478
c.ProcessRepositories(reposChan)
@@ -467,6 +482,7 @@ func (c *Crawler) crawl() error {
467482
for repo := range c.repositories {
468483
reposChan <- repo
469484
}
485+
470486
close(reposChan)
471487
c.repositoriesWg.Wait()
472488

git/clone_repository.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func CloneRepository(hostname, name, gitURL, index string) error {
1717
if name == "" {
1818
return errors.New("cannot save a file without name")
1919
}
20+
2021
if gitURL == "" {
2122
return errors.New("cannot clone a repository without git URL")
2223
}

git/repo_activity.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func CalculateRepoActivity(repository common.Repository, days int) (float64, map
105105
if repoActivity > 100 {
106106
repoActivity = 100
107107
}
108+
108109
vitalityIndex[i] = repoActivity
109110
}
110111

@@ -132,6 +133,7 @@ func userCommunityLastDays(commits []*object.Commit) float64 {
132133
func activityLastDays(commits []*object.Commit) float64 {
133134
numberCommits := float64(len(commits))
134135
numberMerges := 0
136+
135137
for _, c := range commits {
136138
if c.NumParents() > 1 {
137139
numberMerges++
@@ -154,6 +156,7 @@ func extractAllTagsCommit(r *git.Repository) ([]*object.Commit, error) {
154156
if err != nil {
155157
return nil, err
156158
}
159+
157160
err = tagrefs.ForEach(func(t *plumbing.Reference) error {
158161
if !t.Hash().IsZero() {
159162
tagObject, _ := r.CommitObject(t.Hash())
@@ -179,6 +182,7 @@ func extractAllCommits(r *git.Repository) ([]*object.Commit, error) {
179182

180183
return nil, err
181184
}
185+
182186
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
183187
if err != nil {
184188
log.Error(err)
@@ -205,18 +209,21 @@ func calculateLongevityIndex(r *git.Repository) (float64, error) {
205209

206210
return 0, err
207211
}
212+
208213
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
209214
if err != nil {
210215
log.Error(err)
211216

212217
return 0, err
213218
}
219+
214220
creationDate := extractOldestCommitDate(cIter)
215221

216222
age := time.Since(creationDate).Hours() / 24
217223

218224
// Git was invented in 2005. If some repo starts before, remove.
219225
then := time.Date(2005, time.January, 1, 1, 0, 0, 0, time.UTC)
226+
220227
duration := time.Since(then).Hours()
221228
if age > duration/24 {
222229
return -1, errors.New("first commit is too old. Must be after the creation of git (2005)")
@@ -306,6 +313,7 @@ func extractCommitsPerDay(days int, commits []*object.Commit) map[int][]*object.
306313
// extractTagsPerDay returns a map of #[days] commits where a tag is created.
307314
func extractTagsPerDay(days int, tags []*object.Commit) map[int][]*object.Commit {
308315
tagsPerDays := map[int][]*object.Commit{}
316+
309317
for i := range days {
310318
lastDays := time.Now().AddDate(0, 0, -i)
311319
// Append all the commits created before lastDays date.

internal/url.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
1717
if err != nil {
1818
return err
1919
}
20+
2021
*u = (URL)(*urlp)
2122

2223
return nil

scanner/bitbucket.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ func (scanner BitBucketScanner) ScanGroupOfRepos(
5454
Ref: r.Mainbranch.Name,
5555
Path: "publiccode.yml",
5656
}
57+
5758
res, err := scanner.client.Repositories.Repository.GetFileContent(opt)
5859
if err != nil {
5960
log.Infof("[%s]: no publiccode.yml: %s", r.Full_name, err.Error())
6061

6162
continue
6263
}
64+
6365
if res != nil {
6466
u, err := url.Parse(fmt.Sprintf("https://bitbucket.org/%s/%s.git", owner, r.Slug))
6567
if err != nil {
@@ -110,10 +112,12 @@ func (scanner BitBucketScanner) ScanRepo(
110112
Ref: "HEAD",
111113
Path: "publiccode.yml",
112114
}
115+
113116
res, err := scanner.client.Repositories.Repository.GetFileContent(filesOpt)
114117
if err != nil {
115118
return fmt.Errorf("[%s]: no publiccode.yml: %w", url.String(), err)
116119
}
120+
117121
if res != nil {
118122
canonicalURL, err := url.Parse(fmt.Sprintf("https://bitbucket.org/%s/%s.git", owner, repo.Slug))
119123
if err != nil {

scanner/github.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func (scanner GitHubScanner) ScanGroupOfRepos(
6161
for {
6262
Retry:
6363
repos, resp, err := scanner.client.Repositories.ListByOrg(scanner.ctx, orgName, opt)
64+
6465
var rateLimitError *github.RateLimitError
6566
if errors.As(err, &rateLimitError) {
6667
log.Infof("GitHub rate limit hit, sleeping until %s", resp.Rate.Reset.Time.String())
@@ -75,6 +76,7 @@ func (scanner GitHubScanner) ScanGroupOfRepos(
7576

7677
goto Retry
7778
}
79+
7880
if err != nil {
7981
// Try to list repos by user, for backwards compatibility.
8082
log.Warnf(
@@ -157,6 +159,7 @@ Retry:
157159

158160
goto Retry
159161
}
162+
160163
if err != nil {
161164
return fmt.Errorf("can't get repo %s: %w", url.String(), err)
162165
}
@@ -172,6 +175,7 @@ Retry:
172175

173176
goto Retry
174177
}
178+
175179
if errors.As(err, &abuseRateLimitError) {
176180
secondaryRateLimit(abuseRateLimitError)
177181

scanner/gitlab.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func (scanner GitLabScanner) ScanGroupOfRepos(
2525
log.Debugf("GitLabScanner.ScanGroupOfRepos(%s)", url.String())
2626

2727
apiURL, _ := url.Parse("/api/v4")
28+
2829
git, err := gitlab.NewClient(os.Getenv("GITLAB_TOKEN"), gitlab.WithBaseURL(apiURL.String()))
2930
if err != nil {
3031
return err
@@ -51,6 +52,7 @@ func (scanner GitLabScanner) ScanGroupOfRepos(
5152
if err != nil {
5253
return err
5354
}
55+
5456
for _, prj := range projects {
5557
if err = addProject(nil, *prj, publisher, repositories); err != nil {
5658
return err
@@ -60,6 +62,7 @@ func (scanner GitLabScanner) ScanGroupOfRepos(
6062
if res.NextPage == 0 {
6163
break
6264
}
65+
6366
opts.Page = res.NextPage
6467
}
6568
}
@@ -74,12 +77,14 @@ func (scanner GitLabScanner) ScanRepo(
7477
log.Debugf("GitLabScanner.ScanRepo(%s)", url.String())
7578

7679
apiURL, _ := url.Parse("/api/v4")
80+
7781
git, err := gitlab.NewClient(os.Getenv("GITLAB_TOKEN"), gitlab.WithBaseURL(apiURL.String()))
7882
if err != nil {
7983
return err
8084
}
8185

8286
projectName := strings.Trim(url.Path, "/")
87+
8388
prj, _, err := git.Projects.GetProject(projectName, &gitlab.GetProjectOptions{})
8489
if err != nil {
8590
return err
@@ -105,6 +110,7 @@ func generateGitlabRawURL(baseURL, defaultBranch string) (string, error) {
105110
if err != nil {
106111
return "", err
107112
}
113+
108114
u.Path = path.Join(u.Path, "raw", defaultBranch, "publiccode.yml")
109115

110116
return u.String(), err
@@ -124,6 +130,7 @@ func addGroupProjects(
124130
if err != nil {
125131
return err
126132
}
133+
127134
for _, prj := range projects {
128135
err = addProject(nil, *prj, publisher, repositories)
129136
if err != nil {
@@ -134,17 +141,20 @@ func addGroupProjects(
134141
if res.NextPage == 0 {
135142
break
136143
}
144+
137145
opts.Page = res.NextPage
138146
}
139147

140148
dgOpts := &gitlab.ListDescendantGroupsOptions{
141149
ListOptions: gitlab.ListOptions{Page: 1},
142150
}
151+
143152
for {
144153
groups, res, err := client.Groups.ListDescendantGroups(group.ID, dgOpts)
145154
if err != nil {
146155
return err
147156
}
157+
148158
for _, g := range groups {
149159
err = addGroupProjects(*g, publisher, repositories, client)
150160
if err != nil {
@@ -155,6 +165,7 @@ func addGroupProjects(
155165
if res.NextPage == 0 {
156166
break
157167
}
168+
158169
dgOpts.Page = res.NextPage
159170
}
160171

@@ -176,6 +187,7 @@ func addProject(
176187
if err != nil {
177188
return fmt.Errorf("failed to get canonical repo URL for %s: %w", project.WebURL, err)
178189
}
190+
179191
if originalURL == nil {
180192
originalURL = canonicalURL
181193
}

0 commit comments

Comments
 (0)