Skip to content

Commit 3b251fb

Browse files
zakiskchmouel
authored andcommitted
Added changes field in push event struct for Bitbucket server
added changes field in push event struct for Bitbucket to make it available in PipelineRun's dynamic variable https://issues.redhat.com/browse/SRVKP-6981 Signed-off-by: Zaki Shaikh <[email protected]>
1 parent 6ab7a88 commit 3b251fb

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

pkg/provider/bitbucketserver/types/types.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,29 @@ type PullRequestEvent struct {
2323
}
2424

2525
type PushRequestEventChange struct {
26-
ToHash string `json:"toHash"`
27-
RefID string `json:"refId"`
26+
Ref Ref `json:"ref"`
27+
FromHash string `json:"fromHash"`
28+
ToHash string `json:"toHash"`
29+
RefID string `json:"refId"`
30+
Type string `json:"type"`
31+
}
32+
33+
type Ref struct {
34+
ID string `json:"id"`
35+
DisplayID string `json:"displayId"`
36+
Type string `json:"type"`
2837
}
2938

3039
type PushRequestEvent struct {
40+
EventKey string `json:"eventKey"`
3141
Actor bbv1.UserWithLinks `json:"actor"`
3242
Repository bbv1.Repository `json:"repository"`
3343
Changes []PushRequestEventChange `json:"changes"`
3444
Commits []bbv1.Commit `json:"commits"`
45+
ToCommit ToCommit `json:"toCommit"`
46+
}
47+
48+
type ToCommit struct {
49+
bbv1.Commit
50+
Parents []bbv1.Commit `json:"parents"` // bbv1.Commit also has Parents field, but its Parents has only two fields while actual payload has more.
3551
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//go:build e2e
2+
// +build e2e
3+
4+
package test
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"os"
10+
"regexp"
11+
"testing"
12+
13+
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
14+
tbbs "github.com/openshift-pipelines/pipelines-as-code/test/pkg/bitbucketserver"
15+
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
16+
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/scm"
17+
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/wait"
18+
19+
"github.com/tektoncd/pipeline/pkg/names"
20+
"gotest.tools/v3/assert"
21+
)
22+
23+
func TestBitbucketServerDynamicVariables(t *testing.T) {
24+
targetNS := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("pac-e2e-ns")
25+
ctx := context.Background()
26+
bitbucketWSOwner := os.Getenv("TEST_BITBUCKET_SERVER_E2E_REPOSITORY")
27+
28+
ctx, runcnx, opts, client, err := tbbs.Setup(ctx)
29+
assert.NilError(t, err)
30+
31+
repo := tbbs.CreateCRD(ctx, t, client, runcnx, bitbucketWSOwner, targetNS)
32+
runcnx.Clients.Log.Infof("Repository %s has been created", repo.Name)
33+
defer tbbs.TearDownNs(ctx, t, runcnx, targetNS)
34+
35+
branch, _, err := client.Git.CreateRef(ctx, bitbucketWSOwner, targetNS, "refs/heads/main")
36+
assert.NilError(t, err)
37+
runcnx.Clients.Log.Infof("Branch %s has been created", branch.Name)
38+
39+
files := map[string]string{
40+
".tekton/pipelinerun.yaml": "testdata/pipelinerun-dynamic-vars.yaml",
41+
}
42+
43+
files, err = payload.GetEntries(files, targetNS, targetNS, triggertype.Push.String(), map[string]string{})
44+
assert.NilError(t, err)
45+
gitCloneURL, err := scm.MakeGitCloneURL(repo.Clone, opts.UserName, opts.Password)
46+
assert.NilError(t, err)
47+
48+
commitMsg := fmt.Sprintf("commit %s", targetNS)
49+
scmOpts := &scm.Opts{
50+
GitURL: gitCloneURL,
51+
Log: runcnx.Clients.Log,
52+
WebURL: repo.Clone,
53+
TargetRefName: targetNS,
54+
BaseRefName: repo.Branch,
55+
CommitTitle: commitMsg,
56+
}
57+
scm.PushFilesToRefGit(t, scmOpts, files)
58+
runcnx.Clients.Log.Infof("Files has been pushed to branch %s", targetNS)
59+
60+
successOpts := wait.SuccessOpt{
61+
TargetNS: targetNS,
62+
OnEvent: triggertype.Push.String(),
63+
NumberofPRMatch: 1,
64+
MinNumberStatus: 1,
65+
}
66+
wait.Succeeded(ctx, t, runcnx, opts, successOpts)
67+
68+
reg := *regexp.MustCompile(fmt.Sprintf("event: repo:refs_changed, refId: refs/heads/%s, message: %s", targetNS, commitMsg))
69+
err = wait.RegexpMatchingInPodLog(ctx, runcnx, targetNS, "pipelinesascode.tekton.dev/original-prname=pipelinerun-dynamic-vars", "step-task", reg, "", 2)
70+
assert.NilError(t, err)
71+
}

test/bitbucket_server_pull_request_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build e2e
2+
// +build e2e
3+
14
package test
25

36
import (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
apiVersion: tekton.dev/v1beta1
3+
kind: PipelineRun
4+
metadata:
5+
name: "pipelinerun-dynamic-vars"
6+
annotations:
7+
pipelinesascode.tekton.dev/target-namespace: "\\ .TargetNamespace //"
8+
pipelinesascode.tekton.dev/on-target-branch: "[\\ .TargetBranch //]"
9+
pipelinesascode.tekton.dev/on-event: "[push]"
10+
spec:
11+
pipelineSpec:
12+
tasks:
13+
- name: task
14+
taskSpec:
15+
steps:
16+
- name: task
17+
image: registry.access.redhat.com/ubi9/ubi-micro
18+
command: ["/bin/echo", "event: {{ body.eventKey }}, refId: {{ body.changes[0].ref.id }}, message: {{ body.toCommit.message }}"]

0 commit comments

Comments
 (0)