Skip to content

Commit

Permalink
Enable golangci-lint in build tests
Browse files Browse the repository at this point in the history
Adopt similar config to other Tekton projects to run golangci-lint
in the build tests job.
  • Loading branch information
AlanGreene authored and tekton-robot committed Oct 4, 2022
1 parent 41a1469 commit f0eba40
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 24 deletions.
Empty file added .errcheck.txt
Empty file.
39 changes: 39 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
linters-settings:
errcheck:
exclude: .errcheck.txt
linters:
disable-all: true
enable:
- deadcode
- errcheck
- gofmt
- goimports
- gomodguard
- gosec
- gocritic
- revive
- misspell
- unconvert
- depguard
output:
uniq-by-line: false
issues:
exclude-rules:
- path: _test\.go
linters:
- errcheck
- gosec
max-issues-per-linter: 0
max-same-issues: 0
include:
# Enable off-by-default rules for revive requiring that all exported elements have a properly formatted comment.
- EXC0012
- EXC0014
run:
issues-exit-code: 1
build-tags:
- e2e
skip-dirs:
- vendor
timeout: 10m
modules-download-mode: mod
4 changes: 2 additions & 2 deletions cmd/dashboard/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2021 The Tekton Authors
Copyright 2019-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down Expand Up @@ -110,5 +110,5 @@ func main() {
}

logging.Log.Infof("Starting to serve on %s", l.Addr().String())
server.ServeOnListener(l)
logging.Log.Fatal(server.ServeOnListener(l))
}
6 changes: 5 additions & 1 deletion pkg/csrf/csrf.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Tekton Authors
Copyright 2021-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down Expand Up @@ -41,8 +41,10 @@ type options struct {
HeaderName string
}

// Option contains configuration for the CSRF wrapper
type Option func(*csrf)

// Protect wraps the provided Handler with CSRF protection
func Protect(opts ...Option) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
cs := parseOptions(h, opts...)
Expand Down Expand Up @@ -90,12 +92,14 @@ func parseOptions(h http.Handler, opts ...Option) *csrf {
return cs
}

// ErrorHandler configures the provided error handler on the CSRF wrapper
func ErrorHandler(h http.Handler) Option {
return func(cs *csrf) {
cs.opts.ErrorHandler = h
}
}

