Skip to content

Commit e7b4bba

Browse files
committed
feat: Add new dynamic variable git_tag
added new dynamic variable git_tag that represents git tag (e.g. v1.0) on git tag push events and will have empty value if event is not a git push event. added its unit tests as well. Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
1 parent 81ba2fa commit e7b4bba

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed

docs/content/docs/guide/authoringprs.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ check out the code that is being tested.
5858
| repo_owner | The repository owner. | `{{repo_owner}}` | openshift-pipelines |
5959
| repo_url | The repository full URL. | `{{repo_url}}` | https:/github.com/repo/owner |
6060
| revision | The commit full sha revision. | `{{revision}}` | 1234567890abcdef |
61-
| sender | The sender username (or account ID on some providers) of the commit. | `{{sender}}` | johndoe |
62-
| source_branch | The branch name where the event comes from. | `{{source_branch}}` | main |
63-
| source_url | The source repository URL from which the event comes from (same as `repo_url` for push events). | `{{source_url}}` | https:/github.com/repo/owner |
61+
| sender | The sender username (or account ID on some providers) of the commit. | `{{sender}}` | johndoe |
62+
| source_branch | The branch name where the event comes from. | `{{source_branch}}` | main |
63+
| git_tag | The tag pushed to repository. (available only when a tag push event otherwise contains empty "" value) | `{{git_tag}}` | v1.0 |
64+
| source_url | The source repository URL from which the event comes from (same as `repo_url` for push events). | `{{source_url}}` | https:/github.com/repo/owner |
6465
| target_branch | The branch name on which the event targets (same as `source_branch` for push events). | `{{target_branch}}` | main |
6566
| target_namespace | The target namespace where the Repository has matched and the PipelineRun will be created. | `{{target_namespace}}` | my-namespace |
6667
| trigger_comment | The comment triggering the PipelineRun when using a [GitOps command]({{< relref "/docs/guide/running.md#gitops-command-on-pull-or-merge-request" >}}) (like `/test`, `/retest`) | `{{trigger_comment}}` | /merge-pr branch |

