Skip to content

Commit

Permalink
improve get changed files
Browse files Browse the repository at this point in the history
This change will add support for separating the files by the change
type i.e. added, modified, deleted and renamed for a giving
source control event. This provides the ability to target only added
files or only renamed files. Note that this change still supports all
changed files in case specifics types of changes are not needed.

In addition, this change will also allow the changed file information
to be passed down using templating variables. The following templated
variables have been added

- {{all_changed_files}}
- {{added_files}}
- {{deleted_files}}
- {{modified_files}}
- {{renamed_files}}
  • Loading branch information
kcloutie committed Jul 3, 2023
1 parent 13a970a commit 38bef0b
Show file tree
Hide file tree
Showing 21 changed files with 706 additions and 139 deletions.
19 changes: 19 additions & 0 deletions docs/content/docs/guide/authoringprs.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ weight: 3
* `{{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.
* `{{all_changed_files}}`: The list of all files changed in the event (added, deleted, modified and renamed) separated by a comma. On pull request every file belonging to the pull request will be listed.
* `{{added_files}}`: The list of added files in the event separated by a comma. On pull request every added file belonging to the pull request will be listed.
* `{{deleted_files}}`: The list of deleted files in the event separated by a comma. On pull request every deleted file belonging to the pull request will be listed.
* `{{modified_files}}`: The list of modified files in the event separated by a comma. On pull request every modified file belonging to the pull request will be listed.
* `{{renamed_files}}`: The list of renamed files in the event separated by a comma. On pull request every renamed file belonging to the pull request will be listed.

* 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 Expand Up @@ -136,6 +141,20 @@ suffix) in the `docs` directory :
event == "pull_request" && "docs/*.md".pathChanged()
```

This example will match any changed file (added, modified, removed or renamed) that was in the `tmp` directory:

```yaml
pipelinesascode.tekton.dev/on-cel-expression: |
all_changed_files.matches('tmp/')
```

This example will match any added file that was in the `src` directory:

```yaml
pipelinesascode.tekton.dev/on-cel-expression: |
added_files.matches('src/')
```

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