// HeaderName configures the header name on the CSRF wrapper
func HeaderName(header string) Option {
return func(cs *csrf) {
cs.opts.HeaderName = header
Expand Down
8 changes: 6 additions & 2 deletions pkg/endpoints/cluster.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2021 The Tekton Authors
Copyright 2019-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -16,6 +16,8 @@ package endpoints
import (
"encoding/json"
"net/http"

"github.com/tektoncd/dashboard/pkg/logging"
)

// Properties : properties we want to be able to retrieve via REST
Expand Down Expand Up @@ -69,5 +71,7 @@ func (r Resource) GetProperties(response http.ResponseWriter, request *http.Requ
response.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
response.Header().Set("Pragma", "no-cache")
response.Header().Set("Expires", "0")
json.NewEncoder(response).Encode(properties)
if err := json.NewEncoder(response).Encode(properties); err != nil {
logging.Log.Error("Failed encoding properties")
}
}
3 changes: 2 additions & 1 deletion pkg/endpoints/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020-2021 The Tekton Authors
Copyright 2020-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down Expand Up @@ -61,6 +61,7 @@ func getTriggersVersion(r Resource, namespace string) string {
return version
}

// IsTriggersInstalled returns true if it can detect a Triggers install in the cluster, false otherwise
func IsTriggersInstalled(r Resource, namespace string) bool {
version := getTriggersVersion(r, namespace)
return version != ""
Expand Down
3 changes: 2 additions & 1 deletion pkg/endpoints/externallogs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2021 The Tekton Authors
Copyright 2019-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -21,6 +21,7 @@ import (
"github.com/tektoncd/dashboard/pkg/utils"
)

// LogsProxy forwards requests to the configured external log provider and proxies the response to the client
func (r Resource) LogsProxy(response http.ResponseWriter, request *http.Request) {
parsedURL, err := url.Parse(request.URL.String())
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/endpoints/health.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2021 The Tekton Authors
Copyright 2019-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -17,6 +17,7 @@ import (
"net/http"
)

// CheckHealth responds with a status code 200 signalling that the application can receive requests
func (r Resource) CheckHealth(response http.ResponseWriter, request *http.Request) {
// A method here so there's scope for doing anything fancy e.g. checking anything else
response.WriteHeader(http.StatusOK)
Expand Down
5 changes: 2 additions & 3 deletions pkg/endpoints/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2021 The Tekton Authors
Copyright 2019-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down Expand Up @@ -51,8 +51,7 @@ func (o Options) GetTriggersNamespace() string {
return o.InstallNamespace
}

// Store all types here that are reused throughout files
// Wrapper around all necessary clients used for endpoints
// Resource is a wrapper around all necessary clients and config used for endpoints
type Resource struct {
Config *rest.Config
K8sClient k8sclientset.Interface
Expand Down
9 changes: 6 additions & 3 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Tekton Authors
Copyright 2019-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -21,9 +21,12 @@ import (
// Log is our logger for use elsewhere
var Log = zap.NewNop().Sugar()

// InitLogger creates and exposes the logger instance
func InitLogger(level, format string) {
logger := createLogger(level, format)
defer logger.Sync()
defer func() {
_ = logger.Sync()
}()
Log = logger
}

Expand All @@ -37,7 +40,7 @@ func createLogger(level, format string) *zap.SugaredLogger {
}

coreLevel := zapcore.InfoLevel
coreLevel.Set(level)
_ = coreLevel.Set(level)

config.Level.SetLevel(coreLevel)

Expand Down
9 changes: 5 additions & 4 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func Register(r endpoints.Resource, cfg *rest.Config) (*Server, error) {
func NewProxyHandler(apiProxyPrefix string, cfg *rest.Config, keepalive time.Duration) (http.Handler, error) {
host := cfg.Host
if !strings.HasSuffix(host, "/") {
host = host + "/"
host += "/"
}
target, err := url.Parse(host)
if err != nil {
Expand Down Expand Up @@ -188,7 +188,8 @@ func (s *Server) ServeOnListener(l net.Listener) error {
CSRF := csrf.Protect()

server := http.Server{
Handler: CSRF(s.handler),
Handler: CSRF(s.handler),
ReadHeaderTimeout: 30 * time.Second,
}
return server.Serve(l)
}
Expand Down Expand Up @@ -220,8 +221,8 @@ func protectWebSocket(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if !checkUpgradeSameOrigin(req) {
origin := req.Header.Get("Origin")
origin = strings.Replace(origin, "\n", "", -1)
origin = strings.Replace(origin, "\r", "", -1)
origin = strings.ReplaceAll(origin, "\n", "")
origin = strings.ReplaceAll(origin, "\r", "")
logging.Log.Warnf("websocket: Connection upgrade blocked, Host: %s, Origin: %s", req.Host, origin)
http.Error(w, "websocket: request origin not allowed", http.StatusForbidden)
return
Expand Down
3 changes: 2 additions & 1 deletion pkg/utils/flushwriter.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 The Tekton Authors
Copyright 2020-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand Down Expand Up @@ -31,6 +31,7 @@ func (fw *flushWriter) Write(p []byte) (n int, err error) {
return
}

// MakeFlushWriter ensures content is flushed immediately after being written
func MakeFlushWriter(writer io.Writer) io.Writer {
if flusher, ok := writer.(http.Flusher); ok {
return &flushWriter{
Expand Down
9 changes: 7 additions & 2 deletions pkg/utils/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
logging "github.com/tektoncd/dashboard/pkg/logging"
)

// Proxy forwards requests to an upstream provider and proxies the response content and headers back to the caller
func Proxy(request *http.Request, response http.ResponseWriter, url string, client *http.Client) (int, error) {
req, err := http.NewRequest(request.Method, url, request.Body)

Expand Down Expand Up @@ -54,9 +55,13 @@ func Proxy(request *http.Request, response http.ResponseWriter, url string, clie
response.WriteHeader(resp.StatusCode)
contentLength := resp.Header.Get("Content-Length")
if contentLength == "" {
io.Copy(MakeFlushWriter(response), resp.Body)
if _, err := io.Copy(MakeFlushWriter(response), resp.Body); err != nil {
logging.Log.Error("Failed copying response")
}
} else {
io.Copy(response, resp.Body)
if _, err := io.Copy(response, resp.Body); err != nil {
logging.Log.Error("Failed copying response")
}
}

return resp.StatusCode, nil
Expand Down
9 changes: 6 additions & 3 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019-2021 The Tekton Authors
Copyright 2019-2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -22,8 +22,11 @@ import (

// RespondError - logs and writes an error response with a desired status code
func RespondError(response http.ResponseWriter, err error, statusCode int) {
logging.Log.Error("Error: ", strings.Replace(err.Error(), "/", "", -1))
logging.Log.Error("Error: ", strings.ReplaceAll(err.Error(), "/", ""))
response.Header().Set("Content-Type", "text/plain")
response.WriteHeader(statusCode)
response.Write([]byte(err.Error()))
_, err = response.Write([]byte(err.Error()))
if err != nil {
logging.Log.Error("Write failed: %v", err)
}
}
12 changes: 12 additions & 0 deletions test/presubmit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ source $(dirname $0)/../vendor/github.com/tektoncd/plumbing/scripts/presubmit-te
# - post_integration_tests : runs after the integration-test function
#

function post_build_tests() {
header "Testing if golint has been done"
golangci-lint --color=never run

if [[ $? != 0 ]]; then
results_banner "Go Lint" 1
exit 1
fi

results_banner "Go Lint" 0
}

function utility_install() {
# Install envsubst
apt-get install gettext-base
Expand Down

0 comments on commit f0eba40

Please sign in to comment.