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

Enable golangci linter forcetypeassert #3168

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ linters:
- gocritic
- nolintlint
- stylecheck
- forcetypeassert

run:
timeout: 15m
14 changes: 12 additions & 2 deletions pipeline/backend/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ func (e *kube) WaitStep(ctx context.Context, step *types.Step, taskUUID string)
finished := make(chan bool)

podUpdated := func(old, new any) {
pod := new.(*v1.Pod)
pod, ok := new.(*v1.Pod)
if !ok {
log.Error().Msgf("could not parse pod: %v", new)
return
}

if pod.Name == podName {
if isImagePullBackOffState(pod) {
finished <- true
Expand Down Expand Up @@ -279,7 +284,12 @@ func (e *kube) TailStep(ctx context.Context, step *types.Step, taskUUID string)
up := make(chan bool)

podUpdated := func(old, new any) {
pod := new.(*v1.Pod)
pod, ok := new.(*v1.Pod)
if !ok {
log.Error().Msgf("could not parse pod: %v", new)
return
}

if pod.Name == podName {
switch pod.Status.Phase {
case v1.PodRunning, v1.PodSucceeded, v1.PodFailed:
Expand Down
8 changes: 7 additions & 1 deletion pipeline/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,13 @@ func (e *local) getState(taskUUID string) (*workflowState, error) {
if !ok {
return nil, ErrWorkflowStateNotFound
}
return state.(*workflowState), nil

s, ok := state.(*workflowState)
if !ok {
return nil, fmt.Errorf("could not parse state: %v", state)
}

return s, nil
}

func (e *local) saveState(taskUUID string, state *workflowState) {
Expand Down
6 changes: 5 additions & 1 deletion pipeline/frontend/yaml/constraint/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ func (c *Constraint) Match(m metadata.Metadata, global bool, env map[string]stri
if err != nil {
return false, err
}
match = match && result.(bool)
bresult, ok := result.(bool)
if !ok {
return false, fmt.Errorf("could not parse result: %v", result)
}
match = match && bresult
}

return match, nil
Expand Down
18 changes: 15 additions & 3 deletions pipeline/frontend/yaml/types/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func handleNetwork(name string, value any) (*Network, error) {
network := &Network{
Name: name,
}

var ok bool
for mapKey, mapValue := range v {
switch mapKey {
case "aliases":
Expand All @@ -99,12 +101,22 @@ func handleNetwork(name string, value any) (*Network, error) {
}
network.Aliases = []string{}
for _, alias := range aliases {
network.Aliases = append(network.Aliases, alias.(string))
a, ok := alias.(string)
if !ok {
return &Network{}, fmt.Errorf("cannot unmarshal '%v' to type %T into a string value", aliases, aliases)
}
network.Aliases = append(network.Aliases, a)
}
case "ipv4_address":
network.IPv4Address = mapValue.(string)
network.IPv4Address, ok = mapValue.(string)
if !ok {
return &Network{}, fmt.Errorf("cannot unmarshal '%v' to type %T into a string value", network, network)
}
case "ipv6_address":
network.IPv6Address = mapValue.(string)
network.IPv6Address, ok = mapValue.(string)
if !ok {
return &Network{}, fmt.Errorf("cannot unmarshal '%v' to type %T into a string value", network, network)
}
default:
// Ignorer unknown keys ?
continue
Expand Down
10 changes: 6 additions & 4 deletions server/forge/bitbucket/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ func Test_bitbucket(t *testing.T) {

g.It("Should return client with default endpoint", func() {
forge, _ := New(&Opts{Client: "4vyW6b49Z", Secret: "a5012f6c6"})
g.Assert(forge.(*config).url).Equal(DefaultURL)
g.Assert(forge.(*config).API).Equal(DefaultAPI)
g.Assert(forge.(*config).Client).Equal("4vyW6b49Z")
g.Assert(forge.(*config).Secret).Equal("a5012f6c6")

f, _ := forge.(*config)
g.Assert(f.url).Equal(DefaultURL)
g.Assert(f.API).Equal(DefaultAPI)
g.Assert(f.Client).Equal("4vyW6b49Z")
g.Assert(f.Secret).Equal("a5012f6c6")
})

g.It("Should return the netrc file", func() {
Expand Down
6 changes: 4 additions & 2 deletions server/forge/gitea/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ func Test_gitea(t *testing.T) {
URL: "http://localhost:8080",
SkipVerify: true,
})
g.Assert(forge.(*Gitea).url).Equal("http://localhost:8080")
g.Assert(forge.(*Gitea).SkipVerify).Equal(true)

f, _ := forge.(*Gitea)
g.Assert(f.url).Equal("http://localhost:8080")
g.Assert(f.SkipVerify).Equal(true)
})
g.It("Should handle malformed url", func() {
_, err := New(Opts{URL: "%gh&%ij"})
Expand Down
3 changes: 2 additions & 1 deletion server/forge/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ func (c *client) newClientToken(ctx context.Context, token string) *github.Clien
)
tc := oauth2.NewClient(ctx, ts)
if c.SkipVerify {
tc.Transport.(*oauth2.Transport).Base = &http.Transport{
tp, _ := tc.Transport.(*oauth2.Transport)
tp.Base = &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
Expand Down
11 changes: 6 additions & 5 deletions server/forge/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ func Test_github(t *testing.T) {
Secret: "I1NiIsInR5",
SkipVerify: true,
})
g.Assert(forge.(*client).url).Equal("http://localhost:8080")
g.Assert(forge.(*client).API).Equal("http://localhost:8080/api/v3/")
g.Assert(forge.(*client).Client).Equal("0ZXh0IjoiI")
g.Assert(forge.(*client).Secret).Equal("I1NiIsInR5")
g.Assert(forge.(*client).SkipVerify).Equal(true)
f, _ := forge.(*client)
g.Assert(f.url).Equal("http://localhost:8080")
g.Assert(f.API).Equal("http://localhost:8080/api/v3/")
g.Assert(f.Client).Equal("0ZXh0IjoiI")
g.Assert(f.Secret).Equal("I1NiIsInR5")
g.Assert(f.SkipVerify).Equal(true)
})
})

Expand Down
16 changes: 9 additions & 7 deletions server/queue/fifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,12 @@ func (q *fifo) Info(_ context.Context) InfoT {
stats.Stats.Complete = 0 // TODO: implement this

for e := q.pending.Front(); e != nil; e = e.Next() {
stats.Pending = append(stats.Pending, e.Value.(*model.Task))
task, _ := e.Value.(*model.Task)
stats.Pending = append(stats.Pending, task)
}
for e := q.waitingOnDeps.Front(); e != nil; e = e.Next() {
stats.WaitingOnDeps = append(stats.WaitingOnDeps, e.Value.(*model.Task))
task, _ := e.Value.(*model.Task)
stats.WaitingOnDeps = append(stats.WaitingOnDeps, task)
}
for _, entry := range q.running {
stats.Running = append(stats.Running, entry.item)
Expand Down Expand Up @@ -243,7 +245,7 @@ func (q *fifo) process() {
q.resubmitExpiredPipelines()
q.filterWaiting()
for pending, worker := q.assignToWorker(); pending != nil && worker != nil; pending, worker = q.assignToWorker() {
task := pending.Value.(*model.Task)
task, _ := pending.Value.(*model.Task)
task.AgentID = worker.agentID
delete(q.workers, worker)
q.pending.Remove(pending)
Expand All @@ -261,7 +263,7 @@ func (q *fifo) filterWaiting() {
var nextWaiting *list.Element
for e := q.waitingOnDeps.Front(); e != nil; e = nextWaiting {
nextWaiting = e.Next()
task := e.Value.(*model.Task)
task, _ := e.Value.(*model.Task)
q.pending.PushBack(task)
}

Expand All @@ -271,7 +273,7 @@ func (q *fifo) filterWaiting() {
var nextPending *list.Element
for e := q.pending.Front(); e != nil; e = nextPending {
nextPending = e.Next()
task := e.Value.(*model.Task)
task, _ := e.Value.(*model.Task)
if q.depsInQueue(task) {
log.Debug().Msgf("queue: waiting due to unmet dependencies %v", task.ID)
q.waitingOnDeps.PushBack(task)
Expand All @@ -289,7 +291,7 @@ func (q *fifo) assignToWorker() (*list.Element, *worker) {
var next *list.Element
for e := q.pending.Front(); e != nil; e = next {
next = e.Next()
task := e.Value.(*model.Task)
task, _ := e.Value.(*model.Task)
log.Debug().Msgf("queue: trying to assign task: %v with deps %v", task.ID, task.Dependencies)

for w := range q.workers {
Expand Down Expand Up @@ -372,7 +374,7 @@ func (q *fifo) removeFromPending(taskID string) {
var next *list.Element
for e := q.pending.Front(); e != nil; e = next {
next = e.Next()
task := e.Value.(*model.Task)
task, _ := e.Value.(*model.Task)
if task.ID == taskID {
log.Debug().Msgf("queue: %s is removed from pending", taskID)
q.pending.Remove(e)
Expand Down
22 changes: 11 additions & 11 deletions server/queue/fifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestFifo(t *testing.T) {
func TestFifoExpire(t *testing.T) {
want := &model.Task{ID: "1"}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
q.extension = 0
assert.NoError(t, q.Push(noContext, want))
info := q.Info(noContext)
Expand All @@ -95,7 +95,7 @@ func TestFifoExpire(t *testing.T) {
func TestFifoWait(t *testing.T) {
want := &model.Task{ID: "1"}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
assert.NoError(t, q.Push(noContext, want))

got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestFifoDependencies(t *testing.T) {
DepStatus: make(map[string]model.StatusValue),
}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task1}))

got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestFifoErrors(t *testing.T) {
RunOn: []string{"success", "failure"},
}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))

got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestFifoErrors2(t *testing.T) {
DepStatus: make(map[string]model.StatusValue),
}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))

for i := 0; i < 2; i++ {
Expand Down Expand Up @@ -280,7 +280,7 @@ func TestFifoErrorsMultiThread(t *testing.T) {
DepStatus: make(map[string]model.StatusValue),
}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))

obtainedWorkCh := make(chan *model.Task)
Expand Down Expand Up @@ -371,7 +371,7 @@ func TestFifoTransitiveErrors(t *testing.T) {
DepStatus: make(map[string]model.StatusValue),
}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))

got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
Expand Down Expand Up @@ -421,7 +421,7 @@ func TestFifoCancel(t *testing.T) {
RunOn: []string{"success", "failure"},
}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))

_, _ = q.Poll(noContext, 1, func(*model.Task) bool { return true })
Expand All @@ -441,7 +441,7 @@ func TestFifoPause(t *testing.T) {
ID: "1",
}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand Down Expand Up @@ -473,7 +473,7 @@ func TestFifoPauseResume(t *testing.T) {
ID: "1",
}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
q.Pause()
assert.NoError(t, q.Push(noContext, task1))
q.Resume()
Expand All @@ -499,7 +499,7 @@ func TestWaitingVsPending(t *testing.T) {
RunOn: []string{"success", "failure"},
}

q := New(context.Background()).(*fifo)
q, _ := New(context.Background()).(*fifo)
assert.NoError(t, q.PushAtOnce(noContext, []*model.Task{task2, task3, task1}))

got, _ := q.Poll(noContext, 1, func(*model.Task) bool { return true })
Expand Down
2 changes: 1 addition & 1 deletion server/router/middleware/session/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

// AuthorizeAgent authorizes requests from agent to access the queue.
func AuthorizeAgent(c *gin.Context) {
secret := c.MustGet("agent").(string)
secret, _ := c.MustGet("agent").(string)
if secret == "" {
c.String(401, "invalid or empty token.")
return
Expand Down
3 changes: 2 additions & 1 deletion server/store/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type Setter interface {

// FromContext returns the Store associated with this context.
func FromContext(c context.Context) Store {
return c.Value(key).(Store)
store, _ := c.Value(key).(Store)
return store
}

// TryFromContext try to return the Store associated with this context.
Expand Down