Skip to content

Commit b7279f9

Browse files
refactor: GetCommitInfo func in Bitbucket Data Center
This patch refactors GetCommitInfo in Bitucket DataCenter using jenkins-x/go-scm Signed-off-by: PuneetPunamiya <ppunamiy@redhat.com>
1 parent 7c3294d commit b7279f9

File tree

3 files changed

+12
-23
lines changed

3 files changed

+12
-23
lines changed

pkg/provider/bitbucketdatacenter/bitbucketdatacenter.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/jenkins-x/go-scm/scm"
1414
"github.com/jenkins-x/go-scm/scm/driver/stash"
1515
"github.com/jenkins-x/go-scm/scm/transport/oauth2"
16-
"github.com/mitchellh/mapstructure"
1716
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
1817
"github.com/openshift-pipelines/pipelines-as-code/pkg/changedfiles"
1918
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
@@ -315,31 +314,21 @@ func (v *Provider) SetClient(ctx context.Context, run *params.Run, event *info.E
315314
}
316315

317316
func (v *Provider) GetCommitInfo(_ context.Context, event *info.Event) error {
318-
localVarOptionals := map[string]any{}
319-
resp, err := v.Client.DefaultApi.GetCommit(v.projectKey, event.Repository, event.SHA, localVarOptionals)
320-
if err != nil {
321-
return err
322-
}
323-
commitInfo := bbv1.Commit{}
324-
err = mapstructure.Decode(resp.Values, &commitInfo)
317+
OrgAndRepo := fmt.Sprintf("%s/%s", event.Organization, event.Repository)
318+
commit, _, err := v.ScmClient.Git.FindCommit(context.Background(), OrgAndRepo, event.SHA)
325319
if err != nil {
326320
return err
327321
}
328-
event.SHATitle = sanitizeTitle(commitInfo.Message)
322+
event.SHATitle = sanitizeTitle(commit.Message)
329323
event.SHAURL = fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", v.baseURL, v.projectKey, event.Repository, event.SHA)
330324

331-
resp, err = v.Client.DefaultApi.GetDefaultBranch(v.projectKey, event.Repository)
332-
if err != nil {
333-
return err
334-
}
335-
branchInfo := &bbv1.Branch{}
336-
err = mapstructure.Decode(resp.Values, branchInfo)
325+
ref, _, err := v.ScmClient.Git.GetDefaultBranch(context.Background(), OrgAndRepo)
337326
if err != nil {
338327
return err
339328
}
340329

341-
v.defaultBranchLatestCommit = branchInfo.LatestCommit
342-
event.DefaultBranch = branchInfo.DisplayID
330+
v.defaultBranchLatestCommit = ref.Sha
331+
event.DefaultBranch = ref.Name
343332
return nil
344333
}
345334

pkg/provider/bitbucketdatacenter/bitbucketdatacenter_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
2222
bbtest "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketdatacenter/test"
2323

24-
bbv1 "github.com/gfleury/go-bitbucket-v1"
24+
"github.com/jenkins-x/go-scm/scm"
2525
"go.uber.org/zap"
2626
zapobserver "go.uber.org/zap/zaptest/observer"
2727
"gotest.tools/v3/assert"
@@ -384,7 +384,7 @@ func TestGetCommitInfo(t *testing.T) {
384384
tests := []struct {
385385
name string
386386
event *info.Event
387-
commit bbv1.Commit
387+
commit scm.Commit
388388
defaultBranch string
389389
latestCommit string
390390
}{
@@ -396,7 +396,7 @@ func TestGetCommitInfo(t *testing.T) {
396396
SHA: "sha",
397397
},
398398
defaultBranch: "branchmain",
399-
commit: bbv1.Commit{
399+
commit: scm.Commit{
400400
Message: "hello moto",
401401
},
402402
latestCommit: "latestcommit",
@@ -406,11 +406,11 @@ func TestGetCommitInfo(t *testing.T) {
406406
for _, tt := range tests {
407407
t.Run(tt.name, func(t *testing.T) {
408408
ctx, _ := rtesting.SetupFakeContext(t)
409-
bbclient, _, mux, tearDown, tURL := bbtest.SetupBBDataCenterClient(ctx)
409+
_, scmClient, mux, tearDown, tURL := bbtest.SetupBBDataCenterClient(ctx)
410410
bbtest.MuxCommitInfo(t, mux, tt.event, tt.commit)
411411
bbtest.MuxDefaultBranch(t, mux, tt.event, tt.defaultBranch, tt.latestCommit)
412412
defer tearDown()
413-
v := &Provider{Client: bbclient, baseURL: tURL, projectKey: tt.event.Organization}
413+
v := &Provider{ScmClient: scmClient, baseURL: tURL, projectKey: tt.event.Organization}
414414
err := v.GetCommitInfo(ctx, tt.event)
415415
assert.NilError(t, err)
416416
assert.Equal(t, tt.defaultBranch, tt.event.DefaultBranch)

pkg/provider/bitbucketdatacenter/test/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func MuxDirContent(t *testing.T, mux *http.ServeMux, event *info.Event, testDir,
141141
MuxFiles(t, mux, event, event.HeadBranch, targetDirName, filecontents, wantFilesErr)
142142
}
143143

144-
func MuxCommitInfo(t *testing.T, mux *http.ServeMux, event *info.Event, commit bbv1.Commit) {
144+
func MuxCommitInfo(t *testing.T, mux *http.ServeMux, event *info.Event, commit scm.Commit) {
145145
path := fmt.Sprintf("/projects/%s/repos/%s/commits/%s", event.Organization, event.Repository, event.SHA)
146146

147147
mux.HandleFunc(path, func(rw http.ResponseWriter, _ *http.Request) {

0 commit comments

Comments
 (0)