Skip to content

Commit

Permalink
chore(golangci-lint): enable gofumpt and gofmt (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabio authored Mar 1, 2023
1 parent c4c6f9e commit 6ae4769
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 86 deletions.
4 changes: 2 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ linters:
# - godot
# - godox
# - goerr113
# - gofmt
# - gofumpt
- gofmt
- gofumpt
- goheader
# - goimports
# - gomnd
Expand Down
56 changes: 28 additions & 28 deletions apiclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
)

type ApiClient struct {
baseURL string
retryableClient *http.Client
token string
baseURL string
retryableClient *http.Client
token string
}

type Links struct {
Expand All @@ -29,31 +29,31 @@ type Links struct {
}

type PublishersPaginated struct {
Data []Publisher `json:"data"`
Links Links `json:"links"`
Data []Publisher `json:"data"`
Links Links `json:"links"`
}

type SoftwarePaginated struct {
Data []Software `json:"data"`
Data []Software `json:"data"`
Links Links `json:"links"`
}

type Publisher struct {
ID string `json:"id"`
AlternativeID string `json:"alternativeId"`
Email string `json:"email"`
Description string `json:"description"`
CodeHostings []CodeHosting `json:"codeHosting"`
Active bool `json:"active"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ID string `json:"id"`
AlternativeID string `json:"alternativeId"`
Email string `json:"email"`
Description string `json:"description"`
CodeHostings []CodeHosting `json:"codeHosting"`
Active bool `json:"active"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

type CodeHosting struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Group bool `json:"group"`
URL string `json:"url"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Group bool `json:"group"`
URL string `json:"url"`
}

type Software struct {
Expand All @@ -70,9 +70,9 @@ func NewClient() ApiClient {
retryableClient := retryablehttp.NewClient().StandardClient()

return ApiClient{
baseURL: viper.GetString("API_BASEURL"),
baseURL: viper.GetString("API_BASEURL"),
retryableClient: retryableClient,
token: "Bearer " + viper.GetString("API_BEARER_TOKEN"),
token: "Bearer " + viper.GetString("API_BEARER_TOKEN"),
}
}

Expand Down Expand Up @@ -175,10 +175,10 @@ page:
id = p.ID
}
publishers = append(publishers, common.Publisher{
Id: id,
Name: fmt.Sprintf("%s %s", p.Description, p.Email),
Id: id,
Name: fmt.Sprintf("%s %s", p.Description, p.Email),
Organizations: groups,
Repositories: repos,
Repositories: repos,
})
}

Expand Down Expand Up @@ -218,8 +218,8 @@ func (c ApiClient) GetSoftwareByURL(url string) (*Software, error) {
func (c ApiClient) PostSoftware(url string, aliases []string, publiccodeYml string) (*http.Response, error) {
body, err := json.Marshal(map[string]interface{}{
"publiccodeYml": publiccodeYml,
"url": url,
"aliases": aliases,
"url": url,
"aliases": aliases,
})
if err != nil {
return nil, fmt.Errorf("can't create software: %w", err)
Expand All @@ -242,14 +242,14 @@ func (c ApiClient) PostSoftware(url string, aliases []string, publiccodeYml stri
func (c ApiClient) PatchSoftware(id string, url string, aliases []string, publiccodeYml string) (*http.Response, error) {
body, err := json.Marshal(map[string]interface{}{
"publiccodeYml": publiccodeYml,
"url": url,
"aliases": aliases,
"url": url,
"aliases": aliases,
})
if err != nil {
return nil, fmt.Errorf("can't update software: %w", err)
}

res, err := c.Patch(joinPath(c.baseURL, "/software/" + id), body)
res, err := c.Patch(joinPath(c.baseURL, "/software/"+id), body)
if err != nil {
return res, fmt.Errorf("can't update software: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func init() {
var crawlCmd = &cobra.Command{
Use: "crawl publishers.yml [directory/*.yml ...]",
Short: "Crawl publiccode.yml files in publishers' repos.",
Long: `Crawl publiccode.yml files in publishers' repos.
Long: `Crawl publiccode.yml files in publishers' repos.
When run with no arguments, the publishers are fetched from the API,
otherwise the passed YAML files are used.`,
Args: cobra.MinimumNArgs(0),
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
c := crawler.NewCrawler(dryRun)

Expand Down
3 changes: 2 additions & 1 deletion cmd/download_publishers.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ var downloadPublishersCmd = &cobra.Command{
if _, err = f.Write(data); err != nil {
log.Fatal(err)
}
}}
},
}
26 changes: 14 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import (
"github.com/spf13/cobra"
)

