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

Add imports checks to linter #3056

Merged
merged 10 commits into from
Dec 29, 2023
Merged
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
89 changes: 88 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,92 @@ linters-settings:
- ^log.Fatal().*$
errorlint:
errorf-multi: true
depguard:
rules:
pipeline:
list-mode: lax
files:
- '**/pipeline/**/*.go'
- '**/pipeline/*.go'
- '!**/cli/pipeline/*.go'
- '!**/cli/pipeline/**/*.go'
- '!**/server/pipeline/*.go'
- '!**/server/pipeline/**/*.go'
deny:
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/agent'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cli'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cmd'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/server'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/web'
shared:
list-mode: lax
files:
- '**/shared/**/*.go'
- '**/shared/*.go'
- '!**/pipeline/shared/*.go'
- '!**/pipeline/shared/**/*.go'
deny:
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/agent'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cli'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cmd'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/pipeline'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/server'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/web'
woodpecker-go:
list-mode: lax
files:
- '**/woodpecker-go/woodpecker/**/*.go'
- '**/woodpecker-go/woodpecker/*.go'
deny:
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/agent'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cli'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cmd'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/pipeline'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/server'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/shared'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/web'
agent:
list-mode: lax
files:
- '**/agent/**/*.go'
- '**/agent/*.go'
- '**/cmd/agent/**/*.go'
- '**/cmd/agent/*.go'
deny:
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cli'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cmd/cli'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cmd/server'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/server'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/web'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker'
cli:
list-mode: lax
files:
- '**/cli/**/*.go'
- '**/cli/*.go'
- '**/cmd/cli/**/*.go'
- '**/cmd/cli/*.go'
deny:
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/agent'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/server'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cmd/agent'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cmd/server'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/web'
server:
list-mode: lax
files:
- '**/server/**/*.go'
- '**/server/*.go'
- '**/cmd/server/**/*.go'
- '**/cmd/server/*.go'
- '**/web/**/*.go'
- '**/web/*.go'
deny:
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/agent'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cli'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cmd/agent'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/cmd/cli'
- pkg: 'go.woodpecker-ci.org/woodpecker/v2/woodpecker-go/woodpecker'

linters:
disable-all: true
Expand All @@ -37,9 +123,10 @@ linters:
- errorlint
- forbidigo
- zerologlint
- depguard

run:
timeout: 5m
timeout: 15m

issues:
exclude-rules:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ ui-dependencies: ## Install UI dependencies
.PHONY: lint
lint: install-tools ## Lint code
@echo "Running golangci-lint"
golangci-lint run --timeout 15m
golangci-lint run

lint-ui: ## Lint UI code
(cd web/; pnpm install)
Expand Down
2 changes: 1 addition & 1 deletion cli/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func runExec(c *cli.Context, file, repoPath string) error {

axes, err := matrix.ParseString(string(dat))
if err != nil {
return fmt.Errorf("Parse matrix fail")
return fmt.Errorf("parse matrix fail")
}

if len(axes) == 0 {
Expand Down
32 changes: 32 additions & 0 deletions pipeline/frontend/metadata/subsitution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2023 Woodpecker 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metadata

import (
"fmt"
"strings"

"github.com/drone/envsubst"
)

func EnvVarSubst(yaml string, environ map[string]string) (string, error) {
return envsubst.Eval(yaml, func(name string) string {
env := environ[name]
if strings.Contains(env, "\n") {
env = fmt.Sprintf("%q", env)
}
return env
})
}
47 changes: 47 additions & 0 deletions pipeline/frontend/metadata/subsitution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2023 Woodpecker 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metadata

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEnvVarSubst(t *testing.T) {
testCases := []struct {
name string
yaml string
environ map[string]string
want string
}{{
name: "simple substitution",
yaml: `steps:
step1:
image: ${HELLO_IMAGE}`,
environ: map[string]string{"HELLO_IMAGE": "hello-world"},
want: `steps:
step1:
image: hello-world`,
}}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
result, err := EnvVarSubst(testCase.yaml, testCase.environ)
assert.NoError(t, err)
assert.EqualValues(t, testCase.want, result)
})
}
}
3 changes: 1 addition & 2 deletions pipeline/frontend/yaml/constraint/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"

"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
)

