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

lint: sort imports #17131

Merged
merged 3 commits into from
Jul 20, 2022
Merged

lint: sort imports #17131

merged 3 commits into from
Jul 20, 2022

Conversation

daixiang0
Copy link
Contributor

@daixiang0 daixiang0 commented Jul 6, 2022

Signed-off-by: Loong Dai [email protected]

Thank you for contributing to Harbor!

Comprehensive Summary of your change

Add goimprots linter to group Go imports.

Please indicate you've done the following:

  • Well Written Title and Summary of the PR
  • Label the PR as needed. "release-note/ignore-for-release, release-note/new-feature, release-note/update, release-note/enhancement, release-note/community, release-note/breaking-change, release-note/docs, release-note/infra, release-note/deprecation"
  • Accepted the DCO. Commits without the DCO will delay acceptance.
  • Made sure tests are passing and test coverage is added if needed.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed in website repository.

@daixiang0 daixiang0 requested a review from a team as a code owner July 6, 2022 06:57
@daixiang0
Copy link
Contributor Author

release-note/ignore-for-release

@codecov
Copy link

codecov bot commented Jul 6, 2022

Codecov Report

Merging #17131 (84e390d) into main (f3edb03) will decrease coverage by 0.23%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #17131      +/-   ##
==========================================
- Coverage   67.28%   67.04%   -0.24%     
==========================================
  Files         992      993       +1     
  Lines       83942    83550     -392     
  Branches     2665     2665              
==========================================
- Hits        56479    56020     -459     
- Misses      23584    23653      +69     
+ Partials     3879     3877       -2     
Flag Coverage Δ
unittests 67.04% <ø> (-0.24%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/chartserver/cache.go 53.16% <ø> (ø)
src/chartserver/chart_operator.go 67.70% <ø> (ø)
src/chartserver/handler_manipulation.go 26.15% <ø> (ø)
src/chartserver/handler_utility.go 61.45% <ø> (ø)
src/chartserver/redis_sentinel.go 0.88% <ø> (ø)
src/common/api/base.go 27.38% <ø> (ø)
src/common/dao/base.go 58.33% <ø> (ø)
src/common/dao/mysql.go 0.00% <ø> (ø)
src/common/dao/pgsql.go 72.46% <ø> (ø)
src/common/dao/resource_label.go 77.96% <ø> (ø)
... and 301 more

@wy65701436 wy65701436 added the release-note/ignore-for-release Do not include PR or Issue for release notes label Jul 6, 2022
@zyyw
Copy link
Contributor

zyyw commented Jul 7, 2022

@daixiang0 Thank you for the contribution to Harbor project.
Could you please update the import into this format, separating the import into 3 groups:

  • import built-in packages
  • import the current goharbor/harbor project packages, i.e. "github.com/goharbor/harbor"
  • import third-party packages

and sort the import within each group.

for example:

import (
	"net/http"
	"os"
	"strings"

	"github.com/goharbor/harbor/src/common/dao"
	commonthttp "github.com/goharbor/harbor/src/common/http"
	"github.com/goharbor/harbor/src/common/models"
	"github.com/goharbor/harbor/src/lib/log"
	"github.com/goharbor/harbor/src/pkg/exporter"

	_ "github.com/jackc/pgx/v4/stdlib" // registry pgx driver
	"github.com/prometheus/client_golang/prometheus"
	"github.com/spf13/viper"
)

@daixiang0
Copy link
Contributor Author

Sure, the GCI would this work.

@zyyw
Copy link
Contributor

zyyw commented Jul 11, 2022

Hi @daixiang0 , do we have any update on this PR?

@daixiang0
Copy link
Contributor Author

Will update later, thanks.

@daixiang0
Copy link
Contributor Author

daixiang0 commented Jul 13, 2022

Now linter only supports:

  • import built-in packages
  • import third-party packages
  • import the current goharbor/harbor project packages, i.e. "github.com/goharbor/harbor"

@zyyw I will update if ok.

@zyyw
Copy link
Contributor

zyyw commented Jul 13, 2022

Now linter only supports:

  • import built-in packages
  • import third-party packages
  • import the current goharbor/harbor project packages, i.e. "github.com/goharbor/harbor"

@zyyw I will update if ok.

@daixiang0 It will also be grouped into 3 parts, right?
If so, that's also fine with me. Please update it. Thank you for the contribution!

@daixiang0
Copy link
Contributor Author

Sure.

"fmt"
"strconv"
)
Copy link
Contributor

@zyyw zyyw Jul 18, 2022

Choose a reason for hiding this comment

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

This close parentheses ) should NOT be removed. This is important.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems linter's bug, let me fix it.

@@ -1,7 +1,6 @@
package chartserver

