From 1f6e6fd347bac6267aa1122f7e12c0958aa8a38d Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Thu, 27 Jan 2022 10:48:58 +0100 Subject: [PATCH] fix typecheck golangci assertions Closes #404 Signed-off-by: Chmouel Boudjnah --- pkg/matcher/annotation_tasks_install.go | 7 ++++++- pkg/provider/bitbucketcloud/test/bbcloudtest.go | 8 ++++++-- pkg/provider/bitbucketserver/acl.go | 6 +++++- pkg/provider/bitbucketserver/pagination.go | 7 ++++++- test/bitbucket_cloud_pullrequest_test.go | 6 +++++- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/pkg/matcher/annotation_tasks_install.go b/pkg/matcher/annotation_tasks_install.go index fee94170a..c540641c3 100644 --- a/pkg/matcher/annotation_tasks_install.go +++ b/pkg/matcher/annotation_tasks_install.go @@ -33,7 +33,12 @@ func (rt RemoteTasks) convertTotask(data string) (*tektonv1beta1.Task, error) { return nil, fmt.Errorf("we have a task that is not looking like a kubernetes resource: task: %s resource: %w", data, err) } - return obj.(*tektonv1beta1.Task), nil + task, ok := obj.(*tektonv1beta1.Task) + if !ok { + return nil, fmt.Errorf("this doesn't seem to be a proper task") + } + + return task, nil } func (rt RemoteTasks) getTask(ctx context.Context, providerintf provider.Interface, task string) (*tektonv1beta1.Task, error) { diff --git a/pkg/provider/bitbucketcloud/test/bbcloudtest.go b/pkg/provider/bitbucketcloud/test/bbcloudtest.go index feb7799d0..4a34e3878 100644 --- a/pkg/provider/bitbucketcloud/test/bbcloudtest.go +++ b/pkg/provider/bitbucketcloud/test/bbcloudtest.go @@ -54,7 +54,9 @@ func SetupBBCloudClient(t *testing.T) (*bitbucket.Client, *http.ServeMux, func() func MuxComments(t *testing.T, mux *http.ServeMux, event *info.Event, comments []types.Comment) { assert.Assert(t, event.Event != nil) - prID := fmt.Sprintf("%d", event.Event.(*types.PullRequestEvent).PullRequest.ID) + pr, ok := event.Event.(*types.PullRequestEvent) + assert.Assert(t, ok) + prID := fmt.Sprintf("%d", pr.PullRequest.ID) mux.HandleFunc("/repositories/"+event.Organization+"/"+event.Repository+"/pullrequests/"+prID+"/comments/", func(rw http.ResponseWriter, r *http.Request) { members := &types.Comments{ @@ -146,7 +148,9 @@ func MuxCreateCommitstatus(t *testing.T, mux *http.ServeMux, event *info.Event, func MuxCreateComment(t *testing.T, mux *http.ServeMux, event *info.Event, expectedCommentSubstr string) { assert.Assert(t, event.Event != nil) - prID := fmt.Sprintf("%d", event.Event.(*types.PullRequestEvent).PullRequest.ID) + prev, ok := event.Event.(*types.PullRequestEvent) + assert.Assert(t, ok) + prID := fmt.Sprintf("%d", prev.PullRequest.ID) path := fmt.Sprintf("/repositories/%s/%s/pullrequests/%s/comments", event.Organization, event.Repository, prID) mux.HandleFunc(path, func(rw http.ResponseWriter, r *http.Request) { diff --git a/pkg/provider/bitbucketserver/acl.go b/pkg/provider/bitbucketserver/acl.go index 5d427a431..00a9d5de1 100644 --- a/pkg/provider/bitbucketserver/acl.go +++ b/pkg/provider/bitbucketserver/acl.go @@ -52,7 +52,11 @@ func (v *Provider) checkOkToTestCommentFromApprovedMember(event *info.Event) (bo for _, comment := range allPages { activities := &activitiesTypes{} - err := json.Unmarshal(comment.([]byte), activities) + cbyte, ok := comment.([]byte) + if !ok { + return false, fmt.Errorf("cannot convert comment to bytes") + } + err := json.Unmarshal(cbyte, activities) if err != nil { return false, err } diff --git a/pkg/provider/bitbucketserver/pagination.go b/pkg/provider/bitbucketserver/pagination.go index e73b0b928..f94953cfd 100644 --- a/pkg/provider/bitbucketserver/pagination.go +++ b/pkg/provider/bitbucketserver/pagination.go @@ -43,7 +43,12 @@ func paginate(apiResultfunc apiResultfunc) ([]interface{}, error) { if !ok { break } - if isLastPage.(bool) { + isLastPageb, ok := isLastPage.(bool) + if !ok { + break + } + + if isLastPageb { break } } diff --git a/test/bitbucket_cloud_pullrequest_test.go b/test/bitbucket_cloud_pullrequest_test.go index 301ca1011..2be09102a 100644 --- a/test/bitbucket_cloud_pullrequest_test.go +++ b/test/bitbucket_cloud_pullrequest_test.go @@ -37,7 +37,11 @@ func TestBitbucketCloudPullRequest(t *testing.T) { pr, repobranch := createPR(t, bprovider, runcnx, bcrepo, opts, title, targetNS, targetRefName) defer bitbucketTearDown(ctx, t, runcnx, bprovider, opts, pr.ID, targetRefName, targetNS) - checkSuccess(ctx, t, runcnx, opts, pullRequestEvent, targetNS, repobranch.Target["hash"].(string), title) + + hash, ok := repobranch.Target["hash"].(string) + assert.Assert(t, ok) + + checkSuccess(ctx, t, runcnx, opts, pullRequestEvent, targetNS, hash, title) } func createPR(t *testing.T, bprovider bitbucketcloud.Provider, runcnx *params.Run, bcrepo *bitbucket.Repository, opts E2EOptions, title, targetNS, targetRefName string) (*types.PullRequest, *bitbucket.RepositoryBranch) {