Expand Down Expand Up @@ -543,7 +542,7 @@ func TestConstraints(t *testing.T) {

for _, test := range testdata {
t.Run(test.desc, func(t *testing.T) {
conf, err := frontend.EnvVarSubst(test.conf, test.with.Environ())
conf, err := metadata.EnvVarSubst(test.conf, test.with.Environ())
assert.NoError(t, err)
c := parseConstraints(t, conf)
got, err := c.Match(test.with, false, test.env)
Expand Down
4 changes: 2 additions & 2 deletions server/pipeline/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"crypto/sha256"
"fmt"

"go.woodpecker-ci.org/woodpecker/v2/pipeline"
forge_types "go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline/stepbuilder"
"go.woodpecker-ci.org/woodpecker/v2/server/store"
)

Expand All @@ -32,7 +32,7 @@ func findOrPersistPipelineConfig(store store.Store, currentPipeline *model.Pipel
RepoID: currentPipeline.RepoID,
Data: forgeYamlConfig.Data,
Hash: sha,
Name: pipeline.SanitizePath(forgeYamlConfig.Name),
Name: stepbuilder.SanitizePath(forgeYamlConfig.Name),
}
err = store.ConfigCreate(conf)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions server/pipeline/items.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ import (

"github.com/rs/zerolog/log"

"go.woodpecker-ci.org/woodpecker/v2/pipeline"
pipeline_errors "go.woodpecker-ci.org/woodpecker/v2/pipeline/errors"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/yaml/compiler"
"go.woodpecker-ci.org/woodpecker/v2/server"
forge_types "go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline/stepbuilder"
"go.woodpecker-ci.org/woodpecker/v2/server/store"
)

func parsePipeline(store store.Store, currentPipeline *model.Pipeline, user *model.User, repo *model.Repo, yamls []*forge_types.FileMeta, envs map[string]string) ([]*pipeline.Item, error) {
func parsePipeline(store store.Store, currentPipeline *model.Pipeline, user *model.User, repo *model.Repo, yamls []*forge_types.FileMeta, envs map[string]string) ([]*stepbuilder.Item, error) {
netrc, err := server.Config.Services.Forge.Netrc(user, repo)
if err != nil {
log.Error().Err(err).Msg("Failed to generate netrc file")
Expand Down Expand Up @@ -66,7 +66,7 @@ func parsePipeline(store store.Store, currentPipeline *model.Pipeline, user *mod
envs[k] = v
}

b := pipeline.StepBuilder{
b := stepbuilder.StepBuilder{
Repo: repo,
Curr: currentPipeline,
Last: last,
Expand All @@ -89,7 +89,7 @@ func parsePipeline(store store.Store, currentPipeline *model.Pipeline, user *mod
func createPipelineItems(c context.Context, store store.Store,
currentPipeline *model.Pipeline, user *model.User, repo *model.Repo,
yamls []*forge_types.FileMeta, envs map[string]string,
) (*model.Pipeline, []*pipeline.Item, error) {
) (*model.Pipeline, []*stepbuilder.Item, error) {
pipelineItems, err := parsePipeline(store, currentPipeline, user, repo, yamls, envs)
if pipeline_errors.HasBlockingErrors(err) {
currentPipeline, uerr := UpdateToStatusError(store, *currentPipeline, err)
Expand All @@ -113,7 +113,7 @@ func createPipelineItems(c context.Context, store store.Store,
// setPipelineStepsOnPipeline is the link between pipeline representation in "pipeline package" and server
// to be specific this func currently is used to convert the pipeline.Item list (crafted by StepBuilder.Build()) into
// a pipeline that can be stored in the database by the server
func setPipelineStepsOnPipeline(pipeline *model.Pipeline, pipelineItems []*pipeline.Item) *model.Pipeline {
func setPipelineStepsOnPipeline(pipeline *model.Pipeline, pipelineItems []*stepbuilder.Item) *model.Pipeline {
var pidSequence int
for _, item := range pipelineItems {
if pidSequence < item.Workflow.PID {
Expand Down
2 changes: 1 addition & 1 deletion server/pipeline/items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package pipeline
import (
"testing"

sharedPipeline "go.woodpecker-ci.org/woodpecker/v2/pipeline"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
sharedPipeline "go.woodpecker-ci.org/woodpecker/v2/server/pipeline/stepbuilder"
)

func TestSetPipelineStepsOnPipeline(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions server/pipeline/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (
"encoding/json"
"fmt"

"go.woodpecker-ci.org/woodpecker/v2/pipeline"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
"go.woodpecker-ci.org/woodpecker/v2/server"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline/stepbuilder"
)

func queuePipeline(repo *model.Repo, pipelineItems []*pipeline.Item) error {
func queuePipeline(repo *model.Repo, pipelineItems []*stepbuilder.Item) error {
var tasks []*model.Task
for _, item := range pipelineItems {
if item.Workflow.State == model.StatusSkipped {
Expand Down Expand Up @@ -53,7 +53,7 @@ func queuePipeline(repo *model.Repo, pipelineItems []*pipeline.Item) error {
return server.Config.Services.Queue.PushAtOnce(context.Background(), tasks)
}

func taskIds(dependsOn []string, pipelineItems []*pipeline.Item) (taskIds []string) {
func taskIds(dependsOn []string, pipelineItems []*stepbuilder.Item) (taskIds []string) {
for _, dep := range dependsOn {
for _, pipelineItem := range pipelineItems {
if pipelineItem.Workflow.Name == dep {
Expand Down
4 changes: 2 additions & 2 deletions server/pipeline/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (

"github.com/rs/zerolog/log"

"go.woodpecker-ci.org/woodpecker/v2/pipeline"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/server/pipeline/stepbuilder"
"go.woodpecker-ci.org/woodpecker/v2/server/store"
)

// start a pipeline, make sure it was stored persistent in the store before
func start(ctx context.Context, store store.Store, activePipeline *model.Pipeline, user *model.User, repo *model.Repo, pipelineItems []*pipeline.Item) (*model.Pipeline, error) {
func start(ctx context.Context, store store.Store, activePipeline *model.Pipeline, user *model.User, repo *model.Repo, pipelineItems []*stepbuilder.Item) (*model.Pipeline, error) {
// call to cancel previous pipelines if needed
if err := cancelPreviousPipelines(ctx, store, activePipeline, repo, user); err != nil {
// should be not breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package frontend
package stepbuilder

import (
"fmt"
"net/url"
"strings"

"github.com/drone/envsubst"

"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
"go.woodpecker-ci.org/woodpecker/v2/version"
)

func EnvVarSubst(yaml string, environ map[string]string) (string, error) {
return envsubst.Eval(yaml, func(name string) string {
env := environ[name]
if strings.Contains(env, "\n") {
env = fmt.Sprintf("%q", env)
}
return env
})
}

// MetadataFromStruct return the metadata from a pipeline will run with.
func MetadataFromStruct(forge metadata.ServerForge, repo *model.Repo, pipeline, last *model.Pipeline, workflow *model.Workflow, sysURL string) metadata.Metadata {
host := sysURL
Expand Down
Loading