This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
forked from redhat-developer/odo
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 40: Add functions to generate trigger templates and bindings (#15)
* Refactor the bindings functions in triggers package - Using v1alpha1 package instead of v1alpha2 - Renamed createTypeMeta() to createTriggerBindingMeta() - Added test case for createBindingParam() - Using Fatalf() instead of Errorf() * template * Barebones of test working. * templates-test * created go file to generate Templates * Add generated trigger templates and bindings to outputs slice Co-Authored-By: Dhanashri Palodkar <dpalodka@redhat.com> * Merge templates and bindings functions into a single package Co-Authored-By: Dhanashri Palodkar <dpalodka@redhat.com> * Add namespaces to TriggerBindings TriggerTemplates. Co-authored-by: Kevin McDermott <bigkevmcd@gmail.com> Co-authored-by: Dhanashri Palodkar <dpalodkar14@gmail.com> Co-authored-by: William Tam <wtam@redhat.com>
- Loading branch information
1 parent
ae77faa
commit eee2b6b
Showing
7 changed files
with
773 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package triggers | ||
|
||
import ( | ||
"github.com/openshift/odo/pkg/pipelines/meta" | ||
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var ( | ||
triggerBindingTypeMeta = v1.TypeMeta{ | ||
Kind: "TriggerBinding", | ||
APIVersion: "tekton.dev/v1alpha1", | ||
} | ||
) | ||
|
||
// GenerateBindings returns a slice of trigger bindings | ||
func GenerateBindings(ns string) []triggersv1.TriggerBinding { | ||
return []triggersv1.TriggerBinding{ | ||
createDevCDDeployBinding(ns), | ||
createDevCIBuildBinding(ns), | ||
createStageCDDeployBinding(ns), | ||
createStageCIDryRunBinding(ns), | ||
} | ||
} | ||
|
||
func createDevCDDeployBinding(ns string) triggersv1.TriggerBinding { | ||
return triggersv1.TriggerBinding{ | ||
TypeMeta: triggerBindingTypeMeta, | ||
ObjectMeta: meta.CreateObjectMeta(ns, "dev-cd-deploy-from-master-binding"), | ||
Spec: triggersv1.TriggerBindingSpec{ | ||
Params: []pipelinev1.Param{ | ||
createBindingParam("gitref", "$(body.head_commit.id)"), | ||
createBindingParam("gitrepositoryurl", "$(body.repository.clone_url)"), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func createDevCIBuildBinding(ns string) triggersv1.TriggerBinding { | ||
return triggersv1.TriggerBinding{ | ||
TypeMeta: triggerBindingTypeMeta, | ||
ObjectMeta: meta.CreateObjectMeta(ns, "dev-ci-build-from-pr-binding"), | ||
Spec: triggersv1.TriggerBindingSpec{ | ||
Params: []pipelinev1.Param{ | ||
createBindingParam("gitref", "$(body.pull_request.head.ref)"), | ||
createBindingParam("gitsha", "$(body.pull_request.head.sha)"), | ||
createBindingParam("gitrepositoryurl", "$(body.repository.clone_url)"), | ||
createBindingParam("fullname", "$(body.repository.full_name)"), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func createStageCDDeployBinding(ns string) triggersv1.TriggerBinding { | ||
return triggersv1.TriggerBinding{ | ||
TypeMeta: triggerBindingTypeMeta, | ||
ObjectMeta: meta.CreateObjectMeta(ns, "stage-cd-deploy-from-push-binding"), | ||
Spec: triggersv1.TriggerBindingSpec{ | ||
Params: []pipelinev1.Param{ | ||
createBindingParam("gitref", "$(body.ref)"), | ||
createBindingParam("gitsha", "$(body.commits.0.id)"), | ||
createBindingParam("gitrepositoryurl", "$(body.repository.clone_url)"), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func createStageCIDryRunBinding(ns string) triggersv1.TriggerBinding { | ||
return triggersv1.TriggerBinding{ | ||
TypeMeta: triggerBindingTypeMeta, | ||
ObjectMeta: meta.CreateObjectMeta(ns, "stage-ci-dryrun-from-pr-binding"), | ||
Spec: triggersv1.TriggerBindingSpec{ | ||
Params: []pipelinev1.Param{ | ||
createBindingParam("gitref", "$(body.pull_request.head.ref)"), | ||
createBindingParam("gitrepositoryurl", "$(body.repository.clone_url)"), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func createObjectMeta(name string) v1.ObjectMeta { | ||
return v1.ObjectMeta{ | ||
Name: name, | ||
} | ||
} | ||
func createBindingParam(name string, value string) pipelinev1.Param { | ||
return pipelinev1.Param{ | ||
Name: name, | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: value, | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package triggers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1" | ||
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestCreateDevCDDeployBinding(t *testing.T) { | ||
validDevCDBinding := triggersv1.TriggerBinding{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "TriggerBinding", | ||
APIVersion: "tekton.dev/v1alpha1", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "dev-cd-deploy-from-master-binding", | ||
Namespace: "testns", | ||
}, | ||
Spec: triggersv1.TriggerBindingSpec{ | ||
Params: []pipelinev1.Param{ | ||
pipelinev1.Param{ | ||
Name: "gitref", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.head_commit.id)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
pipelinev1.Param{ | ||
Name: "gitrepositoryurl", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.repository.clone_url)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
binding := createDevCDDeployBinding("testns") | ||
if diff := cmp.Diff(validDevCDBinding, binding); diff != "" { | ||
t.Fatalf("createDevCDDeployBinding() failed:\n%s", diff) | ||
} | ||
} | ||
|
||
func TestCreateDevCIBuildBinding(t *testing.T) { | ||
validDevCIBinding := triggersv1.TriggerBinding{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "TriggerBinding", | ||
APIVersion: "tekton.dev/v1alpha1", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "dev-ci-build-from-pr-binding", | ||
Namespace: "testns", | ||
}, | ||
Spec: triggersv1.TriggerBindingSpec{ | ||
Params: []pipelinev1.Param{ | ||
pipelinev1.Param{ | ||
Name: "gitref", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.pull_request.head.ref)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
pipelinev1.Param{ | ||
Name: "gitsha", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.pull_request.head.sha)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
pipelinev1.Param{ | ||
Name: "gitrepositoryurl", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.repository.clone_url)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
pipelinev1.Param{ | ||
Name: "fullname", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.repository.full_name)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
binding := createDevCIBuildBinding("testns") | ||
if diff := cmp.Diff(validDevCIBinding, binding); diff != "" { | ||
t.Fatalf("createDevCIBuildBinding() failed:\n%s", diff) | ||
} | ||
} | ||
|
||
func TestCreateStageCDDeployBinding(t *testing.T) { | ||
validStageCDDeployBinding := triggersv1.TriggerBinding{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "TriggerBinding", | ||
APIVersion: "tekton.dev/v1alpha1", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "stage-cd-deploy-from-push-binding", | ||
Namespace: "testns", | ||
}, | ||
Spec: triggersv1.TriggerBindingSpec{ | ||
Params: []pipelinev1.Param{ | ||
pipelinev1.Param{ | ||
Name: "gitref", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.ref)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
pipelinev1.Param{ | ||
Name: "gitsha", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.commits.0.id)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
pipelinev1.Param{ | ||
Name: "gitrepositoryurl", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.repository.clone_url)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
binding := createStageCDDeployBinding("testns") | ||
if diff := cmp.Diff(validStageCDDeployBinding, binding); diff != "" { | ||
t.Fatalf("createDevCIBuildBinding() failed:\n%s", diff) | ||
} | ||
} | ||
|
||
func TestCreateStageCIDryRunBinding(t *testing.T) { | ||
validStageCIDryRunBinding := triggersv1.TriggerBinding{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "TriggerBinding", | ||
APIVersion: "tekton.dev/v1alpha1", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "stage-ci-dryrun-from-pr-binding", | ||
Namespace: "testns", | ||
}, | ||
Spec: triggersv1.TriggerBindingSpec{ | ||
Params: []pipelinev1.Param{ | ||
pipelinev1.Param{ | ||
Name: "gitref", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.pull_request.head.ref)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
pipelinev1.Param{ | ||
Name: "gitrepositoryurl", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.repository.clone_url)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
binding := createStageCIDryRunBinding("testns") | ||
if diff := cmp.Diff(validStageCIDryRunBinding, binding); diff != "" { | ||
t.Errorf("createStageCIDryRunBinding() failed:\n%s", diff) | ||
} | ||
} | ||
|
||
func TestCreateBindingParam(t *testing.T) { | ||
validParam := pipelinev1.Param{ | ||
Name: "gitref", | ||
Value: pipelinev1.ArrayOrString{ | ||
StringVal: "$(body.ref)", | ||
Type: pipelinev1.ParamTypeString, | ||
}, | ||
} | ||
bindingParam := createBindingParam("gitref", "$(body.ref)") | ||
if diff := cmp.Diff(validParam, bindingParam); diff != "" { | ||
t.Fatalf("createBindingParam() failed\n%s", diff) | ||
} | ||
} |
Oops, something went wrong.