Skip to content

Commit c7486aa

Browse files
zakiskchmouel
authored andcommitted
refactor: replace getActivities func call with jenkins-x/go-scm
replaced getActivities api call from bbv1 to jenkinx-x/go-scm Signed-off-by: Zaki Shaikh <[email protected]>
1 parent 857f96d commit c7486aa

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

pkg/provider/bitbucketdatacenter/acl.go

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ package bitbucketdatacenter
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"strings"
87

9-
bbv1 "github.com/gfleury/go-bitbucket-v1"
108
"github.com/openshift-pipelines/pipelines-as-code/pkg/acl"
119
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
12-
)
1310

14-
type activitiesTypes struct{ Values []*bbv1.Activity }
11+
"github.com/jenkins-x/go-scm/scm"
12+
)
1513

1614
func (v *Provider) IsAllowed(ctx context.Context, event *info.Event) (bool, error) {
1715
allowed, err := v.checkMemberShip(ctx, event)
@@ -44,50 +42,42 @@ func (v *Provider) IsAllowedOwnersFile(ctx context.Context, event *info.Event) (
4442
}
4543

4644
func (v *Provider) checkOkToTestCommentFromApprovedMember(ctx context.Context, event *info.Event) (bool, error) {
47-
allPages, err := paginate(func(nextPage int) (*bbv1.APIResponse, error) {
48-
localVarOptionals := map[string]any{
49-
"fromType": "COMMENT",
45+
allComments := []*scm.Comment{}
46+
OrgAndRepo := fmt.Sprintf("%s/%s", event.Organization, event.Repository)
47+
opts := &scm.ListOptions{Page: 1, Size: apiResponseLimit}
48+
for {
49+
comments, _, err := v.ScmClient().PullRequests.ListComments(ctx, OrgAndRepo, v.pullRequestNumber, opts)
50+
if err != nil {
51+
return false, err
5052
}
51-
if nextPage > 0 {
52-
localVarOptionals["start"] = int(nextPage)
53+
54+
allComments = append(allComments, comments...)
55+
56+
if len(comments) < apiResponseLimit {
57+
break
5358
}
54-
// will replace this API call with jenkins-x/go-scm after my PR on go-scm is merged
55-
// https://github.com/jenkins-x/go-scm/pull/494
56-
return v.Client().DefaultApi.GetActivities(v.projectKey, event.Repository, v.pullRequestNumber, localVarOptionals)
57-
})
58-
if err != nil {
59-
return false, err
59+
60+
opts.Page++
6061
}
6162

62-
for _, comment := range allPages {
63-
activities := &activitiesTypes{}
64-
cbyte, ok := comment.([]byte)
65-
if !ok {
66-
return false, fmt.Errorf("cannot convert comment to bytes")
67-
}
68-
err := json.Unmarshal(cbyte, activities)
69-
if err != nil {
70-
return false, err
71-
}
72-
for _, activity := range activities.Values {
73-
if acl.MatchRegexp(acl.OKToTestCommentRegexp, activity.Comment.Text) {
74-
commenterEvent := info.NewEvent()
75-
commenterEvent.Sender = activity.Comment.Author.Slug
76-
commenterEvent.AccountID = fmt.Sprintf("%d", activity.Comment.Author.ID)
77-
commenterEvent.Event = event.Event
78-
commenterEvent.BaseBranch = event.BaseBranch
79-
commenterEvent.HeadBranch = event.HeadBranch
80-
commenterEvent.Repository = event.Repository
81-
commenterEvent.Organization = v.projectKey
82-
commenterEvent.DefaultBranch = event.DefaultBranch
83-
allowed, err := v.checkMemberShip(ctx, commenterEvent)
84-
if err != nil {
85-
return false, err
86-
}
87-
if allowed {
88-
// TODO: show a log how come this has been allowed
89-
return true, nil
90-
}
63+
for _, comment := range allComments {
64+
if acl.MatchRegexp(acl.OKToTestCommentRegexp, comment.Body) {
65+
commenterEvent := info.NewEvent()
66+
commenterEvent.Sender = comment.Author.Login
67+
commenterEvent.AccountID = fmt.Sprintf("%d", comment.Author.ID)
68+
commenterEvent.Event = event.Event
69+
commenterEvent.BaseBranch = event.BaseBranch
70+
commenterEvent.HeadBranch = event.HeadBranch
71+
commenterEvent.Repository = event.Repository
72+
commenterEvent.Organization = v.projectKey
73+
commenterEvent.DefaultBranch = event.DefaultBranch
74+
allowed, err := v.checkMemberShip(ctx, commenterEvent)
75+
if err != nil {
76+
return false, err
77+
}
78+
if allowed {
79+
// TODO: show a log how come this has been allowed
80+
return true, nil
9181
}
9282
}
9383
}

pkg/provider/bitbucketdatacenter/acl_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func TestIsAllowed(t *testing.T) {
7878
},
7979
activities: []*bbv1test.Activity{
8080
{
81+
Action: "COMMENTED",
8182
Comment: types.ActivityComment{
8283
Text: "/ok-to-test",
8384
Author: types.User{
@@ -100,6 +101,7 @@ func TestIsAllowed(t *testing.T) {
100101
defaultBranchLatestCommit: "defaultlatestcommit",
101102
activities: []*bbv1test.Activity{
102103
{
104+
Action: "COMMENTED",
103105
Comment: types.ActivityComment{
104106
Text: "/ok-to-test",
105107
Author: types.User{
@@ -190,6 +192,7 @@ func TestIsAllowed(t *testing.T) {
190192
},
191193
activities: []*bbv1test.Activity{
192194
{
195+
Action: "COMMENTED",
193196
Comment: types.ActivityComment{
194197
Text: "this is a valid\n/ok-to-test",
195198
Author: types.User{

pkg/provider/bitbucketdatacenter/bitbucketdatacenter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ func (v *Provider) getRaw(ctx context.Context, runevent *info.Event, revision, p
228228

229229
func (v *Provider) GetTektonDir(ctx context.Context, event *info.Event, path, provenance string) (string, error) {
230230
v.provenance = provenance
231+
// If "at" is empty string "" then default branch will be used as source
231232
at := ""
232233
if v.provenance == "source" {
233234
at = event.SHA

pkg/provider/bitbucketdatacenter/types/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type ActivityComment struct {
7272
}
7373

7474
type Properties struct {
75-
Key string `json:"key"`
75+
RepositoryID int `json:"repositoryId"`
7676
}
7777

7878
type PermittedOperations struct {

0 commit comments

Comments
 (0)