Skip to content

Commit

Permalink
Add source repo url information to standard parameters
Browse files Browse the repository at this point in the history
Users can now use source_repo_url in pipelinerun template to get the information from which forked repo the event (Pull Request / Push) is coming from

Signed-off-by: Savita Ashture <[email protected]>
  • Loading branch information
savitaashture authored and chmouel committed Aug 30, 2023
1 parent 6a2f9c4 commit aa36883
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 5 deletions.
11 changes: 6 additions & 5 deletions docs/content/docs/guide/authoringprs.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ weight: 3
allows you to have those "dynamic" variables expanded. Those variables look
like this `{{ var }}` and those are the one you can use:

* `{{repo_owner}}`: The repository owner.
* `{{event_type}}`: The event type (eg: `pull_request` or `push`)
* `{{event_type}}`: The event type (eg: `pull_request` or `push`).
* `{{git_auth_secret}}`: The secret name auto generated with provider token to check out private repos.
* `{{pull_request_number}}`: The pull or merge request number, only defined when we are in a `pull_request` event type.
* `{{repo_name}}`: The repository name.
* `{{repo_owner}}`: The repository owner.
* `{{repo_url}}`: The repository full URL.
* `{{target_namespace}}`: The target namespace where the Repository has matched and the PipelineRun will be created.
* `{{revision}}`: The commit full sha revision.
* `{{sender}}`: The sender username (or accountid on some providers) of the commit.
* `{{source_branch}}`: The branch name where the event come from.
* `{{source_url}}`: The source repository URL from which the event come from (same as `repo_url` for push events).
* `{{target_branch}}`: The branch name on which the event targets (same as `source_branch` for push events).
* `{{pull_request_number}}`: The pull or merge request number, only defined when we are in a `pull_request` event type.
* `{{git_auth_secret}}`: The secret name auto generated with provider token to check out private repos.
* `{{target_namespace}}`: The target namespace where the Repository has matched and the PipelineRun will be created.

