Skip to content

Added changes field in push event struct for Bitbucket server #1886

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

Merged
merged 2 commits into from
Jan 21, 2025
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
20 changes: 18 additions & 2 deletions pkg/provider/bitbucketserver/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,29 @@ type PullRequestEvent struct {
}

type PushRequestEventChange struct {
ToHash string `json:"toHash"`
RefID string `json:"refId"`
Ref Ref `json:"ref"`
FromHash string `json:"fromHash"`
ToHash string `json:"toHash"`
RefID string `json:"refId"`
Type string `json:"type"`
}

type Ref struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Type string `json:"type"`
}

type PushRequestEvent struct {
EventKey string `json:"eventKey"`
Actor bbv1.UserWithLinks `json:"actor"`
Repository bbv1.Repository `json:"repository"`
Changes []PushRequestEventChange `json:"changes"`
Commits []bbv1.Commit `json:"commits"`
ToCommit ToCommit `json:"toCommit"`
}

type ToCommit struct {
bbv1.Commit
Parents []bbv1.Commit `json:"parents"` // bbv1.Commit also has Parents field, but its Parents has only two fields while actual payload has more.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any test testing this with dynamic variable for bitbucketserver?

Copy link
Contributor Author

@zakisk zakisk Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No unit test, it could have done in bitbucketserver/parse_payload_test.go but no field from this struct is referenced there as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so how to make sure this works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed running locally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chmouel is there anyway to cover it in unit test because I am seeing it only referenced in dynamic variable (apart from E2E, can be covered in it)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you see if you ca add one ?

}
71 changes: 71 additions & 0 deletions test/bitbucket_server_dynamic_variables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build e2e
// +build e2e

package test

import (
"context"
"fmt"
"os"
"regexp"
"testing"

"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
tbbs "github.com/openshift-pipelines/pipelines-as-code/test/pkg/bitbucketserver"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/scm"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"

"github.com/tektoncd/pipeline/pkg/names"
"gotest.tools/v3/assert"
)

func TestBitbucketServerDynamicVariables(t *testing.T) {
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
ctx := context.Background()
bitbucketWSOwner := os.Getenv("TEST_BITBUCKET_SERVER_E2E_REPOSITORY")

ctx, runcnx, opts, client, err := tbbs.Setup(ctx)
assert.NilError(t, err)

repo := tbbs.CreateCRD(ctx, t, client, runcnx, bitbucketWSOwner, targetNS)
runcnx.Clients.Log.Infof("Repository %s has been created", repo.Name)
defer tbbs.TearDownNs(ctx, t, runcnx, targetNS)

branch, _, err := client.Git.CreateRef(ctx, bitbucketWSOwner, targetNS, "refs/heads/main")
assert.NilError(t, err)
runcnx.Clients.Log.Infof("Branch %s has been created", branch.Name)

files := map[string]string{
".tekton/pipelinerun.yaml": "testdata/pipelinerun-dynamic-vars.yaml",
}

files, err = payload.GetEntries(files, targetNS, targetNS, triggertype.Push.String(), map[string]string{})
assert.NilError(t, err)
gitCloneURL, err := scm.MakeGitCloneURL(repo.Clone, opts.UserName, opts.Password)
assert.NilError(t, err)

commitMsg := fmt.Sprintf("commit %s", targetNS)
scmOpts := &scm.Opts{
GitURL: gitCloneURL,
Log: runcnx.Clients.Log,
WebURL: repo.Clone,
TargetRefName: targetNS,
BaseRefName: repo.Branch,
CommitTitle: commitMsg,
}
scm.PushFilesToRefGit(t, scmOpts, files)
runcnx.Clients.Log.Infof("Files has been pushed to branch %s", targetNS)

successOpts := wait.SuccessOpt{
TargetNS: targetNS,
OnEvent: triggertype.Push.String(),
NumberofPRMatch: 1,
MinNumberStatus: 1,
}
wait.Succeeded(ctx, t, runcnx, opts, successOpts)
Copy link
Contributor Author

@zakisk zakisk Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failure of E2E test is obvious here because wait.Succeeded checks for EventType, but in bitbucket server EventType is not setin its ParsePayload func. PR #1891 fixes that after getting merge this E2E test will pass.


reg := *regexp.MustCompile(fmt.Sprintf("event: repo:refs_changed, refId: refs/heads/%s, message: %s", targetNS, commitMsg))
err = wait.RegexpMatchingInPodLog(ctx, runcnx, targetNS, "pipelinesascode.tekton.dev/original-prname=pipelinerun-dynamic-vars", "step-task", reg, "", 2)
assert.NilError(t, err)
}
3 changes: 3 additions & 0 deletions test/bitbucket_server_pull_request_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build e2e
// +build e2e

package test

import (
Expand Down
18 changes: 18 additions & 0 deletions test/testdata/pipelinerun-dynamic-vars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: "pipelinerun-dynamic-vars"
annotations:
pipelinesascode.tekton.dev/target-namespace: "\\ .TargetNamespace //"
pipelinesascode.tekton.dev/on-target-branch: "[\\ .TargetBranch //]"
pipelinesascode.tekton.dev/on-event: "[push]"
spec:
pipelineSpec:
tasks:
- name: task
taskSpec:
steps:
- name: task
image: registry.access.redhat.com/ubi9/ubi-micro
command: ["/bin/echo", "event: {{ body.eventKey }}, refId: {{ body.changes[0].ref.id }}, message: {{ body.toCommit.message }}"]
Loading