Skip to content

Commit

Permalink
Filter events by title, using CEL
Browse files Browse the repository at this point in the history
This supports push and pull_request (pull request title and sha event)

Closes #768

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
  • Loading branch information
chmouel committed Jul 25, 2022
1 parent b4b5260 commit a90e2dc
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 2 deletions.
13 changes: 12 additions & 1 deletion docs/content/docs/guide/authoringprs.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ If you have the ``pipelinesascode.tekton.dev/on-cel-expression`` annotation in
your PipelineRun, the CEL expression will be used and the `on-target-branch` or
`on-target-branch` annotations will then be skipped.

For example this will match a `pull_request` event targeting the branch `main` coming from a branch called `wip`:
This example will match a `pull_request` event targeting the branch `main`
coming from a branch called `wip`:

```yaml
pipelinesascode.tekton.dev/on-cel-expression: |
Expand All @@ -124,12 +125,22 @@ suffix) in the `docs` directory :
event == "pull_request" && "docs/*.md".pathChanged()
```

This example will match all pull request starting with the title `[DOWNSTREAM]`:

```yaml
pipelinesascode.tekton.dev/on-cel-expression: |
event == "pull_request && event_title.startsWith("[DOWNSTREAM]")
```

The fields available are :

* `event`: `push` or `pull_request`
* `target_branch`: The branch we are targeting.
* `source_branch`: The branch where this pull_request come from. (on `push` this
is the same as `target_branch`).
* `event_title`: Match the title of the event. When doing a push this will match
the commit title and when matching on PR it will match the Pull or Merge
Request title. (only `GitHub`, `Gitlab` and `BitbucketCloud` providers are supported)
* `.pathChanged`: a suffix function to a string which can be a glob of a path to
check if changed (only `GitHub` and `Gitlab` provider is supported)

Expand Down
115 changes: 115 additions & 0 deletions pkg/matcher/annotation_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,121 @@ func TestMatchPipelinerunAnnotationAndRepositories(t *testing.T) {
},
},
},
{
name: "cel/match path title pr",
wantPRName: pipelineTargetNSName,
args: args{
pruns: []*tektonv1beta1.PipelineRun{
{
ObjectMeta: metav1.ObjectMeta{
Name: pipelineTargetNSName,
Annotations: map[string]string{
pipelinesascode.GroupName + "/" + onCelExpression: "event_title.startsWith(\"[UPSTREAM]\")",
},
},
},
},
runevent: info.Event{
URL: targetURL,
TriggerTarget: "pull_request",
EventType: "pull_request",
BaseBranch: mainBranch,
HeadBranch: "unittests",
PullRequestNumber: 1000,
PullRequestTitle: "[UPSTREAM] test me cause i'm famous",
Organization: "mylittle",
Repository: "pony",
},
data: testclient.Data{
Repositories: []*v1alpha1.Repository{
testnewrepo.NewRepo(
testnewrepo.RepoTestcreationOpts{
Name: "test-good",
URL: targetURL,
InstallNamespace: targetNamespace,
},
),
},
},
},
},

{
name: "cel/match path title push",
wantPRName: pipelineTargetNSName,
args: args{
pruns: []*tektonv1beta1.PipelineRun{
{
ObjectMeta: metav1.ObjectMeta{
Name: pipelineTargetNSName,
Annotations: map[string]string{
pipelinesascode.GroupName + "/" + onCelExpression: "event_title.startsWith(\"[UPSTREAM]\")",
},
},
},
},
runevent: info.Event{
URL: targetURL,
TriggerTarget: "push",
EventType: "push",
BaseBranch: mainBranch,
HeadBranch: "unittests",
SHATitle: "[UPSTREAM] test me cause i'm famous",
Organization: "mylittle",
Repository: "pony",
},
data: testclient.Data{
Repositories: []*v1alpha1.Repository{
testnewrepo.NewRepo(
testnewrepo.RepoTestcreationOpts{
Name: "test-good",
URL: targetURL,
InstallNamespace: targetNamespace,
},
),
},
},
},
},

