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

refactor: add lint step and fix linting issues #233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 16 additions & 44 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ linters-settings:
- wrapperFunc
- importShadow # not important for now
- unnamedResult # not important
- commentedOutCode
- nestingReduce
gocyclo:
min-complexity: 15
goimports:
goimports: {}
golint:
min-confidence: 0
gofmt:
Expand All @@ -47,9 +45,9 @@ linters-settings:
settings:
mnd:
# don't include the "operation" and "assign"
checks: argument,case,condition,return
checks: [argument, case, condition, return]
govet:
check-shadowing: false
check-shadowing: true
settings:
printf:
funcs:
Expand All @@ -62,7 +60,7 @@ linters-settings:
line-length: 140
maligned:
suggest-new: true
misspell:
misspell: {}
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
allow-unused: false # report any unused nolint directives
Expand All @@ -77,18 +75,13 @@ linters:
- bodyclose
- deadcode
- depguard
# - dogsled
- dupl
- errcheck
# - funlen
# - goconst
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- ineffassign
# - interfacer (This tool is deprecated)
- misspell
- nakedret
- rowserrcheck
Expand All @@ -99,52 +92,31 @@ linters:
- unparam
- unused
- varcheck
- golint
- revive
- gocritic
- govet
# - lll
# - gocyclo
# - nestif
# don't enable:
# - testpackage
# - gochecknoinits
# - stylecheck
# - lll
# - govet
# - whitespace
# - exhaustive (TODO: enable after next release; current release at time of writing is v1.27)
# - gochecknoglobals
# - gocognit
# - godot
# - godox
# - goerr113
# - maligned
# - prealloc
# - wsl
# - nolintlint
# - gomnd
# - scopelint
- dupl
issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# - path: _test\.go
# linters:
# - gomnd
# https://github.com/go-critic/go-critic/issues/926
# - path: _test\.go
# linters:
# - gomnd
# https://github.com/go-critic/go-critic/issues/926
- linters:
- gocritic
text: "unnecessaryDefer:"
exclude:
- 'shadow: declaration of "err" shadows declaration at'
max-same-issues: 0

run:
timeout: 30m
skip-dirs:
- test/testdata_etc
- internal/cache
- internal/renameio
- internal/robustio
- cmd/docs
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.29.x # use the fixed version to not introduce new linters unexpectedly
golangci-lint-version: 1.42.x # use the fixed version to not introduce new linters unexpectedly
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
30 changes: 30 additions & 0 deletions .lighthouse/jenkins-x/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
creationTimestamp: null
name: lint
spec:
pipelineSpec:
tasks:
- name: jx-admin-lint
resources: {}
taskSpec:
metadata: {}
stepTemplate:
image: uses:jenkins-x/jx3-pipeline-catalog/tasks/go/pullrequest.yaml@versionStream
name: ""
resources:
requests:
cpu: 400m
memory: 600Mi
workingDir: /workspace/source
steps:
- image: uses:jenkins-x/jx3-pipeline-catalog/tasks/git-clone/git-clone-pr.yaml@versionStream
name: ""
resources: {}
- name: make-lint
resources: {}
podTemplate: {}
serviceAccountName: tekton-bot
timeout: 30m0s
status: {}
2 changes: 1 addition & 1 deletion .lighthouse/jenkins-x/pullrequest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ spec:
resources: {}
podTemplate: {}
serviceAccountName: tekton-bot
timeout: 240h0m0s
timeout: 1h0m0s
status: {}
11 changes: 9 additions & 2 deletions .lighthouse/jenkins-x/triggers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ spec:
context: "pr"
always_run: true
optional: false
trigger: "/test"
rerun_command: "/retest"
trigger: (?m)^/test( all| pr),?(s+|$)
rerun_command: /test pr
source: "pullrequest.yaml"
- name: lint
context: "lint"
always_run: true
optional: false
trigger: (?m)^/test( all| lint),?(s+|$)
rerun_command: /test lint
source: "lint.yaml"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ORG := jenkins-x-plugins
ORG_REPO := $(ORG)/$(NAME)
RELEASE_ORG_REPO := $(ORG_REPO)
ROOT_PACKAGE := github.com/$(ORG_REPO)
GO_VERSION := 1.13
GO_VERSION := $(shell $(GO) version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/')
GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,cmd/,*.go)

BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2> /dev/null || echo 'unknown')
Expand Down
2 changes: 2 additions & 0 deletions OWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ approvers:
- rawlingsj
- jstrachan
- rajdavies
- ankitm123
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can help with reviewing/approving PRs ...

reviewers:
- rawlingsj
- jstrachan
- rajdavies
- ankitm123
5 changes: 2 additions & 3 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//nolint:gofmt,goimports
// +build !windows

package app

import (
"github.com/jenkins-x-plugins/jx-admin/pkg/cmd"
)
import "github.com/jenkins-x-plugins/jx-admin/pkg/cmd"

