8000 chore(ci): add golangci-lint action and apply linting changes by clok · Pull Request #120 · git-chglog/git-chglog · GitHub
[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ci): add golangci-lint action and apply linting changes #120

Merged
merged 19 commits into from
Mar 17, 2021
Merged

Conversation

clok
Copy link
Collaborator
@clok clok commented Mar 15, 2021

What does this do / why do we need it?

This adds linting and formatting expectations to the project. Linting allows for code written and maintained by a group of individuals to read as if it comes from one collective mind. This helps with maintainability, readability and other "bility" words.

How this PR fixes the problem?

It adds the golangci-lint project as a Development and CI dependency. It uses the linters that are enabled by default as well as:

What should your reviewer look out for in this PR?

These are linters that I have used in the past. I have found them helpful, but I would like the opinions of others.

Each commit contains the delta required to pass a given linter.

Check lists

  • Test passed
  • Coding style (indentation, etc)

Additional Comments (if any)

For reference, here was the golangci-lint output before these changes:
image

Which issue(s) does this PR fix?

fixes #89

@clok clok added the type: refactoring Refactoring source code or architecture label Mar 15, 2021
@clok clok requested review from khos2ow and mavogel March 15, 2021 15:48
@clok clok self-assigned this Mar 15, 2021
@coveralls
Copy link
coveralls commented Mar 15, 2021

Pull Request Test Coverage Report for Build 658991180

  • 44 of 61 (72.13%) changed or added relevant lines in 14 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.6%) to 76.13%

Changes Missing Coverage Covered Lines Changed/Added Lines %
commit_parser.go 5 6 83.33%
tag_reader.go 0 1 0.0%
tag_selector.go 7 8 87.5%
chglog.go 3 5 60.0%
cmd/git-chglog/config_loader.go 0 2 0.0%
cmd/git-chglog/questioner.go 0 4 0.0%
cmd/git-chglog/main.go 0 6 0.0%
Files with Coverage Reduction New Missed Lines %
tag_selector.go 1 90.63%
Totals Coverage Status
Change from base Build 652113868: 0.6%
Covered Lines: 1751
Relevant Lines: 2300

💛 - Coveralls

Type string // (e.g. `feat`)
Scope string // (e.g. `core`)
Subject string // (e.g. `Add new feature`)
JiraIssueID string // (e.g. `RNWY-310`)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BREAKING CHANGE as the original role out of the Jira integration expected JiraIssueId as the type in the config.

@clok clok added the type: breaking change Has breaking changes label Mar 15, 2021
@khos2ow
Copy link
Collaborator
khos2ow commented Mar 15, 2021

Thank you @clok for taking the initiative! I personally would like to suggest even more enhancement (collapsed for brevity).

content of .golangci.yml file.
# https://golangci-lint.run/usage/configuration/
run:
  timeout: 10m
  deadline: 5m

  tests: true

output:
  format: tab

linters-settings:
  govet:
    # report about shadowed variables
    check-shadowing: true

  golint:
    # minimal confidence for issues, default is 0.8
    min-confidence: 0.8

  gofmt:
    # simplify code: gofmt with `-s` option, true by default
    simplify: true

  goimports:
    # put imports beginning with prefix after 3rd-party packages;
    # it's a comma-separated list of prefixes
    local-prefixes: github.com/git-chglog/git-chglog

  gocyclo:
    # minimal code complexity to report, 30 by default (but we recommend 10-20)
    min-complexity: 10

  maligned:
    # print struct with more effective memory layout or not, false by default
    suggest-new: true

  dupl:
    # tokens count to trigger issue, 150 by default
    threshold: 100

  goconst:
    # minimal length of string constant, 3 by default
    min-len: 3
    # minimal occurrences count to trigger, 3 by default
    min-occurrences: 5

  lll:
    # tab width in spaces. Default to 1.
    tab-width: 1

  unused:
    # treat code as a program (not a library) and report unused exported identifiers; default is false.
    # XXX: if you enable this setting, unused will report a lot of false-positives in text editors:
    # if it's called for subdir of a project it can't find funcs usages. All text editor integrations
    # with golangci-lint call it on a directory with the changed file.
    check-exported: false

  unparam:
    # Inspect exported functions, default is false. Set to true if no external program/library imports your code.
    # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
    # if it's called for subdir of a project it can't find external interfaces. All text editor integrations
    # with golangci-lint call it on a directory with the changed file.
    check-exported: false

  nakedret:
    # make an issue if func has more lines of code than this setting and it has naked returns; default is 30
    max-func-lines: 30

  prealloc:
    # XXX: we don't recommend using this linter before doing performance profiling.
    # For most programs usage of prealloc will be a premature optimization.

    # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them.
    # True by default.
    simple: true
    range-loops: true # Report preallocation suggestions on range loops, true by default
    for-loops: false # Report preallocation suggestions on for loops, false by default

  gocritic:
    # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint` run to see all tags and checks.
    # Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
    enabled-tags:
      - performance

    settings: # settings passed to gocritic
      captLocal: # must be valid enabled check name
        paramsOnly: true
      rangeValCopy:
        sizeThreshold: 32

  misspell:
    locale: US