```yaml
Expand Down
7 changes: 5 additions & 2 deletions pkg/customparams/customparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/openshift-pipelines/pipelines-as-code/pkg/kubeinteraction"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
sectypes "github.com/openshift-pipelines/pipelines-as-code/pkg/secrets/types"
"go.uber.org/zap"
)
Expand All @@ -19,15 +20,17 @@ type CustomParams struct {
k8int kubeinteraction.Interface
eventEmitter *events.EventEmitter
repo *v1alpha1.Repository
vcx provider.Interface
}

func NewCustomParams(event *info.Event, repo *v1alpha1.Repository, run *params.Run, k8int kubeinteraction.Interface, eventEmitter *events.EventEmitter) CustomParams {
func NewCustomParams(event *info.Event, repo *v1alpha1.Repository, run *params.Run, k8int kubeinteraction.Interface, eventEmitter *events.EventEmitter, prov provider.Interface) CustomParams {
return CustomParams{
event: event,
repo: repo,
run: run,
k8int: k8int,
eventEmitter: eventEmitter,
vcx: prov,
}
}

Expand All @@ -38,7 +41,7 @@ func NewCustomParams(event *info.Event, repo *v1alpha1.Repository, run *params.R
// if multiple params name has a filter we pick up the first one that has
// matched true.
func (p *CustomParams) GetParams(ctx context.Context) (map[string]string, error) {
stdParams := p.makeStandardParamsFromEvent()
stdParams := p.makeStandardParamsFromEvent(ctx)
if p.repo.Spec.Params == nil {
return stdParams, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/customparams/customparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func TestProcessTemplates(t *testing.T) {
if tt.event == nil {
tt.event = &info.Event{}
}
p := NewCustomParams(tt.event, repo, run, &kitesthelper.KinterfaceTest{GetSecretResult: tt.secretData}, nil)
p := NewCustomParams(tt.event, repo, run, &kitesthelper.KinterfaceTest{GetSecretResult: tt.secretData}, nil, nil)
stdata, _ := testclient.SeedTestData(t, ctx, testclient.Data{})
p.eventEmitter = events.NewEventEmitter(stdata.Kube, logger)
ret, err := p.GetParams(ctx)
Expand Down
43 changes: 32 additions & 11 deletions pkg/customparams/standard.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
package customparams

import (
"context"
"fmt"
"strings"

"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"go.uber.org/zap"
)

func (p *CustomParams) getChangedFilesCommaSeparated(ctx context.Context) provider.ChangedFiles {
if p.vcx == nil {
return provider.ChangedFiles{}
}
changedFiles, err := p.vcx.GetFiles(ctx, p.event)
if err != nil {
p.eventEmitter.EmitMessage(p.repo, zap.ErrorLevel, "ParamsError", fmt.Sprintf("error getting changed files: %s", err.Error()))
}

return changedFiles
}

// makeStandardParamsFromEvent will create a map of standard params out of the event
func (p *CustomParams) makeStandardParamsFromEvent() map[string]string {
func (p *CustomParams) makeStandardParamsFromEvent(ctx context.Context) map[string]string {
repoURL := p.event.URL
// On bitbucket server you are have a special url for checking it out, they
// seemed to fix it in 2.0 but i guess we have to live with this until then.
if p.event.CloneURL != "" {
repoURL = p.event.CloneURL
}

changedFiles := p.getChangedFilesCommaSeparated(ctx)
return map[string]string{
"revision": p.event.SHA,
"repo_url": repoURL,
"repo_owner": strings.ToLower(p.event.Organization),
"repo_name": strings.ToLower(p.event.Repository),
"target_branch": formatting.SanitizeBranch(p.event.BaseBranch),
"source_branch": formatting.SanitizeBranch(p.event.HeadBranch),
"sender": strings.ToLower(p.event.Sender),
"target_namespace": p.repo.GetNamespace(),
"event_type": p.event.EventType,
"revision": p.event.SHA,
"repo_url": repoURL,
"repo_owner": strings.ToLower(p.event.Organization),
"repo_name": strings.ToLower(p.event.Repository),
"target_branch": formatting.SanitizeBranch(p.event.BaseBranch),
"source_branch": formatting.SanitizeBranch(p.event.HeadBranch),
"sender": strings.ToLower(p.event.Sender),
"target_namespace": p.repo.GetNamespace(),
"event_type": p.event.EventType,
"all_changed_files": strings.Join(formatting.UniqueStringArray(changedFiles.All), ","),
"added_files": strings.Join(formatting.UniqueStringArray(changedFiles.Added), ","),
"deleted_files": strings.Join(formatting.UniqueStringArray(changedFiles.Deleted), ","),
"modified_files": strings.Join(formatting.UniqueStringArray(changedFiles.Modified), ","),
"renamed_files": strings.Join(formatting.UniqueStringArray(changedFiles.Renamed), ","),
}
}
40 changes: 28 additions & 12 deletions pkg/customparams/standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (

"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
testprovider "github.com/openshift-pipelines/pipelines-as-code/pkg/test/provider"
"gotest.tools/v3/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
rectesting "knative.dev/pkg/reconciler/testing"
)

func TestMakeStandardParamsFromEvent(t *testing.T) {
Expand All @@ -22,15 +24,20 @@ func TestMakeStandardParamsFromEvent(t *testing.T) {
}

result := map[string]string{
"event_type": "pull_request",
"repo_name": "repo",
"repo_owner": "org",
"repo_url": "https://paris.com",
"revision": "1234567890",
"sender": "sender",
"source_branch": "foo",
"target_branch": "main",
"target_namespace": "myns",
"event_type": "pull_request",
"repo_name": "repo",
"repo_owner": "org",
"repo_url": "https://paris.com",
"revision": "1234567890",
"sender": "sender",
"source_branch": "foo",
"target_branch": "main",
"target_namespace": "myns",
"all_changed_files": "added.go,deleted.go,modified.go,renamed.go",
"added_files": "added.go",
"deleted_files": "deleted.go",
"modified_files": "modified.go",
"renamed_files": "renamed.go",
}

repo := &v1alpha1.Repository{
Expand All @@ -40,15 +47,24 @@ func TestMakeStandardParamsFromEvent(t *testing.T) {
},
}

p := NewCustomParams(event, repo, nil, nil, nil)
params := p.makeStandardParamsFromEvent()
ctx, _ := rectesting.SetupFakeContext(t)
vcx := &testprovider.TestProviderImp{
WantAllChangedFiles: []string{"added.go", "deleted.go", "modified.go", "renamed.go"},
WantAddedFiles: []string{"added.go"},
WantDeletedFiles: []string{"deleted.go"},
WantModifiedFiles: []string{"modified.go"},
WantRenamedFiles: []string{"renamed.go"},
}

p := NewCustomParams(event, repo, nil, nil, nil, vcx)
params := p.makeStandardParamsFromEvent(ctx)
assert.DeepEqual(t, params, result)

nevent := &info.Event{}
event.DeepCopyInto(nevent)
nevent.CloneURL = "https://blahblah"
p.event = nevent
nparams := p.makeStandardParamsFromEvent()
nparams := p.makeStandardParamsFromEvent(ctx)
result["repo_url"] = nevent.CloneURL
assert.DeepEqual(t, nparams, result)
}
13 changes: 13 additions & 0 deletions pkg/formatting/array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package formatting

func UniqueStringArray(slice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
40 changes: 40 additions & 0 deletions pkg/formatting/array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package formatting

import (
"reflect"
"testing"
)

func TestUniqueStringArray(t *testing.T) {
type args struct {
slice []string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "no duplicates",
args: args{
slice: []string{"1", "2"},
},
want: []string{"1", "2"},
},

{
name: "with duplicates",
args: args{
slice: []string{"1", "2", "1", "2", "1", "2", "3", "2", "5", "5"},
},
want: []string{"1", "2", "3", "5"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := UniqueStringArray(tt.args.slice); !reflect.DeepEqual(got, tt.want) {
t.Errorf("UniqueStringArray() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 38bef0b

Please sign in to comment.