From 39e9e21313e1eb914a019c7cb77aea395d20e8c6 Mon Sep 17 00:00:00 2001 From: Kai Armstrong Date: Thu, 17 Nov 2022 18:36:09 +0000 Subject: [PATCH] chore: re-add linting/formatting job to ci --- .gitlab-ci.yml | 5 ++- .gitlab/ci/lint.gitlab-ci.yml | 5 ++- .golangci.yml | 18 +++++++-- CONTRIBUTING.md | 40 +++++++++++++------ api/client.go | 3 ++ api/client_test.go | 3 +- api/pipeline.go | 8 ++-- api/repository_files.go | 1 - api/variable.go | 4 +- cmd/gen-docs/docs.go | 14 +++---- commands/alias/alias.go | 2 +- commands/alias/delete/alias_delete.go | 3 +- commands/alias/list/alias_list.go | 2 +- commands/alias/set/alias_set.go | 2 +- commands/alias/set/alias_set_test.go | 14 ++++--- commands/api/api.go | 2 +- commands/api/http.go | 1 - commands/api/http_test.go | 2 +- commands/auth/login/login.go | 1 - commands/auth/status/status_test.go | 1 - commands/ci/artifact/artifact.go | 5 +-- commands/ci/ci.go | 2 +- commands/ci/ci_test.go | 1 - commands/ci/delete/delete.go | 4 +- commands/ci/get/get.go | 9 ++++- commands/ci/legacyci/pipeline_ci.go | 2 +- commands/ci/legacyci/pipeline_ci_test.go | 1 - commands/ci/lint/lint.go | 2 +- commands/ci/list/list.go | 2 +- commands/ci/retry/retry.go | 3 +- commands/ci/run/run.go | 2 +- commands/ci/status/status.go | 4 +- commands/ci/trace/trace.go | 2 +- commands/ci/trace/trace_test.go | 2 - commands/ci/view/view.go | 10 +++-- commands/ci/view/view_test.go | 3 +- commands/cmdtest/helper.go | 2 +- commands/cmdutils/cmdutils.go | 1 - commands/cmdutils/cmdutils_test.go | 5 +-- commands/cmdutils/factory.go | 1 - commands/completion/completion.go | 2 +- commands/config/config.go | 2 +- commands/issue/board/board.go | 2 +- .../issue/board/create/issue_board_create.go | 2 +- .../board/create/issue_board_create_test.go | 1 - commands/issue/board/view/issue_board_view.go | 23 ++++++----- commands/issue/close/issue_close.go | 2 +- commands/issue/close/issue_close_test.go | 3 +- commands/issue/create/issue_create.go | 2 +- commands/issue/create/issue_create_test.go | 3 +- commands/issue/delete/issue_delete.go | 2 +- commands/issue/delete/issue_delete_test.go | 1 - commands/issue/issue.go | 2 +- commands/issue/issueutils/utils.go | 7 ++-- commands/issue/list/issue_list.go | 4 +- commands/issue/list/issue_list_test.go | 2 - commands/issue/note/issue_note_create.go | 2 +- commands/issue/note/issue_note_create_test.go | 3 -- commands/issue/reopen/issue_reopen.go | 3 +- commands/issue/subscribe/issue_subscribe.go | 2 +- .../issue/unsubscribe/issue_unsubscribe.go | 2 +- commands/issue/update/issue_update.go | 2 +- commands/issue/update/issue_update_test.go | 1 - commands/issue/view/issue_view.go | 5 +-- commands/issue/view/issue_view_test.go | 1 - commands/label/create/label_create.go | 3 +- commands/label/label.go | 2 +- commands/label/list/label_list.go | 3 +- commands/mr/approve/mr_approve.go | 4 +- commands/mr/approvers/mr_approvers.go | 2 +- commands/mr/checkout/mr_checkout.go | 7 +--- commands/mr/close/mr_close.go | 2 +- commands/mr/create/mr_create.go | 4 +- commands/mr/create/mr_create_test.go | 3 +- commands/mr/delete/mr_delete.go | 2 +- commands/mr/delete/mr_delete_test.go | 1 - commands/mr/for/mr_for.go | 3 +- commands/mr/issues/mr_issues.go | 2 +- commands/mr/list/mr_list.go | 5 +-- commands/mr/merge/mr_merge.go | 6 +-- commands/mr/mr.go | 2 +- commands/mr/mr_test.go | 1 - commands/mr/mrutils/mrutils_test.go | 1 - commands/mr/note/mr_note_create.go | 2 +- commands/mr/note/mr_note_create_test.go | 4 -- commands/mr/rebase/mr_rebase.go | 4 +- commands/mr/reopen/mr_reopen.go | 2 +- commands/mr/revoke/mr_revoke.go | 2 +- commands/mr/subscribe/mr_subscribe.go | 2 +- commands/mr/subscribe/mr_subscribe_test.go | 3 +- commands/mr/todo/mr_todo.go | 2 +- commands/mr/unsubscribe/mr_unsubscribe.go | 2 +- commands/mr/update/mr_update.go | 2 +- commands/mr/update/mr_update_test.go | 6 ++- commands/mr/view/mr_view.go | 2 +- commands/mr/view/mr_view_test.go | 1 - commands/project/archive/repo_archive.go | 2 +- commands/project/clone/repo_clone.go | 6 +-- commands/project/clone/repo_clone_test.go | 14 +++---- .../project/contributors/repo_contributors.go | 2 +- commands/project/create/project_create.go | 3 +- commands/project/delete/delete.go | 4 +- commands/project/fork/fork.go | 8 ++-- commands/project/list/list.go | 2 +- commands/project/mirror/project_mirror.go | 6 +-- commands/project/repo.go | 2 +- commands/project/repo_test.go | 1 - commands/project/search/project_search.go | 3 +- commands/project/transfer/project_transfer.go | 2 +- commands/project/view/project_view.go | 7 ++-- commands/release/download/download.go | 5 ++- commands/release/list/release_list.go | 3 +- commands/release/list/release_list_test.go | 2 - commands/release/release.go | 2 +- commands/release/releaseutils/releaseutils.go | 3 +- commands/release/view/view.go | 4 +- commands/root.go | 2 +- commands/snippet/create/create.go | 2 +- commands/snippet/snippet.go | 2 +- commands/snippet/snippet_test.go | 1 - commands/ssh-key/get/get.go | 2 +- commands/update/check_update.go | 2 +- commands/user/events/events.go | 1 - commands/user/user.go | 2 +- commands/variable/delete/delete.go | 4 +- commands/variable/delete/delete_test.go | 4 +- commands/variable/get/get.go | 1 - commands/variable/list/list.go | 2 +- commands/variable/update/update.go | 1 - commands/version/version.go | 2 +- internal/config/config_file.go | 11 ++--- internal/config/config_migration.go | 2 +- internal/config/config_type.go | 3 +- internal/config/config_type_test.go | 6 +-- internal/config/testing.go | 1 - internal/config/writefile_test.go | 4 +- internal/glrepo/repo_test.go | 1 - internal/glrepo/resolver_test.go | 1 - pkg/git/git.go | 3 -- pkg/git/git_test.go | 1 - pkg/glinstance/host.go | 6 ++- pkg/httpmock/stub.go | 6 ++- pkg/iostreams/color_test.go | 1 - pkg/iostreams/iostreams_test.go | 2 - pkg/text/truncate_test.go | 1 - pkg/utils/utils_test.go | 2 +- 146 files changed, 253 insertions(+), 269 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1a0f6632..967f6705 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -20,17 +20,20 @@ include: - template: Security/SAST.gitlab-ci.yml - template: Security/Dependency-Scanning.gitlab-ci.yml - template: Security/Secret-Detection.gitlab-ci.yml + - local: .gitlab/ci/*.gitlab-ci.yml # From: https://docs.gitlab.com/ee/ci/caching/#cache-go-dependencies .go-cache: variables: GOPATH: $CI_PROJECT_DIR/.go + GOLANGCI_LINT_CACHE: $CI_PROJECT_DIR/.golangci-lint before_script: - - mkdir -p .go + - mkdir -p .go .golangci-lint cache: paths: - .go/pkg/mod/ + - .golangci-lint/ .documentation: stage: documentation diff --git a/.gitlab/ci/lint.gitlab-ci.yml b/.gitlab/ci/lint.gitlab-ci.yml index 29ca259d..a3bd1406 100644 --- a/.gitlab/ci/lint.gitlab-ci.yml +++ b/.gitlab/ci/lint.gitlab-ci.yml @@ -1,13 +1,14 @@ lint: + extends: .go-cache image: registry.gitlab.com/gitlab-org/gitlab-build-images:golangci-lint-alpine stage: test script: + - golangci-lint --version # Use default .golangci.yml file from the image if one is not present in the project root. - '[ -e .golangci.yml ] || cp /golangci/.golangci.yml .' # Write the code coverage report to gl-code-quality-report.json - # and print linting issues to stdout in the format: path/to/file:line description # remove `--issues-exit-code 0` or set to non-zero to fail the job if linting issues are detected - - golangci-lint run --issues-exit-code 0 --out-format code-climate | tee gl-code-quality-report.json | jq -r '.[] | "\(.location.path):\(.location.lines.begin) \(.description)"' + - golangci-lint run --out-format colored-line-number:stdout,code-climate:gl-code-quality-report.json artifacts: reports: codequality: gl-code-quality-report.json diff --git a/.golangci.yml b/.golangci.yml index f3f3e90f..bddcd06a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,7 +1,17 @@ -linters: - enable: - - gofmt - run: timeout: 5m modules-download-mode: readonly + +linters: + disable-all: true + enable: + - errcheck + - exportloopref + - gofumpt + - goimports + - gosimple + - govet + - ineffassign + - staticcheck + - typecheck + - unused diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9cd78614..77a7de4e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,3 +1,5 @@ +# Contributing Guide + ## Developer Certificate of Origin + License Contributions to this repository are subject to the [Developer Certificate of Origin](https://docs.gitlab.com/ee/legal/developer_certificate_of_origin.html#developer-certificate-of-origin-version-11). @@ -16,26 +18,29 @@ Use your best judgement, and feel free to propose changes to this document in a Please do: -* Check existing issues to verify that the bug or feature request has not already been submitted. -* Open an issue if things aren't working as expected. -* Open an issue to propose a significant change. -* Open an issue to propose a feature. -* Open a merge request to fix a bug. -* Open a merge request to fix documentation about a command. -* Open a merge request for an issue and leave a comment claiming it. +- Check existing issues to verify that the bug or feature request has not already been submitted. +- Open an issue if things aren't working as expected. +- Open an issue to propose a significant change. +- Open an issue to propose a feature. +- Open a merge request to fix a bug. +- Open a merge request to fix documentation about a command. +- Open a merge request for an issue and leave a comment claiming it. Please avoid: -* Opening merge requests for issues marked `blocked`. -* Opening merge requests for documentation for a new command specifically. Manual pages are auto-generated from source after every release +- Opening merge requests for issues marked `blocked`. +- Opening merge requests for documentation for a new command specifically. Manual pages are auto-generated from source after every release + ## Code of Conduct We want to create a welcoming environment for everyone who is interested in contributing. Visit our [Code of Conduct page](https://about.gitlab.com/community/contribute/code-of-conduct/) to learn more about our commitment to an open and welcoming environment. ## Getting Started + ### Building the project Prerequisites: + - Go 1.16+ Build with: `make` or `go build -o bin/glab ./cmd/glab/main.go` @@ -59,6 +64,15 @@ on [GitLab.com](https://gitlab.com). 1. Make your change, add tests, and ensure tests pass 1. Submit a merge request +### Formatting your code + +We use [`golangci-lint`](https://golangci-lint.run/) to lint and format +the code in this project. The linter configuration can be seen +[here](https://gitlab.com/gitlab-org/cli/-/blob/main/.golangci.yml). + +Additional details about code style and format are in the +[go guide](https://docs.gitlab.com/ee/development/go_guide/#code-style-and-format). + ## Commit Messages ### TL;DR: Your commit message should be semantic @@ -76,6 +90,7 @@ Any line of the commit message cannot be longer than 100 characters! This allows ``` ### Message Header + Ideally, the commit message heading which contains the description, should not be more than 50 characters The message header is a single line that contains a succinct description of the change containing a type, an optional scope, and a subject. @@ -100,9 +115,9 @@ Scope can be anything specifying the place of the commit change. For example eve This is a very short description of the change -* `use imperative, present tense: “change” not “changed” nor “changes”` -* `don't capitalize the first letter` -* `no dot (.) at the end` +- `use imperative, present tense: “change” not “changed” nor “changes”` +- `don't capitalize the first letter` +- `no dot (.) at the end` ### Message Body @@ -137,4 +152,3 @@ fix(login): allow provided user preferences to override default preferences Fixes #025 ``` - diff --git a/api/client.go b/api/client.go index 263f2132..3a50a80f 100644 --- a/api/client.go +++ b/api/client.go @@ -78,6 +78,7 @@ func GetClient() *Client { // HTTPClient returns the httpClient instance used to initialise the gitlab api client func HTTPClient() *http.Client { return apiClient.HTTPClient() } + func (c *Client) HTTPClient() *http.Client { if c.httpClientOverride != nil { return c.httpClientOverride @@ -90,12 +91,14 @@ func (c *Client) HTTPClient() *http.Client { // OverrideHTTPClient overrides the default http client func OverrideHTTPClient(client *http.Client) { apiClient.OverrideHTTPClient(client) } + func (c *Client) OverrideHTTPClient(client *http.Client) { c.httpClientOverride = client } // Token returns the authentication token func Token() string { return apiClient.Token() } + func (c *Client) Token() string { return c.token } diff --git a/api/client_test.go b/api/client_test.go index f4523351..001e9b0e 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -11,7 +11,7 @@ func Test_tlsConfig(t *testing.T) { type args struct { host string } - var tests = []struct { + tests := []struct { name string args args want []uint16 @@ -38,7 +38,6 @@ func Test_tlsConfig(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - client := tlsConfig(tt.args.host) assert.Equal(t, tt.want, client.CipherSuites) diff --git a/api/pipeline.go b/api/pipeline.go index 86372a67..94293e16 100644 --- a/api/pipeline.go +++ b/api/pipeline.go @@ -166,7 +166,6 @@ var GetPipeline = func(client *gitlab.Client, pid int, l *gitlab.RequestOptionFu } pipe, _, err := client.Pipelines.GetPipeline(repo, pid) - if err != nil { return nil, err } @@ -179,13 +178,12 @@ var GetPipelineVariables = func(client *gitlab.Client, pid int, l *gitlab.Reques } pipe, _, err := client.Pipelines.GetPipeline(repo, pid) - if err != nil { - return nil, err - } + if err != nil { + return nil, err + } projectID := pipe.ProjectID pipelineVars, _, err := client.Pipelines.GetPipelineVariables(projectID, pid) - if err != nil { return nil, err } diff --git a/api/repository_files.go b/api/repository_files.go index 62742297..1ef1081a 100644 --- a/api/repository_files.go +++ b/api/repository_files.go @@ -12,7 +12,6 @@ var GetFile = func(client *gitlab.Client, projectID interface{}, path string, re Ref: &ref, } file, _, err := client.RepositoryFiles.GetFile(projectID, path, fileOpts) - if err != nil { return nil, err } diff --git a/api/variable.go b/api/variable.go index 589f26cb..31e1bac8 100644 --- a/api/variable.go +++ b/api/variable.go @@ -55,7 +55,6 @@ var DeleteProjectVariable = func(client *gitlab.Client, projectID interface{}, k }, } _, err := client.ProjectVariables.RemoveVariable(projectID, key, reqOpts) - if err != nil { return err } @@ -68,7 +67,7 @@ var UpdateProjectVariable = func(client *gitlab.Client, projectID interface{}, k client = apiClient.Lab() } - var filter = func(request *retryablehttp.Request) error { + filter := func(request *retryablehttp.Request) error { q := request.URL.Query() q.Add("filter[environment_scope]", *opts.EnvironmentScope) @@ -127,7 +126,6 @@ var DeleteGroupVariable = func(client *gitlab.Client, groupID interface{}, key s } _, err := client.GroupVariables.RemoveVariable(groupID, key) - if err != nil { return err } diff --git a/cmd/gen-docs/docs.go b/cmd/gen-docs/docs.go index 589097f7..fa0e6187 100644 --- a/cmd/gen-docs/docs.go +++ b/cmd/gen-docs/docs.go @@ -36,7 +36,7 @@ func main() { } os.Exit(1) } - err := os.MkdirAll(*path, 0755) + err := os.MkdirAll(*path, 0o755) if err != nil { fatal(err) } @@ -75,7 +75,7 @@ func genWebDocs(glabCli *cobra.Command, path string) error { for _, cmd := range cmds { fmt.Println("Generating docs for " + cmd.Name()) // create directories for parent commands - _ = os.MkdirAll(path+cmd.Name(), 0750) + _ = os.MkdirAll(path+cmd.Name(), 0o750) // Generate parent command out := new(bytes.Buffer) @@ -88,7 +88,7 @@ func genWebDocs(glabCli *cobra.Command, path string) error { for _, cmdC := range cmd.Commands() { if cmdC.HasAvailableSubCommands() { fmt.Println("Generating subcommand docs for " + cmdC.Name()) - _ = os.MkdirAll(path+cmd.Name()+"/"+cmdC.Name(), 0750) + _ = os.MkdirAll(path+cmd.Name()+"/"+cmdC.Name(), 0o750) // Generate parent command out := new(bytes.Buffer) @@ -97,7 +97,7 @@ func genWebDocs(glabCli *cobra.Command, path string) error { return err } - err = config.WriteFile(path+cmd.Name()+"/"+cmdC.Name()+"/index.md", out.Bytes(), 0755) + err = config.WriteFile(path+cmd.Name()+"/"+cmdC.Name()+"/index.md", out.Bytes(), 0o755) if err != nil { return err } @@ -119,7 +119,7 @@ func genWebDocs(glabCli *cobra.Command, path string) error { } } - err = config.WriteFile(path+cmd.Name()+"/index.md", out.Bytes(), 0755) + err = config.WriteFile(path+cmd.Name()+"/index.md", out.Bytes(), 0o755) if err != nil { return err } @@ -190,14 +190,14 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer) error { buf := new(bytes.Buffer) name := cmd.CommandPath() - //GitLab Specific Docs Metadata + // GitLab Specific Docs Metadata buf.WriteString("---" + "\n") buf.WriteString("stage: Create" + "\n") buf.WriteString("group: Code Review" + "\n") buf.WriteString("info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments" + "\n") buf.WriteString("---" + "\n\n") - //Generated by a script + // Generated by a script buf.WriteString("