From 00ee890cc581c23a433408ce4b61090d7e7ece4a Mon Sep 17 00:00:00 2001 From: Fabio Bonelli Date: Fri, 22 Sep 2023 14:55:38 +0200 Subject: [PATCH] refactor: enable revive linter (#364) See #331. --- .golangci.yaml | 2 +- apiclient/apiclient.go | 48 +++++++++++++++++++------------------- cmd/download_publishers.go | 4 ++-- common/publisher.go | 2 +- crawler/crawler.go | 12 +++++----- scanner/gitlab.go | 6 +---- 6 files changed, 35 insertions(+), 39 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 0b92b2a..cb50c55 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -131,7 +131,7 @@ linters: - predeclared - promlinter - reassign - # - revive + - revive - rowserrcheck - sqlclosecheck # - stylecheck diff --git a/apiclient/apiclient.go b/apiclient/apiclient.go index da5e477..86948d7 100644 --- a/apiclient/apiclient.go +++ b/apiclient/apiclient.go @@ -17,7 +17,7 @@ import ( "github.com/spf13/viper" ) -type ApiClient struct { +type APIClient struct { baseURL string retryableClient *http.Client token string @@ -66,17 +66,17 @@ type Software struct { UpdatedAt time.Time `json:"updatedAt"` } -func NewClient() ApiClient { +func NewClient() APIClient { retryableClient := retryablehttp.NewClient().StandardClient() - return ApiClient{ + return APIClient{ baseURL: viper.GetString("API_BASEURL"), retryableClient: retryableClient, token: "Bearer " + viper.GetString("API_BEARER_TOKEN"), } } -func (clt ApiClient) Get(url string) (*http.Response, error) { +func (clt APIClient) Get(url string) (*http.Response, error) { req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil) if err != nil { return nil, err @@ -85,7 +85,7 @@ func (clt ApiClient) Get(url string) (*http.Response, error) { return clt.retryableClient.Do(req) } -func (clt ApiClient) Post(url string, body []byte) (*http.Response, error) { +func (clt APIClient) Post(url string, body []byte) (*http.Response, error) { req, err := http.NewRequestWithContext( context.Background(), http.MethodPost, @@ -102,7 +102,7 @@ func (clt ApiClient) Post(url string, body []byte) (*http.Response, error) { return clt.retryableClient.Do(req) } -func (clt ApiClient) Patch(url string, body []byte) (*http.Response, error) { +func (clt APIClient) Patch(url string, body []byte) (*http.Response, error) { req, err := http.NewRequestWithContext( context.Background(), http.MethodPatch, @@ -121,30 +121,30 @@ func (clt ApiClient) Patch(url string, body []byte) (*http.Response, error) { // GetPublishers returns a slice with all the publishers from the API and // any error encountered. -func (c ApiClient) GetPublishers() ([]common.Publisher, error) { +func (clt APIClient) GetPublishers() ([]common.Publisher, error) { var publishersResponse *PublishersPaginated pageAfter := "" publishers := make([]common.Publisher, 0, 25) page: - reqUrl := joinPath(c.baseURL, "/publishers") + pageAfter + reqURL := joinPath(clt.baseURL, "/publishers") + pageAfter - res, err := c.Get(reqUrl) + res, err := clt.Get(reqURL) if err != nil { - return nil, fmt.Errorf("can't get publishers %s: %w", reqUrl, err) + return nil, fmt.Errorf("can't get publishers %s: %w", reqURL, err) } defer res.Body.Close() if res.StatusCode < 200 || res.StatusCode > 299 { - return nil, fmt.Errorf("can't get publishers %s: HTTP status %s", reqUrl, res.Status) + return nil, fmt.Errorf("can't get publishers %s: HTTP status %s", reqURL, res.Status) } publishersResponse = &PublishersPaginated{} err = json.NewDecoder(res.Body).Decode(&publishersResponse) if err != nil { - return nil, fmt.Errorf("can't parse GET %s response: %w", reqUrl, err) + return nil, fmt.Errorf("can't parse GET %s response: %w", reqURL, err) } for _, p := range publishersResponse.Data { @@ -153,7 +153,7 @@ page: for _, codeHosting := range p.CodeHostings { u, err := url.Parse(codeHosting.URL) if err != nil { - return nil, fmt.Errorf("can't parse GET %s response: %w", reqUrl, err) + return nil, fmt.Errorf("can't parse GET %s response: %w", reqURL, err) } if codeHosting.Group { @@ -176,7 +176,7 @@ page: id = p.ID } publishers = append(publishers, common.Publisher{ - Id: id, + ID: id, Name: fmt.Sprintf("%s %s", p.Description, p.Email), Organizations: groups, Repositories: repos, @@ -195,10 +195,10 @@ page: // GetSoftwareByURL returns the software matching the given repo URL and // any error encountered. // In case no software is found and no error occours, (nil, nil) is returned. -func (c ApiClient) GetSoftwareByURL(url string) (*Software, error) { +func (clt APIClient) GetSoftwareByURL(url string) (*Software, error) { var softwareResponse SoftwarePaginated - res, err := c.retryableClient.Get(joinPath(c.baseURL, "/software") + "?url=" + url) + res, err := clt.retryableClient.Get(joinPath(clt.baseURL, "/software") + "?url=" + url) if err != nil { return nil, fmt.Errorf("can't GET /software?url=%s: %w", url, err) } @@ -219,7 +219,7 @@ func (c ApiClient) GetSoftwareByURL(url string) (*Software, error) { // PostSoftware creates a new software resource with the given fields and returns // a Software struct or any error encountered. -func (c ApiClient) PostSoftware(url string, aliases []string, publiccodeYml string, active bool) (*Software, error) { +func (clt APIClient) PostSoftware(url string, aliases []string, publiccodeYml string, active bool) (*Software, error) { body, err := json.Marshal(map[string]interface{}{ "publiccodeYml": publiccodeYml, "url": url, @@ -230,7 +230,7 @@ func (c ApiClient) PostSoftware(url string, aliases []string, publiccodeYml stri return nil, fmt.Errorf("can't create software: %w", err) } - res, err := c.Post(joinPath(c.baseURL, "/software"), body) + res, err := clt.Post(joinPath(clt.baseURL, "/software"), body) if err != nil { return nil, fmt.Errorf("can't create software: %w", err) } @@ -250,7 +250,7 @@ func (c ApiClient) PostSoftware(url string, aliases []string, publiccodeYml stri // PatchSoftware updates a software resource with the given fields and returns // an http.Response and any error encountered. -func (c ApiClient) PatchSoftware( +func (clt APIClient) PatchSoftware( id string, url string, aliases []string, publiccodeYml string, ) (*http.Response, error) { body, err := json.Marshal(map[string]interface{}{ @@ -262,7 +262,7 @@ func (c ApiClient) PatchSoftware( return nil, fmt.Errorf("can't update software: %w", err) } - res, err := c.Patch(joinPath(c.baseURL, "/software/"+id), body) + res, err := clt.Patch(joinPath(clt.baseURL, "/software/"+id), body) if err != nil { return res, fmt.Errorf("can't update software: %w", err) } @@ -276,7 +276,7 @@ func (c ApiClient) PatchSoftware( // PostSoftwareLog creates a new software log with the given fields and returns // an http.Response and any error encountered. -func (c ApiClient) PostSoftwareLog(softwareId string, message string) (*http.Response, error) { +func (clt APIClient) PostSoftwareLog(softwareID string, message string) (*http.Response, error) { payload, err := json.Marshal(map[string]interface{}{ "message": message, }) @@ -284,7 +284,7 @@ func (c ApiClient) PostSoftwareLog(softwareId string, message string) (*http.Res return nil, fmt.Errorf("can't create log: %w", err) } - res, err := c.Post(joinPath(c.baseURL, "/software/", softwareId, "logs"), payload) + res, err := clt.Post(joinPath(clt.baseURL, "/software/", softwareID, "logs"), payload) if err != nil { return res, fmt.Errorf("can't create software log: %w", err) } @@ -298,7 +298,7 @@ func (c ApiClient) PostSoftwareLog(softwareId string, message string) (*http.Res // PostLog creates a new log with the given message and returns an http.Response // and any error encountered. -func (c ApiClient) PostLog(message string) (*http.Response, error) { +func (clt APIClient) PostLog(message string) (*http.Response, error) { payload, err := json.Marshal(map[string]interface{}{ "message": message, }) @@ -306,7 +306,7 @@ func (c ApiClient) PostLog(message string) (*http.Response, error) { return nil, fmt.Errorf("can't create log: %w", err) } - res, err := c.Post(joinPath(c.baseURL, "/logs"), payload) + res, err := clt.Post(joinPath(clt.baseURL, "/logs"), payload) if err != nil { return res, fmt.Errorf("can't create log: %w", err) } diff --git a/cmd/download_publishers.go b/cmd/download_publishers.go index 7a1e9f1..9fbd358 100644 --- a/cmd/download_publishers.go +++ b/cmd/download_publishers.go @@ -59,7 +59,7 @@ var downloadPublishersCmd = &cobra.Command{ REPOLIST: for _, i := range repolist.Registrati { for idx, publisher := range publishers { - if publisher.Id == i.IPA { + if publisher.ID == i.IPA { u, _ := url.Parse(i.URL) // If this Id is already known, append this URL to the existing item publishers[idx].Organizations = append(publisher.Organizations, (ymlurl.URL)(*u)) @@ -72,7 +72,7 @@ var downloadPublishersCmd = &cobra.Command{ // If this IPA code is not known, append a new publisher item publishers = append(publishers, common.Publisher{ Name: i.IPA, - Id: i.IPA, + ID: i.IPA, Organizations: []ymlurl.URL{(ymlurl.URL)(*u)}, }) } diff --git a/common/publisher.go b/common/publisher.go index 92fb53e..7c2776e 100644 --- a/common/publisher.go +++ b/common/publisher.go @@ -11,7 +11,7 @@ import ( var fileReaderInject = ioutil.ReadFile type Publisher struct { - Id string `yaml:"id"` + ID string `yaml:"id"` Name string `yaml:"name"` Organizations []url.URL `yaml:"orgs"` Repositories []url.URL `yaml:"repos"` diff --git a/crawler/crawler.go b/crawler/crawler.go index 7a91d9b..9bbf73d 100644 --- a/crawler/crawler.go +++ b/crawler/crawler.go @@ -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). @@ -358,7 +358,7 @@ func (c *Crawler) ProcessRepo(repository common.Repository) { //nolint:maintidx } publisherID := viper.GetString("MAIN_PUBLISHER_ID") - if valid && repository.Publisher.Id != publisherID { + if valid && repository.Publisher.ID != publisherID { err = validateFile(repository.Publisher, *parser, repository.FileRawURL) if err != nil { valid = false @@ -435,7 +435,7 @@ func (c *Crawler) ProcessRepo(repository common.Repository) { //nolint:maintidx } // Calculate Repository activity index and vitality. Defaults to 60 days. - var activityDays int = 60 + activityDays := 60 if viper.IsSet("ACTIVITY_DAYS") { activityDays = viper.GetInt("ACTIVITY_DAYS") } @@ -486,16 +486,16 @@ func validateFile(publisher common.Publisher, parser publiccode.Parser, fileRawU // //nolint:godox // TODO: This is not ideal and also an Italian-specific check // (https://github.com/italia/publiccode-crawler/issues/298) - idIsUUID, _ := regexp.MatchString("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", publisher.Id) + idIsUUID, _ := regexp.MatchString("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", publisher.ID) if !idIsUUID && !strings.EqualFold( - strings.TrimSpace(publisher.Id), + strings.TrimSpace(publisher.ID), strings.TrimSpace(parser.PublicCode.It.Riuso.CodiceIPA), ) { return fmt.Errorf( "codiceIPA is '%s', but '%s' was expected for '%s' in %s", parser.PublicCode.It.Riuso.CodiceIPA, - publisher.Id, + publisher.ID, publisher.Name, fileRawURL, ) diff --git a/scanner/gitlab.go b/scanner/gitlab.go index 7dc8ae8..698f72c 100644 --- a/scanner/gitlab.go +++ b/scanner/gitlab.go @@ -80,11 +80,7 @@ func (scanner GitLabScanner) ScanRepo( return err } - if err = addProject(&url, *prj, publisher, repositories); err != nil { - return err - } - - return nil + return addProject(&url, *prj, publisher, repositories) } // isGitlabGroup returns true if the API URL points to a group.