{
name: "cel/no match path title pr",
wantErr: true,
args: args{
pruns: []*tektonv1beta1.PipelineRun{
{
ObjectMeta: metav1.ObjectMeta{
Name: pipelineTargetNSName,
Annotations: map[string]string{
pipelinesascode.GroupName + "/" + onCelExpression: "event_title.startsWith(\"[UPSTREAM]\")",
},
},
},
},
runevent: info.Event{
URL: targetURL,
TriggerTarget: "pull_request",
EventType: "pull_request",
BaseBranch: mainBranch,
HeadBranch: "unittests",
PullRequestNumber: 1000,
PullRequestTitle: "[DOWNSTREAM] don't test me cause i'm famous",
Organization: "mylittle",
Repository: "pony",
},
data: testclient.Data{
Repositories: []*v1alpha1.Repository{
testnewrepo.NewRepo(
testnewrepo.RepoTestcreationOpts{
Name: "test-good",
URL: targetURL,
InstallNamespace: targetNamespace,
},
),
},
},
},
},
{
name: "cel/no match path by glob",
wantErr: true,
Expand Down
7 changes: 7 additions & 0 deletions pkg/matcher/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ import (
)

func celEvaluate(ctx context.Context, expr string, event *info.Event, vcx provider.Interface) (ref.Val, error) {
eventTitle := event.PullRequestTitle
if event.TriggerTarget == "push" {
eventTitle = event.SHATitle
}

data := map[string]interface{}{
"event": event.TriggerTarget,
"event_title": eventTitle,
"target_branch": event.BaseBranch,
"source_branch": event.HeadBranch,
}
Expand All @@ -27,6 +33,7 @@ func celEvaluate(ctx context.Context, expr string, event *info.Event, vcx provid
cel.Lib(celPac{vcx, ctx, event}),
cel.Declarations(
decls.NewVar("event", decls.String),
decls.NewVar("event_title", decls.String),
decls.NewVar("target_branch", decls.String),
decls.NewVar("source_branch", decls.String)))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/params/info/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Event struct {
SHAURL string // pretty URL for web browsing for UIs (cli/web)
SHATitle string // commit title for UIs
PullRequestNumber int // Pull or Merge Request number
PullRequestTitle string // Title of the pull Request

// TODO: move forge specifics to each driver
// Github
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/bitbucketcloud/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (v *Provider) ParsePayload(ctx context.Context, run *params.Run, request *h
processedEvent.AccountID = e.PullRequest.Author.AccountID
processedEvent.Sender = e.PullRequest.Author.Nickname
processedEvent.PullRequestNumber = e.PullRequest.ID
processedEvent.PullRequestTitle = e.PullRequest.Title
case *types.PushRequestEvent:
processedEvent.Event = "push"
processedEvent.TriggerTarget = "push"
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/bitbucketcloud/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type PullRequest struct {
Source Source `json:"source"`
ID int `json:"id"`
Links Links
Title string `json:"title"`
}

type PullRequestEvent struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func (v *Provider) getPullRequest(ctx context.Context, runevent *info.Event) (*i
runevent.URL = pr.GetBase().GetRepo().GetHTMLURL()
runevent.SHA = pr.GetHead().GetSHA()
runevent.SHAURL = fmt.Sprintf("%s/commit/%s", pr.GetHTMLURL(), pr.GetHead().GetSHA())
runevent.PullRequestTitle = pr.GetTitle()

// TODO: check if we really need this
if runevent.Sender == "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ func (v *Provider) ParsePayload(_ context.Context, _ *params.Run, request *http.
processedEvent.SHATitle = gitEvent.ObjectAttributes.Title
processedEvent.HeadBranch = gitEvent.ObjectAttributes.SourceBranch
processedEvent.BaseBranch = gitEvent.ObjectAttributes.TargetBranch

processedEvent.PullRequestNumber = gitEvent.ObjectAttributes.IID
processedEvent.PullRequestTitle = gitEvent.ObjectAttributes.Title
v.targetProjectID = gitEvent.Project.ID
v.sourceProjectID = gitEvent.ObjectAttributes.SourceProjectID
v.userID = gitEvent.User.ID
Expand Down

0 comments on commit a90e2dc

Please sign in to comment.