Skip to content

Commit

Permalink
refactor: enable revive linter (#364)
Browse files Browse the repository at this point in the history
See #331.
  • Loading branch information
bfabio authored Sep 22, 2023
1 parent a8a2964 commit 00ee890
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ linters:
- predeclared
- promlinter
- reassign
# - revive
- revive
- rowserrcheck
- sqlclosecheck
# - stylecheck
Expand Down
48 changes: 24 additions & 24 deletions apiclient/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/spf13/viper"
)

type ApiClient struct {
type APIClient struct {
baseURL string
retryableClient *http.Client
token string
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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)
}
Expand All @@ -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,
Expand All @@ -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)
}
Expand All @@ -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{}{
Expand All @@ -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)
}
Expand All @@ -276,15 +276,15 @@ 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,
})
if err != nil {
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)
}
Expand All @@ -298,15 +298,15 @@ 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,
})
if err != nil {
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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/download_publishers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)},
})
}
Expand Down
2 changes: 1 addition & 1 deletion common/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
12 changes: 6 additions & 6 deletions crawler/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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,
)
Expand Down
6 changes: 1 addition & 5 deletions scanner/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 00ee890

Please sign in to comment.