-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
21 changed files
with
706 additions
and
139 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
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 |
---|---|---|
@@ -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), ","), | ||
} | ||
} |
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.