linters:
  enable:
    - megacheck
    - govet
    - gocyclo
    - gocritic
    - interfacer
    - goconst
    - goimports
    - gofmt  # We enable this as well as goimports for its simplify mode.
    - prealloc
    - golint
    - unconvert
    - misspell
    - nakedret
    - dupl
    - depguard

  presets:
    - bugs
    - unused
  fast: false

issues:
  # Excluding configuration per-path and per-linter
  exclude-rules:
    # Exclude some linters from running on tests files.
    - path: _test(ing)?\.go
      linters:
        - gocyclo
        - errcheck
        - dupl
        - gosec
        - scopelint
        - unparam

    # These are performance optimisations rather than style issues per se.
    # They warn when function arguments or range values copy a lot of memory
    # rather than using a pointer.
    - text: "(hugeParam|rangeValCopy):"
      linters:
      - gocritic

  # Independently from option `exclude` we use default exclude patterns,
  # it can be disabled by this option. To list all
  # excluded by default patterns execute `golangci-lint run --help`.
  # Default value for this option is true.
  exclude-use-default: false

  # Show only new issues: if there are unstaged changes or untracked files,
  # only those changes are analyzed, else only changes in HEAD~ are analyzed.
  # It's a super-useful option for integration of golangci-lint into existing
  # large codebase. It's not practical to fix all existing issues at the moment
  # of integration: much better don't allow issues in new code.
  # Default is false.
  new: false

  # Maximum issues count per one linter. Set to 0 to disable. Default is 50.
  max-per-linter: 0

  # Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
  max-same-issues: 0

@khos2ow
Copy link
Collaborator
khos2ow commented Mar 15, 2021

And please don't hesitate to let me know to give a hand for fixing these. I didn't already do it to prevent stepping on your toes.

@clok
Copy link
Collaborator Author
clok commented Mar 15, 2021

And please don't hesitate to let me know to give a hand for fixing these. I didn't already do it to prevent stepping on your toes.

@khos2ow Appreciate it! Just an FYI - no need to worry about stepping on my toes. Always appreciate a collaborative effort on coding. I just recommend communicating one's intentions to the team just so that way we don't have duplicated efforts.

I really appreciate the more detailed and thorough configuration. I am going to take some time to look it over. For now, if you want to take a pass at addressing these issues, please do so. I won't be able to focus on this until later this evening.

Thank you again for the collaboration! I'll keep an eye for any changes that may be pushed before taking on any other actions when I come back around to this.

@khos2ow
Copy link
Collaborator
khos2ow commented Mar 15, 2021

Awesome, then I will commit the configuration just now, and will attempt to fix as much as I can a little while later.

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
@khos2ow khos2ow removed their request for review March 15, 2021 18:59
@khos2ow
Copy link
Collaborator
khos2ow commented Mar 15, 2021

I also removed myself from reviewers, as it doesn't make sense for me to be reviewing my own work! 😅

@clok
Copy link
Collaborator Author
clok commented Mar 16, 2021

@khos2ow thank you for the updates. I pushed a few other patches to address scopelint, gosec and unparam.

The prealloc linter in nice. The failure that is popping up is a bit interesting in this case. I don't think we will have the ability to know the size of the []*Tag slice that we will be building in that function. I am interested in what you think.

We still have gocyclo, errorlint and govet failures as well.

@clok clok requested a review from wadackel March 16, 2021 04:19
@clok clok requested a review from ghostsquad March 16, 2021 04:19
Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
@khos2ow
Copy link
Collaborator
khos2ow commented Mar 16, 2021

@clok I think the best approach for dealing with gocyclo issues was to ignore them. Basically the idea of having a complexity goal enforces us to not put everything in one function, but in some cases that's inevitable (e.g. commitFilter()).

On the other hand sortCommitGroups() has some potential for reducing the complexity, which in the scope of this PR, IMO, can be ignored and be tackled later on.

Copy link
Member
@mavogel mavogel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!
I agree the gocyclo can be tackled later in separate issues. Feel free to open them, so we keep track. The BC is also fine. As the naming will be caught in the future and we are still pre-major.

@clok
Copy link
Collaborator Author
clok commented Mar 16, 2021

@khos2ow I like that approach. Thank you for putting the comments and TODO callouts. Agree that a separate slice of work can take care of those refactors.

Unless you feel we can take care of the prealloc failure, I am inclined to ignore it on that line. For now, I am going to add a comment in code and push that change.

@mavogel Roger that on the BC.

chglog.go Outdated
Comment on lines 143 to 145
scopeErr := back()
if scopeErr != nil {
log.Fatal(scopeErr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also can be written as

		if err = back(); err != nil {
			log.Fatal(err)
		}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that. I always forget the inline boolean check.

@clok clok merged commit ae3382b into master Mar 17, 2021
@clok clok deleted the ci/lint branch March 17, 2021 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: breaking change Has breaking changes type: refactoring Refactoring source code or architecture
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ci: add format check and linting
4 participants
0