From 68b29a0a0e232c9fb52d70fe97e753afea478ea5 Mon Sep 17 00:00:00 2001 From: Christoph Witzko <github@christophwitzko.com> Date: Fri, 13 Jan 2023 14:55:32 +0100 Subject: [PATCH] style: add .golangci.yaml --- .golangci.yaml | 25 +++++++++++++ pkg/provider/gitlab.go | 12 +++---- pkg/provider/gitlab_test.go | 71 +++++++++++++++++++------------------ 3 files changed, 67 insertions(+), 41 deletions(-) create mode 100644 .golangci.yaml diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..f5c0478 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,25 @@ +linters: + enable: + - errorlint + - forbidigo + - gochecknoinits + - gocritic + - goconst + - gocyclo + - gofumpt + - goimports + - misspell + - revive + - unconvert + - unparam + - wastedassign + +linters-settings: + gocyclo: + min-complexity: 12 + gofumpt: + extra-rules: true + govet: + enable-all: true + disable: + - fieldalignment diff --git a/pkg/provider/gitlab.go b/pkg/provider/gitlab.go index c1ef410..f08ab8b 100644 --- a/pkg/provider/gitlab.go +++ b/pkg/provider/gitlab.go @@ -23,9 +23,9 @@ type GitLabRepository struct { } func (repo *GitLabRepository) Init(config map[string]string) error { - gitlabBaseUrl := config["gitlab_baseurl"] - if gitlabBaseUrl == "" { - gitlabBaseUrl = os.Getenv("CI_SERVER_URL") + gitlabBaseURL := config["gitlab_baseurl"] + if gitlabBaseURL == "" { + gitlabBaseURL = os.Getenv("CI_SERVER_URL") } token := config["token"] @@ -61,8 +61,8 @@ func (repo *GitLabRepository) Init(config map[string]string) error { repo.branch = branch var client *gitlab.Client - if gitlabBaseUrl != "" { - client, err = gitlab.NewClient(token, gitlab.WithBaseURL(gitlabBaseUrl)) + if gitlabBaseURL != "" { + client, err = gitlab.NewClient(token, gitlab.WithBaseURL(gitlabBaseURL)) } else { client, err = gitlab.NewClient(token) } @@ -77,7 +77,6 @@ func (repo *GitLabRepository) Init(config map[string]string) error { func (repo *GitLabRepository) GetInfo() (*provider.RepositoryInfo, error) { project, _, err := repo.client.Projects.GetProject(repo.projectID, nil) - if err != nil { return nil, err } @@ -103,7 +102,6 @@ func (repo *GitLabRepository) GetCommits(fromSha, toSha string) ([]*semrel.RawCo for { commits, resp, err := repo.client.Commits.ListCommits(repo.projectID, opts) - if err != nil { return nil, err } diff --git a/pkg/provider/gitlab_test.go b/pkg/provider/gitlab_test.go index c5dd9d7..0c4cd8c 100644 --- a/pkg/provider/gitlab_test.go +++ b/pkg/provider/gitlab_test.go @@ -49,37 +49,40 @@ func createGitlabCommit(sha, message string) *gitlab.Commit { return &gitlab.Commit{ID: sha, Message: message} } -func createGitlabTag(name, sha string) *gitlab.Tag { +var testSHA = "deadbeef" + +func createGitlabTag(name string) *gitlab.Tag { return &gitlab.Tag{Name: name, Commit: &gitlab.Commit{ - ID: sha, + ID: testSHA, }} } var ( - GITLAB_PROJECT_ID = 12324322 - GITLAB_DEFAULTBRANCH = "master" - GITLAB_PROJECT = gitlab.Project{DefaultBranch: GITLAB_DEFAULTBRANCH, Visibility: gitlab.PrivateVisibility, ID: GITLAB_PROJECT_ID} - GITLAB_COMMITS = []*gitlab.Commit{ + gitlabProjectID = 12324322 + gitlabDefaultBranch = "master" + gitlabProjects = gitlab.Project{DefaultBranch: gitlabDefaultBranch, Visibility: gitlab.PrivateVisibility, ID: gitlabProjectID} + gitlabCommits = []*gitlab.Commit{ createGitlabCommit("abcd", "feat(app): new feature"), createGitlabCommit("dcba", "Fix: bug"), createGitlabCommit("cdba", "Initial commit"), createGitlabCommit("efcd", "chore: break\nBREAKING CHANGE: breaks everything"), } - GITLAB_TAGS = []*gitlab.Tag{ - createGitlabTag("test-tag", "deadbeef"), - createGitlabTag("v1.0.0", "deadbeef"), - createGitlabTag("v2.0.0", "deadbeef"), - createGitlabTag("v2.1.0-beta", "deadbeef"), - createGitlabTag("v3.0.0-beta.2", "deadbeef"), - createGitlabTag("v3.0.0-beta.1", "deadbeef"), - createGitlabTag("2020.04.19", "deadbeef"), + gitlabTags = []*gitlab.Tag{ + createGitlabTag("test-tag"), + createGitlabTag("v1.0.0"), + createGitlabTag("v2.0.0"), + createGitlabTag("v2.1.0-beta"), + createGitlabTag("v3.0.0-beta.2"), + createGitlabTag("v3.0.0-beta.1"), + createGitlabTag("2020.04.19"), } ) //nolint:errcheck +//gocyclo:ignore func GitlabHandler(w http.ResponseWriter, r *http.Request) { // Rate Limit headers - if r.Method == "GET" && r.URL.Path == "/api/v4/" { + if r.Method == http.MethodGet && r.URL.Path == "/api/v4/" { json.NewEncoder(w).Encode(struct{}{}) return } @@ -89,22 +92,22 @@ func GitlabHandler(w http.ResponseWriter, r *http.Request) { return } - if r.Method == "GET" && r.URL.Path == fmt.Sprintf("/api/v4/projects/%d", GITLAB_PROJECT_ID) { - json.NewEncoder(w).Encode(GITLAB_PROJECT) + if r.Method == http.MethodGet && r.URL.Path == fmt.Sprintf("/api/v4/projects/%d", gitlabProjectID) { + json.NewEncoder(w).Encode(gitlabProjects) return } - if r.Method == "GET" && r.URL.Path == fmt.Sprintf("/api/v4/projects/%d/repository/commits", GITLAB_PROJECT_ID) { - json.NewEncoder(w).Encode(GITLAB_COMMITS) + if r.Method == http.MethodGet && r.URL.Path == fmt.Sprintf("/api/v4/projects/%d/repository/commits", gitlabProjectID) { + json.NewEncoder(w).Encode(gitlabCommits) return } - if r.Method == "GET" && r.URL.Path == fmt.Sprintf("/api/v4/projects/%d/repository/tags", GITLAB_PROJECT_ID) { - json.NewEncoder(w).Encode(GITLAB_TAGS) + if r.Method == http.MethodGet && r.URL.Path == fmt.Sprintf("/api/v4/projects/%d/repository/tags", gitlabProjectID) { + json.NewEncoder(w).Encode(gitlabTags) return } - if r.Method == "POST" && r.URL.Path == fmt.Sprintf("/api/v4/projects/%d/releases", GITLAB_PROJECT_ID) { + if r.Method == http.MethodPost && r.URL.Path == fmt.Sprintf("/api/v4/projects/%d/releases", gitlabProjectID) { var data map[string]string json.NewDecoder(r.Body).Decode(&data) r.Body.Close() @@ -128,7 +131,7 @@ func getNewGitlabTestRepo(t *testing.T) (*GitLabRepository, *httptest.Server) { "gitlab_baseurl": ts.URL, "token": "gitlab-examples-ci", "gitlab_branch": "", - "gitlab_projectid": strconv.Itoa(GITLAB_PROJECT_ID), + "gitlab_projectid": strconv.Itoa(gitlabProjectID), }) require.NoError(t, err) @@ -140,7 +143,7 @@ func TestGitlabGetInfo(t *testing.T) { defer ts.Close() repoInfo, err := repo.GetInfo() require.NoError(t, err) - require.Equal(t, GITLAB_DEFAULTBRANCH, repoInfo.DefaultBranch) + require.Equal(t, gitlabDefaultBranch, repoInfo.DefaultBranch) require.True(t, repoInfo.Private) } @@ -152,8 +155,8 @@ func TestGitlabGetCommits(t *testing.T) { require.Len(t, commits, 4) for i, c := range commits { - require.Equal(t, c.SHA, GITLAB_COMMITS[i].ID) - require.Equal(t, c.RawMessage, GITLAB_COMMITS[i].Message) + require.Equal(t, c.SHA, gitlabCommits[i].ID) + require.Equal(t, c.RawMessage, gitlabCommits[i].Message) } } @@ -167,11 +170,11 @@ func TestGitlabGetReleases(t *testing.T) { expectedSHA string expectedVersion string }{ - {"", "", "deadbeef", "2020.4.19"}, - {"", "^v[0-9]*", "deadbeef", "2.0.0"}, - {"2-beta", "", "deadbeef", "2.1.0-beta"}, - {"3-beta", "", "deadbeef", "3.0.0-beta.2"}, - {"4-beta", "", "deadbeef", "4.0.0-beta"}, + {"", "", testSHA, "2020.4.19"}, + {"", "^v[0-9]*", testSHA, "2.0.0"}, + {"2-beta", "", testSHA, "2.1.0-beta"}, + {"3-beta", "", testSHA, "3.0.0-beta.2"}, + {"4-beta", "", testSHA, "4.0.0-beta"}, } for _, tc := range testCases { @@ -189,7 +192,7 @@ func TestGitlabGetReleases(t *testing.T) { func TestGitlabCreateRelease(t *testing.T) { repo, ts := getNewGitlabTestRepo(t) defer ts.Close() - err := repo.CreateRelease(&provider.CreateReleaseConfig{NewVersion: "2.0.0", SHA: "deadbeef"}) + err := repo.CreateRelease(&provider.CreateReleaseConfig{NewVersion: "2.0.0", SHA: testSHA}) require.NoError(t, err) } @@ -202,12 +205,12 @@ func TestGitlabStripVTagRelease(t *testing.T) { "gitlab_baseurl": ts.URL, "token": "gitlab-examples-ci", "gitlab_branch": "", - "gitlab_projectid": strconv.Itoa(GITLAB_PROJECT_ID), + "gitlab_projectid": strconv.Itoa(gitlabProjectID), "strip_v_tag_prefix": "true", }) require.NoError(t, err) - err = repo.CreateRelease(&provider.CreateReleaseConfig{NewVersion: "2.0.0", SHA: "deadbeef"}) + err = repo.CreateRelease(&provider.CreateReleaseConfig{NewVersion: "2.0.0", SHA: testSHA}) require.NoError(t, err) } -- GitLab