pkg/customparams/customparams_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func TestProcessTemplates(t *testing.T) {
153153
"sender": "",
154154
"source_branch": "",
155155
"source_url": "",
156+
"git_tag": "",
156157
"target_branch": "",
157158
"target_namespace": "",
158159
"trigger_comment": "",

pkg/customparams/standard.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,20 @@ func (p *CustomParams) makeStandardParamsFromEvent(ctx context.Context) (map[str
3636
triggerCommentAsSingleLine := strings.ReplaceAll(strings.ReplaceAll(p.event.TriggerComment, "\r\n", "\\n"), "\n", "\\n")
3737
pullRequestLabels := strings.Join(p.event.PullRequestLabel, "\\n")
3838

39+
gitTag := ""
40+
if strings.Contains(p.event.BaseBranch, "refs/tags/") {
41+
parts := strings.Split(p.event.BaseBranch, "/")
42+
gitTag = parts[len(parts)-1]
43+
}
44+
3945
return map[string]string{
4046
"revision": p.event.SHA,
4147
"repo_url": repoURL,
4248
"repo_owner": strings.ToLower(p.event.Organization),
4349
"repo_name": strings.ToLower(p.event.Repository),
4450
"target_branch": formatting.SanitizeBranch(p.event.BaseBranch),
4551
"source_branch": formatting.SanitizeBranch(p.event.HeadBranch),
52+
"git_tag": gitTag,
4653
"source_url": p.event.HeadURL,
4754
"sender": strings.ToLower(p.event.Sender),
4855
"target_namespace": p.repo.GetNamespace(),

pkg/customparams/standard_test.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func TestMakeStandardParamsFromEvent(t *testing.T) {
4949
"revision": "1234567890",
5050
"sender": "sender",
5151
"source_branch": "foo",
52+
"git_tag": "",
5253
"target_branch": "main",
5354
"target_namespace": "myns",
5455
"trigger_comment": `\n/test me\nHelp me obiwan kenobi\n\n\nTo test or not to test, is the question?\n\n\n`,
@@ -63,7 +64,7 @@ func TestMakeStandardParamsFromEvent(t *testing.T) {
6364
},
6465
},
6566
{
66-
name: "basic event test",
67+
name: "event with different clone URL",
6768
event: &info.Event{
6869
SHA: "1234567890",
6970
Organization: "Org",
@@ -76,6 +77,7 @@ func TestMakeStandardParamsFromEvent(t *testing.T) {
7677
HeadURL: "https://india.com",
7778
TriggerComment: "/test me\nHelp me obiwan kenobi",
7879
PullRequestLabel: []string{"bugs", "enhancements"},
80+
CloneURL: "https://blahblah",
7981
},
8082
repo: &v1alpha1.Repository{
8183
ObjectMeta: metav1.ObjectMeta{
@@ -87,11 +89,12 @@ func TestMakeStandardParamsFromEvent(t *testing.T) {
8789
"event_type": "pull_request",
8890
"repo_name": "repo",
8991
"repo_owner": "org",
90-
"repo_url": "https://paris.com",
92+
"repo_url": "https://blahblah",
9193
"source_url": "https://india.com",
9294
"revision": "1234567890",
9395
"sender": "sender",
9496
"source_branch": "foo",
97+
"git_tag": "",
9598
"target_branch": "main",
9699
"target_namespace": "myns",
97100
"trigger_comment": "/test me\\nHelp me obiwan kenobi",
@@ -106,20 +109,19 @@ func TestMakeStandardParamsFromEvent(t *testing.T) {
106109
},
107110
},
108111
{
109-
name: "event with different clone URL",
112+
name: "git tag push test event",
110113
event: &info.Event{
111-
SHA: "1234567890",
112-
Organization: "Org",
113-
Repository: "Repo",
114-
BaseBranch: "main",
115-
HeadBranch: "foo",
116-
EventType: "pull_request",
117-
Sender: "SENDER",
118-
URL: "https://paris.com",
119-
HeadURL: "https://india.com",
120-
TriggerComment: "/test me\nHelp me obiwan kenobi",
121-
PullRequestLabel: []string{"bugs", "enhancements"},
122-
CloneURL: "https://blahblah",
114+
SHA: "1234567890",
115+
Organization: "Org",
116+
Repository: "Repo",
117+
BaseBranch: "refs/tags/v1.0",
118+
HeadBranch: "refs/tags/v1.0",
119+
EventType: "push",
120+
Sender: "SENDER",
121+
URL: "https://paris.com",
122+
HeadURL: "https://india.com",
123+
TriggerComment: "/test me\nHelp me obiwan kenobi",
124+
CloneURL: "https://blahblah",
123125
},
124126
repo: &v1alpha1.Repository{
125127
ObjectMeta: metav1.ObjectMeta{
@@ -128,18 +130,19 @@ func TestMakeStandardParamsFromEvent(t *testing.T) {
128130
},
129131
},
130132
want: map[string]string{
131-
"event_type": "pull_request",
133+
"event_type": "push",
132134
"repo_name": "repo",
133135
"repo_owner": "org",
134136
"repo_url": "https://blahblah",
135137
"source_url": "https://india.com",
136138
"revision": "1234567890",
137139
"sender": "sender",
138-
"source_branch": "foo",
139-
"target_branch": "main",
140+
"source_branch": "refs/tags/v1.0",
141+
"git_tag": "v1.0",
142+
"target_branch": "refs/tags/v1.0",
140143
"target_namespace": "myns",
141144
"trigger_comment": "/test me\\nHelp me obiwan kenobi",
142-
"pull_request_labels": "bugs\\nenhancements",
145+
"pull_request_labels": "",
143146
},
144147
wantVCX: &testprovider.TestProviderImp{
145148
WantAllChangedFiles: []string{"added.go", "deleted.go", "modified.go", "renamed.go"},

pkg/pipelineascode/template_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ func TestProcessTemplates(t *testing.T) {
8686
template: `{{ repo_url }}`,
8787
expected: "https://cloneurl",
8888
},
89+
{
90+
name: "test git_tag variable",
91+
event: &info.Event{
92+
BaseBranch: "refs/tags/v1.0",
93+
},
94+
template: `{{ git_tag }}`,
95+
expected: "v1.0",
96+
},
8997
{
9098
name: "replace target_namespace",
9199
template: `ns {{ target_namespace }}`,

pkg/templates/templating_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ func TestReplacePlaceHoldersVariables(t *testing.T) {
1919
}{
2020
{
2121
name: "Test Replace standard",
22-
template: `revision: {{ revision }}} url: {{ url }} bar: {{ bar}}`,
23-
expected: `revision: master} url: https://chmouel.com bar: {{ bar}}`,
22+
template: `revision: {{ revision }}} url: {{ url }} bar: {{ bar}} tag: {{ git_tag }}`,
23+
expected: `revision: master} url: https://chmouel.com bar: {{ bar}} tag: v1.0`,
2424
dicto: map[string]string{
2525
"revision": "master",
2626
"url": "https://chmouel.com",
27+
"git_tag": "v1.0",
2728
},
2829
changedFiles: map[string]any{},
2930
},

0 commit comments

Comments
 (0)