* For Pipelines-as-Code to process your `PipelineRun`, you must have either an
embedded `PipelineSpec` or a separate `Pipeline` object that references a YAML
Expand Down
1 change: 1 addition & 0 deletions pkg/customparams/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (p *CustomParams) makeStandardParamsFromEvent() map[string]string {
"repo_name": strings.ToLower(p.event.Repository),
"target_branch": formatting.SanitizeBranch(p.event.BaseBranch),
"source_branch": formatting.SanitizeBranch(p.event.HeadBranch),
"source_url": p.event.HeadURL,
"sender": strings.ToLower(p.event.Sender),
"target_namespace": p.repo.GetNamespace(),
"event_type": p.event.EventType,
Expand Down
2 changes: 2 additions & 0 deletions pkg/customparams/standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ func TestMakeStandardParamsFromEvent(t *testing.T) {
EventType: "pull_request",
Sender: "SENDER",
URL: "https://paris.com",
HeadURL: "https://india.com",
}

result := map[string]string{
"event_type": "pull_request",
"repo_name": "repo",
"repo_owner": "org",
"repo_url": "https://paris.com",
"source_url": "https://india.com",
"revision": "1234567890",
"sender": "sender",
"source_branch": "foo",
Expand Down
48 changes: 48 additions & 0 deletions test/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,54 @@ func TestGiteaClusterTasks(t *testing.T) {
tgitea.WaitForStatus(t, topts, topts.TargetRefName, "", true)
}

func TestGiteaStandardParamsCheckForPushAndPullEvent(t *testing.T) {
var (
repoURL string
sourceURL string
sourceBranch string
targetBranch string
)
topts := &tgitea.TestOpts{
Regexp: successRegexp,
TargetEvent: "pull_request, push",
YAMLFiles: map[string]string{
".tekton/pr.yaml": "testdata/pipelinerun-standard-params-display.yaml",
},
CheckForStatus: "success",
ExpectEvents: false,
}
defer tgitea.TestPR(t, topts)()
merged, resp, err := topts.GiteaCNX.Client.MergePullRequest(topts.Opts.Organization, topts.Opts.Repo, topts.PullRequest.Index,
gitea.MergePullRequestOption{
Title: "Merged with Panache",
Style: "merge",
},
)
assert.NilError(t, err)
assert.Assert(t, resp.StatusCode < 400, resp)
assert.Assert(t, merged)
tgitea.WaitForStatus(t, topts, topts.PullRequest.Head.Sha, "", false)
time.Sleep(5 * time.Second)

// get standard parameter info for pull_request
_, _, sourceBranch, targetBranch = tgitea.GetStandardParams(t, topts, "pull_request")
// sourceBranch and targetBranch are different for pull_request
if sourceBranch == targetBranch {
assert.Error(t, fmt.Errorf(`source_branch %s is same as target_branch %s for pull_request`, sourceBranch, targetBranch), fmt.Sprintf(`source_branch %s should be different from target_branch %s for pull_request`, sourceBranch, targetBranch))
}

// get standard parameter info for push
repoURL, sourceURL, sourceBranch, targetBranch = tgitea.GetStandardParams(t, topts, "push")
// sourceBranch and targetBranch are same for push
if sourceBranch != targetBranch {
assert.Error(t, fmt.Errorf(`source_branch %s is different from target_branch %s for push`, sourceBranch, targetBranch), fmt.Sprintf(`source_branch %s is same as target_branch %s for push`, sourceBranch, targetBranch))
}
// sourceURL and repoURL are same for push
if repoURL != sourceURL {
assert.Error(t, fmt.Errorf(`source_url %s is different from repo_url %s for push`, repoURL, sourceURL), fmt.Sprintf(`source_url %s is same as repo_url %s for push`, repoURL, sourceURL))
}
}

// Local Variables:
// compile-command: "go test -tags=e2e -v -run TestGiteaPush ."
// End:
24 changes: 24 additions & 0 deletions test/pkg/gitea/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
pgitea "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitea"
tlogs "github.com/openshift-pipelines/pipelines-as-code/test/pkg/logs"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/options"
"github.com/openshift-pipelines/pipelines-as-code/test/pkg/payload"
pacrepo "github.com/openshift-pipelines/pipelines-as-code/test/pkg/repository"
Expand Down Expand Up @@ -329,3 +330,26 @@ func CheckIfPipelineRunsCancelled(t *testing.T, topts *TestOpts) {
i++
}
}

func GetStandardParams(t *testing.T, topts *TestOpts, eventType string) (repoURL, sourceURL, sourceBranch, targetBranch string) {
t.Helper()
prs, err := topts.ParamsRun.Clients.Tekton.TektonV1().PipelineRuns(topts.TargetNS).List(context.Background(), metav1.ListOptions{
LabelSelector: keys.EventType + "=" + eventType,
})
assert.NilError(t, err)
assert.Equal(t, len(prs.Items), 1, "should have only one "+eventType+" pipelinerun")

out, err := tlogs.GetPodLog(context.Background(), topts.ParamsRun.Clients.Kube.CoreV1(), topts.TargetNS, fmt.Sprintf("tekton.dev/pipelineRun=%s",
prs.Items[0].Name), "step-test-standard-params-value")
assert.NilError(t, err)
assert.Assert(t, out != "")

outputDataForPR := strings.Split(out, "--")

repoURL = outputDataForPR[0]
sourceURL = strings.TrimPrefix(outputDataForPR[1], "\n")
sourceBranch = strings.TrimPrefix(outputDataForPR[2], "\n")
targetBranch = strings.TrimPrefix(outputDataForPR[3], "\n")

return repoURL, sourceURL, sourceBranch, targetBranch
}
36 changes: 36 additions & 0 deletions test/testdata/pipelinerun-standard-params-display.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
name: "\\ .PipelineName //"
annotations:
pipelinesascode.tekton.dev/target-namespace: "\\ .TargetNamespace //"
pipelinesascode.tekton.dev/on-target-branch: "[\\ .TargetBranch //]"
pipelinesascode.tekton.dev/on-event: "[\\ .TargetEvent //]"
spec:
params:
- name: repo_url
value: "{{ repo_url }}"
- name: source_url
value: "{{ source_url }}"
- name: source_branch
value: "{{ source_branch }}"
- name: target_branch
value: "{{ target_branch }}"
pipelineSpec:
params:
- name: repo_url
- name: source_url
- name: source_branch
- name: target_branch
tasks:
- name: params
taskSpec:
steps:
- name: test-standard-params-value
image: registry.access.redhat.com/ubi9/ubi-micro
script: |
echo "{{ repo_url }}--"
echo "{{ source_url }}--"
echo "{{ source_branch }}--"
echo "{{ target_branch }}--"

0 comments on commit aa36883

Please sign in to comment.