var dryRun bool
var rootCmd = &cobra.Command{
Use: "crawler",
Short: "A crawler for publiccode.yml files.",
Long: `A fast and robust publiccode.yml file crawler.
var (
dryRun bool
rootCmd = &cobra.Command{
Use: "crawler",
Short: "A crawler for publiccode.yml files.",
Long: `A fast and robust publiccode.yml file crawler.
Complete documentation is available at https://github.com/italia/publiccode-crawler`,
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
if err != nil {
log.Fatal(err)
}
},
}
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
if err != nil {
log.Fatal(err)
}
},
}
)

// Execute is the entrypoint for cmd package Cobra.
func Execute() {
Expand Down
28 changes: 13 additions & 15 deletions crawler/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"sync"

"github.com/alranel/go-vcsurl/v2"
httpclient "github.com/italia/httpclient-lib-go"
"github.com/italia/publiccode-crawler/v3/apiclient"
"github.com/italia/publiccode-crawler/v3/common"
"github.com/italia/publiccode-crawler/v3/git"
"github.com/italia/publiccode-crawler/v3/metrics"
"github.com/italia/publiccode-crawler/v3/scanner"
httpclient "github.com/italia/httpclient-lib-go"
publiccode "github.com/italia/publiccode-parser-go/v3"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand All @@ -28,8 +28,8 @@ import (
type Crawler struct {
DryRun bool

Index string
repositories chan common.Repository
Index string
repositories chan common.Repository
// Sync mutex guard.
publishersWg sync.WaitGroup
repositoriesWg sync.WaitGroup
Expand All @@ -38,7 +38,7 @@ type Crawler struct {
gitLabScanner scanner.Scanner
bitBucketScanner scanner.Scanner

apiClient apiclient.ApiClient
apiClient apiclient.ApiClient
}

// NewCrawler initializes a new Crawler object and connects to Elasticsearch (if dryRun == false).
Expand All @@ -48,7 +48,7 @@ func NewCrawler(dryRun bool) *Crawler {
c.DryRun = dryRun

datadir := viper.GetString("CRAWLER_DATADIR")
if err := os.MkdirAll(datadir, 0744); err != nil {
if err := os.MkdirAll(datadir, 0o744); err != nil {
log.Fatalf("can't create data directory (%s): %s", datadir, err.Error())
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func (c *Crawler) crawl() error {

log.Infof(
"Summary: Total repos scanned: %v. With good publiccode.yml file: %v. With bad publiccode.yml file: %v\n"+
"Repos with good publiccode.yml file: New repos: %v, Known repos: %v, Failures saving to API: %v",
"Repos with good publiccode.yml file: New repos: %v, Known repos: %v, Failures saving to API: %v",
metrics.GetCounterValue("repository_processed", c.Index),
metrics.GetCounterValue("repository_good_publiccodeyml", c.Index),
metrics.GetCounterValue("repository_bad_publiccodeyml", c.Index),
Expand Down Expand Up @@ -192,7 +192,7 @@ func (c *Crawler) ScanPublisher(publisher common.Publisher) {
} else if vcsurl.IsGitLab(&orgURL) {
err = c.gitLabScanner.ScanGroupOfRepos(orgURL, publisher, c.repositories)
} else {
err = fmt.Errorf(
err = fmt.Errorf(
"publisher %s: unsupported code hosting platform for %s",
publisher.Name,
u.String(),
Expand Down Expand Up @@ -244,7 +244,6 @@ func (c *Crawler) ProcessRepositories(repos chan common.Repository) {
}
}


// ProcessRepo looks for a publiccode.yml file in a repository, and if found it processes it.
func (c *Crawler) ProcessRepo(repository common.Repository) {
var logEntries []string
Expand Down Expand Up @@ -302,29 +301,29 @@ func (c *Crawler) ProcessRepo(repository common.Repository) {
logEntries,
fmt.Sprintf(
"[%s] publiccode.yml found at %s\n",
repository.CanonicalURL.String(),
repository.FileRawURL,
repository.CanonicalURL.String(),
repository.FileRawURL,
),
)

var parser *publiccode.Parser
parser, err = publiccode.NewParser(repository.FileRawURL)
if err != nil {
logEntries = append(logEntries,fmt.Sprintf("[%s] BAD publiccode.yml: %s\n", repository.Name, err.Error()))
logEntries = append(logEntries, fmt.Sprintf("[%s] BAD publiccode.yml: %s\n", repository.Name, err.Error()))
metrics.GetCounter("repository_bad_publiccodeyml", c.Index).Inc()

return
}

// FIXME: this is hardcoded for now, because it requires changes to publiccode-parser-go.
domain := publiccode.Domain{
Host: "github.com",
Host: "github.com",
UseTokenFor: []string{"github.com", "api.github.com", "raw.githubusercontent.com"},
BasicAuth: []string{os.Getenv("GITHUB_TOKEN")},
BasicAuth: []string{os.Getenv("GITHUB_TOKEN")},
}

err = parser.ParseInDomain(resp.Body, domain.Host, domain.UseTokenFor, domain.BasicAuth)
if err != nil {
if err != nil {
valid := true
out:
for _, res := range err.(publiccode.ValidationResults) {
Expand Down Expand Up @@ -419,7 +418,6 @@ func (c *Crawler) ProcessRepo(repository common.Repository) {
logEntries = append(logEntries, fmt.Sprintf("[%s] activity index in the last %d days: %f\n", repository.Name, activityDays, activityIndex))
}
}

}

// validateFile performs additional validations that are not strictly mandated
Expand Down
1 change: 0 additions & 1 deletion git/repo_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ func ranges(name string, value float64) float64 {
return r.Points
}
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"regexp"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/client_golang/prometheus/promhttp"
dto "github.com/prometheus/client_model/go"
log "github.com/sirupsen/logrus"
)

Expand All @@ -32,7 +32,7 @@ func GetCounter(name, namespace string) prometheus.Counter {
}

func GetCounterValue(name, namespace string) float64 {
var m = &dto.Metric{}
m := &dto.Metric{}

if err := GetCounter(name, namespace).Write(m); err != nil {
log.Error(err)
Expand Down
25 changes: 12 additions & 13 deletions scanner/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func (scanner BitBucketScanner) ScanGroupOfRepos(url url.URL, publisher common.P
}

res, err := scanner.client.Repositories.ListForAccount(opt)

if err != nil {
return fmt.Errorf("Can't list repositories in %s: %w", url.String(), err)
}
Expand All @@ -44,14 +43,14 @@ func (scanner BitBucketScanner) ScanGroupOfRepos(url url.URL, publisher common.P
continue
}

opt := &bitbucket.RepositoryFilesOptions {
Owner: owner,
opt := &bitbucket.RepositoryFilesOptions{
Owner: owner,
RepoSlug: r.Slug,
Ref: r.Mainbranch.Name,
Path: "publiccode.yml",
Ref: r.Mainbranch.Name,
Path: "publiccode.yml",
}
res, err := scanner.client.Repositories.Repository.GetFileContent(opt)
if (err != nil) {
if err != nil {
log.Infof("[%s]: no publiccode.yml: %s", r.Full_name, err.Error())
continue
}
Expand All @@ -63,7 +62,7 @@ func (scanner BitBucketScanner) ScanGroupOfRepos(url url.URL, publisher common.P

repositories <- common.Repository{
Name: r.Full_name,
FileRawURL: fmt.Sprintf("https://bitbucket.org/%s/%s/raw/%s/publiccode.yml", owner,r.Slug, r.Mainbranch.Name),
FileRawURL: fmt.Sprintf("https://bitbucket.org/%s/%s/raw/%s/publiccode.yml", owner, r.Slug, r.Mainbranch.Name),
URL: *u,
CanonicalURL: *u,
GitBranch: r.Mainbranch.Name,
Expand All @@ -86,22 +85,22 @@ func (scanner BitBucketScanner) ScanRepo(url url.URL, publisher common.Publisher
slug := splitted[1]

opt := &bitbucket.RepositoryOptions{
Owner: owner,
Owner: owner,
RepoSlug: slug,
}

repo , err := scanner.client.Repositories.Repository.Get(opt)
repo, err := scanner.client.Repositories.Repository.Get(opt)
if err != nil {
return err
}

filesOpt := &bitbucket.RepositoryFilesOptions {
Owner: owner,
filesOpt := &bitbucket.RepositoryFilesOptions{
Owner: owner,
RepoSlug: slug,
Path: "publiccode.yml",
Path: "publiccode.yml",
}
res, err := scanner.client.Repositories.Repository.GetFileContent(filesOpt)
if (err != nil) {
if err != nil {
return err
}
if res != nil {
Expand Down
2 changes: 1 addition & 1 deletion scanner/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (scanner GitHubScanner) ScanGroupOfRepos(url url.URL, publisher common.Publ
orgName := splitted[0]

for {
Retry:
Retry:
repos, resp, err := scanner.client.Repositories.ListByOrg(scanner.ctx, orgName, opt)
if _, ok := err.(*github.RateLimitError); ok {
log.Infof("GitHub rate limit hit, sleeping until %s", resp.Rate.Reset.Time.String())
Expand Down
Loading

0 comments on commit 6ae4769

Please sign in to comment.