// Run runs the command, if args are not nil they will be set on the command
func Run(args []string) error {
Expand Down
3 changes: 1 addition & 2 deletions hack/linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if ! [ -x "$(command -v golangci-lint)" ]; then
echo "Installing GolangCI-Lint"
${DIR}/install_golint.sh -b $GOPATH/bin v1.31.0
${DIR}/install_golint.sh -b $GOPATH/bin v1.42.1
fi

export GO111MODULE=on
golangci-lint run \
--timeout 30m \
--verbose \
--build-tags build
10 changes: 6 additions & 4 deletions pkg/bootjobs/bootjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package bootjobs

import (
"context"
"sort"
"strings"

"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
"github.com/pkg/errors"
batchv1 "k8s.io/api/batch/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sort"
"strings"
)

// GetSortedJobs gets the boot jobs with an optional commit sha filter
func GetSortedJobs(client kubernetes.Interface, ns string, selector string, commitSHA string) ([]batchv1.Job, error) {
func GetSortedJobs(client kubernetes.Interface, ns, selector, commitSHA string) ([]batchv1.Job, error) {
jobList, err := client.BatchV1().Jobs(ns).List(context.TODO(), metav1.ListOptions{
LabelSelector: selector,
})
Expand All @@ -24,7 +25,8 @@ func GetSortedJobs(client kubernetes.Interface, ns string, selector string, comm
answer := jobList.Items
if commitSHA != "" {
var filtered []batchv1.Job
for _, job := range answer {
for k := range answer {
job := answer[k]
labels := job.Labels
if labels != nil {
sha := labels[LabelCommitSHA]
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func TestCreate(t *testing.T) {
outFile, err := ioutil.TempFile("", "")
require.NoError(t, err, "failed to create tempo file")
outFileName := outFile.Name()
args := append(tc.Args, "--git-server", "https://fake.com", "--git-kind", "fake", "--env-git-owner", "jstrachan", "--cluster", tc.Name, "--out", outFileName)
args = append(args, tc.Args...)
co.Args = args
tc.Args = append(tc.Args, "--git-server", "https://fake.com", "--git-kind", "fake", "--env-git-owner", "jstrachan", "--cluster", tc.Name, "--out", outFileName)

co.Args = tc.Args
co.Environment = tc.Environment
if co.Environment == "" {
co.Environment = "dev"
Expand Down
11 changes: 5 additions & 6 deletions pkg/cmd/joblog/joblog.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,13 @@ See: https://jenkins-x.io/docs/v3/guides/operator/

if o.CommitSHA != "" {
logger.Logger().Infof("waiting for boot Job pod with selector %s in namespace %s for commit SHA %s...", info(selector), info(ns), info(o.CommitSHA))

} else {
logger.Logger().Infof("waiting for boot Job pod with selector %s in namespace %s...", info(selector), info(ns))
}
return nil
}

func (o *Options) waitForActiveJob(client kubernetes.Interface, ns string, selector string, info func(a ...interface{}) string, containerName string) error {
func (o *Options) waitForActiveJob(client kubernetes.Interface, ns, selector string, info func(a ...interface{}) string, containerName string) error {
job, err := o.waitForLatestJob(client, ns, selector)
if err != nil {
return errors.Wrapf(err, "failed to wait for active Job in namespace %s with selector %v", ns, selector)
Expand All @@ -184,7 +183,7 @@ func (o *Options) waitForActiveJob(client kubernetes.Interface, ns string, selec
return o.viewActiveJobLog(client, ns, selector, containerName, job)
}

func (o *Options) viewActiveJobLog(client kubernetes.Interface, ns string, selector string, containerName string, job *batchv1.Job) error {
func (o *Options) viewActiveJobLog(client kubernetes.Interface, ns, selector, containerName string, job *batchv1.Job) error {
var foundPods []string
for {
complete, pod, err := o.waitForJobCompleteOrPodRunning(client, ns, selector, job.Name)
Expand Down Expand Up @@ -233,7 +232,7 @@ func (o *Options) viewActiveJobLog(client kubernetes.Interface, ns string, selec
}
}

func (o *Options) viewJobLog(client kubernetes.Interface, ns string, selector string, containerName string, job *batchv1.Job) error {
func (o *Options) viewJobLog(client kubernetes.Interface, ns, selector, containerName string, job *batchv1.Job) error {
opts := metav1.ListOptions{
LabelSelector: "job-name=" + job.Name,
}
Expand Down Expand Up @@ -436,7 +435,7 @@ func (o *Options) checkIfJobComplete(client kubernetes.Interface, ns, name strin
return false, job, nil
}

func (o *Options) pickJobToLog(client kubernetes.Interface, ns string, selector string, jobs []batchv1.Job) error {
func (o *Options) pickJobToLog(client kubernetes.Interface, ns, selector string, jobs []batchv1.Job) error {
var names []string
m := map[string]*batchv1.Job{}
for i := range jobs {
Expand All @@ -462,7 +461,7 @@ func (o *Options) pickJobToLog(client kubernetes.Interface, ns string, selector

func toJobName(j *batchv1.Job, number int) string {
status := JobStatus(j)
d := time.Now().Sub(j.CreationTimestamp.Time).Round(time.Minute)
d := time.Since(j.CreationTimestamp.Time).Round(time.Minute)
return fmt.Sprintf("#%d started %s %s", number, d.String(), status)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (o *Options) switchNamespace(ns string) error {
if err != nil {
return errors.Wrapf(err, "failed to update the kube config to namepace %s", ns)
}
log.Logger().Infof("switched to namespace %s so that you can start to create or import projects into Jenkins X: https://jenkins-x.io/docs/v3/create-project/", termcolor.ColorInfo(ns))
log.Logger().Infof("switched to namespace %s so that you can start to create or import projects into Jenkins X: https://jenkins-x.io/v3/develop/create-project/", termcolor.ColorInfo(ns))
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/reqhelpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ func applyDefaults(cmd *cobra.Command, r *jxcore.RequirementsConfig, flags *Requ
}

gitKind := r.Cluster.GitKind
gitKinds := append(giturl.KindGits, "fake")
if gitKind != "" && stringhelpers.StringArrayIndex(gitKinds, gitKind) < 0 {
giturl.KindGits = append(giturl.KindGits, "fake")
if gitKind != "" && stringhelpers.StringArrayIndex(giturl.KindGits, gitKind) < 0 {
return nil, options.InvalidOption("git-kind", gitKind, giturl.KindGits)
}

Expand Down