import "testing"

Copy link
Contributor

Choose a reason for hiding this comment

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

Why removing this line 4 here? Is it deleted by make lint? Maybe we should keep it.

Copy link
Contributor

@zyyw zyyw Jul 18, 2022

Choose a reason for hiding this comment

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

Essentially, we would like there is an empty line between import and func TestGetIndexFile. If this line 4 is deleted automatically by make lint, maybe we can try to change

import "testing"

// Test get /index.yaml
func TestGetIndexFile(t *testing.T) {
...
}

to

import (
    "testing"
)

// Test get /index.yaml
func TestGetIndexFile(t *testing.T) {
...
}

to ensure there is an empty line between import and its subsequent code.

This is optional. if you prefer to what it is, I'm also okay with it. And it should be consistent, for example, if we change it to be formatted as I suggest, then this kind of one line import in other files should also be updated like this one. Or if you prefer to what it is, then this kind of one line import should also stay the same format as this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, I will try to update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now all linters do not config the one-line import, they will keep it as they were. I recommend that we keep the same with them.

The emply line is necessary, I will update them all but keep one-line import as they were.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, thanks a lot!

@@ -15,7 +15,6 @@
package event

import "github.com/goharbor/harbor/src/pkg/reg/model"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -1,7 +1,6 @@
package model

import "github.com/goharbor/harbor/src/pkg/retention/policy/rule"

Copy link
Contributor

@zyyw zyyw Jul 18, 2022

Choose a reason for hiding this comment

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

@@ -15,7 +15,6 @@
package jobservice

import "time"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -15,7 +15,6 @@
package flow

import "github.com/goharbor/harbor/src/pkg/reg/adapter"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -1,7 +1,6 @@
package notifications

import "github.com/goharbor/harbor/src/core/api"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -15,7 +15,6 @@
package legacy

import "github.com/goharbor/harbor/src/pkg/scheduler"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -1,7 +1,6 @@
package backend

import "testing"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -3,7 +3,6 @@
package period

import mock "github.com/stretchr/testify/mock"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -15,6 +15,5 @@
package lib

import "github.com/goharbor/harbor/src/common/http/modifier"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -15,7 +15,6 @@
package metadata

import "github.com/goharbor/harbor/src/common"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -1,7 +1,6 @@
package repository

import "net/url"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -15,7 +15,6 @@
package orm

import "github.com/beego/beego/orm"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -15,7 +15,6 @@
package lib

import "net/http"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -15,7 +15,6 @@
package retention

import "time"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -1,7 +1,6 @@
package export

import "github.com/bmatcuk/doublestar"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -15,7 +15,6 @@
package model

import "github.com/theupdateframework/notary/tuf/data"

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -1,7 +1,6 @@
package immutable

import "fmt"

Copy link
Contributor

Choose a reason for hiding this comment

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

@zyyw
Copy link
Contributor

zyyw commented Jul 18, 2022

A new release of golangci-lint will come next day, I will use the latest one to update.

Thank you! When you update the code, could you please rebase the latest main into your branch to resolve the conflicts as well?

@daixiang0 daixiang0 changed the title lint: add goimports lint: sort imports Jul 19, 2022
@daixiang0
Copy link
Contributor Author

@zyyw I have rebased the latest main.

Thanks for your hard review, the changes are big while meaningful, after this, we can get clear and ordered imports.

"net"
"net/http"
"time"

"github.com/goharbor/harbor/src/lib/trace"
Copy link
Contributor

Choose a reason for hiding this comment

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

line 23 & line 27 are the same.

"net/http"
"time"

"github.com/goharbor/harbor/src/lib/cache/memory"

Copy link
Contributor

Choose a reason for hiding this comment

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

line 23 and line 26 are the same

"time"

proModels "github.com/goharbor/harbor/src/pkg/project/models"
Copy link
Contributor

Choose a reason for hiding this comment

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

line 6 and line 11 are the same

@daixiang0
Copy link
Contributor Author

Goimports do not remove duplicate imports, let me fix it by hand :(

@zyyw
Copy link
Contributor

zyyw commented Jul 19, 2022

Hi @daixiang0, I'm sorry, maybe we should stay on [email protected], since [email protected] does not works well. Here is my reasons:

  • some files have duplicate import.
  • there are 890+ files updated in yesterday's PR review while there are only 315 files updated in today's PR review. I suspect there are some files are not grouped into 3 parts (which is our primary goal of this PR, sorting import within each group is secondary and doing it in a separate PR is also acceptable)
  • concerns about introducing failures of the existing enabled lint check, https://github.com/goharbor/harbor/runs/7402038562?check_suite_focus=true

@daixiang0
Copy link
Contributor Author

OK, I will turn back to 1.45.2.

Signed-off-by: Loong Dai <[email protected]>
@daixiang0
Copy link
Contributor Author

The root cause of CI failure is goharbor/mockery would generate some codes with unordered imports, I am not familiar with it, I will ignore those files, what do you think about it?

@zyyw
Copy link
Contributor

zyyw commented Jul 19, 2022

The root cause of CI failure is goharbor/mockery would generate some codes with unordered imports, I am not familiar with it, I will ignore those files, what do you think about it?

My initial idea is to change the following content in Makefile

mocks_check: gen_mocks

to

mocks_check: gen_mocks lint

But I need to discuss it with the team first.

Apart from the mocks_check, when I run make lint, it errors out alone as well.
error message:

INFO [runner] linters took 3.445226937s with stages: goanalysis_metalinter: 3.321637021s, unused: 28.535µs, bodyclose: 16.579µs, gosimple: 9.203µs, structcheck: 4.951µs
pkg/reg/manager.go:24: File is not `goimports`-ed with -local github.com/goharbor/harbor (goimports)
	"github.com/goharbor/harbor/src/pkg/reg/adapter"
common/job/test/server.go:15: File is not `gofmt`-ed (gofmt)
	job_models "github.com/goharbor/harbor/src/jobservice/job"
controller/icon/controller.go:21: File is not `goimports`-ed with -local github.com/goharbor/harbor (goimports)
	"image"
INFO File cache stats: 15 entries of total size 79.8KiB
INFO Memory: 75 samples, avg is 125.7MB, max is 219.3MB
INFO Execution took 7.312123433s
make: *** [lint] Error 1

Hi @daixiang0 , could you please help to resolve this make lint first? Thanks
BTW, the golangci-lint version of my local env is also v1.45.2

Signed-off-by: Loong Dai <[email protected]>
@daixiang0
Copy link
Contributor Author

Has fixed, goimports would add new line before a comment

diff --git a/src/controller/icon/controller.go b/src/controller/icon/controller.go
index 9122acdab..8927aa136 100644
--- a/src/controller/icon/controller.go
+++ b/src/controller/icon/controller.go
@@ -19,6 +19,7 @@ import (
        "context"
        "encoding/base64"
        "image"
+
        // import the gif format
        _ "image/gif"
        // import the jpeg format
diff --git a/src/pkg/reg/manager.go b/src/pkg/reg/manager.go
index a316641c2..af1f57c4c 100644
--- a/src/pkg/reg/manager.go
+++ b/src/pkg/reg/manager.go
@@ -22,6 +22,7 @@ import (
        "github.com/goharbor/harbor/src/lib/config"
        "github.com/goharbor/harbor/src/lib/q"
        "github.com/goharbor/harbor/src/pkg/reg/adapter"
+
        // register the AliACR adapter
        _ "github.com/goharbor/harbor/src/pkg/reg/adapter/aliacr"
        // register the Artifact Hub adapter

@zyyw
Copy link
Contributor

zyyw commented Jul 19, 2022

Thank you for the fix.
Regarding the goharbor/mockery thing, it seems that my initial idea doesn't work. Let's ignore those mock files.

We can ignore all files under src/testing directory. And in terms of the following mock files that are not under src/testing directory, let's use pattern matching (if possible), for example: src/controller/replication/flow/mock_*.go, src/controller/replication/mock_*.go(just an example of the idea, the matching pattern may not be exactly as they are) :

src/controller/replication/flow/mock_adapter_factory_test.go
src/controller/replication/flow/mock_adapter_test.go
src/controller/replication/mock_flow_controller_test.go
src/jobservice/mgt/mock_manager.go
src/pkg/scheduler/mock_dao_test.go
src/pkg/task/mock_execution_dao_test.go
src/pkg/task/mock_jobservice_client_test.go
src/pkg/task/mock_task_dao_test.go
src/pkg/task/mock_task_manager_test.go

Signed-off-by: Loong Dai <[email protected]>
Copy link
Contributor

@zyyw zyyw left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@wy65701436 wy65701436 left a comment

Choose a reason for hiding this comment

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

lgtm

@zyyw zyyw merged commit 09371b4 into goharbor:main Jul 20, 2022
@daixiang0 daixiang0 deleted the gci branch July 20, 2022 03:40
sluetze pushed a commit to sluetze/harbor that referenced this pull request Oct 29, 2022
* lint: add goimports

Signed-off-by: Loong Dai <[email protected]>
mcsage pushed a commit to mcsage/harbor that referenced this pull request Feb 16, 2023
* lint: add goimports

Signed-off-by: Loong Dai <[email protected]>
Signed-off-by: Stephan Hohn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/ignore-for-release Do not include PR or Issue for release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants