Repository: openshift-pipelines/pipelines-as-code
Pull request #254 (2021-10-04 19:26:28)

GetFileInsideRepo specify target branch by string
instead of the weird way of how we were doign before by a bool between
head and base, let's just specify the target branch directly. since we
need mainbranch too.

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>



Patch:
diff --git a/pkg/matcher/annotation_tasks_install.go b/pkg/matcher/annotation_tasks_install.go
index 9ae10b26..eebc8ff3 100644
--- a/pkg/matcher/annotation_tasks_install.go
+++ b/pkg/matcher/annotation_tasks_install.go
@@ -49,7 +49,7 @@ func (rt RemoteTasks) getTask(ctx context.Context, vcsintf webvcs.Interface, tas
 		defer res.Body.Close()
 		return rt.convertTotask(string(data))
 	case strings.Contains(task, "/"):
-		data, err := vcsintf.GetFileInsideRepo(ctx, rt.Run.Info.Event, task, false)
+		data, err := vcsintf.GetFileInsideRepo(ctx, rt.Run.Info.Event, task, "")
 		if err != nil {
 			return ret, err
 		}
diff --git a/pkg/webvcs/github/acl.go b/pkg/webvcs/github/acl.go
index 7812fb75..ee6b12c5 100644
--- a/pkg/webvcs/github/acl.go
+++ b/pkg/webvcs/github/acl.go
@@ -28,8 +28,8 @@ func (v VCS) IsAllowed(ctx context.Context, event *info.Event) (bool, error) {
 	return v.aclAllowedOkToTestFromAnOwner(ctx, event)
 }
 
-// allowedOkToTestFromAnOwner Goes on evry comments in a pull-request and sess
-// if there is a /ok-to-test in there running an aclCheck again on the commment
+// allowedOkToTestFromAnOwner Go over every comments in a pull request and check
+// if there is a /ok-to-test in there running an aclCheck again on the comment
 // Sender if she is an OWNER and then allow it to run CI.
 // TODO: pull out the github logic from there in an agnostic way.
 func (v VCS) aclAllowedOkToTestFromAnOwner(ctx context.Context, event *info.Event) (bool, error) {
@@ -140,12 +140,9 @@ func (v VCS) checkSenderOrgMembership(ctx context.Context, runevent *info.Event)
 // getFileFromDefaultBranch will get a file directly from the Default BaseBranch as
 // configured in runinfo which is directly set in webhook by Github
 func (v VCS) getFileFromDefaultBranch(ctx context.Context, path string, runevent *info.Event) (string, error) {
-	runEventOnMain := runevent
-	runEventOnMain.BaseBranch = runEventOnMain.DefaultBranch
-
-	tektonyaml, err := v.GetFileInsideRepo(ctx, runEventOnMain, path, true)
+	tektonyaml, err := v.GetFileInsideRepo(ctx, runevent, path, runevent.DefaultBranch)
 	if err != nil {
-		return "", fmt.Errorf("cannot find %s inside the %s branch: %w", path, runEventOnMain.BaseBranch, err)
+		return "", fmt.Errorf("cannot find %s inside the %s branch: %w", path, runevent.DefaultBranch, err)
 	}
 	return tektonyaml, err
 }
diff --git a/pkg/webvcs/github/github.go b/pkg/webvcs/github/github.go
index 93bdb8e4..009bba78 100644
--- a/pkg/webvcs/github/github.go
+++ b/pkg/webvcs/github/github.go
@@ -84,9 +84,10 @@ func (v *VCS) GetCommitInfo(ctx context.Context, runevent *info.Event) error {
 // GetFileInsideRepo Get a file via Github API using the runinfo information, we
 // branch is true, the user the branch as ref isntead of the SHA
 // TODO: merge GetFileInsideRepo amd GetTektonDir
-func (v *VCS) GetFileInsideRepo(ctx context.Context, runevent *info.Event, path string, branch bool) (string, error) {
+func (v *VCS) GetFileInsideRepo(ctx context.Context, runevent *info.Event, path, target string) (string,
+	error) {
 	ref := runevent.SHA
-	if branch {
+	if target != "" {
 		ref = runevent.BaseBranch
 	}
 
diff --git a/pkg/webvcs/interface.go b/pkg/webvcs/interface.go
index 09b227be..f9a0dd4c 100644
--- a/pkg/webvcs/interface.go
+++ b/pkg/webvcs/interface.go
@@ -20,8 +20,8 @@ type Interface interface {
 	ParsePayload(context.Context, *params.Run, string) (*info.Event, error)
 	IsAllowed(context.Context, *info.Event) (bool, error)
 	CreateStatus(context.Context, *info.Event, info.PacOpts, StatusOpts) error
-	GetTektonDir(context.Context, *info.Event, string) (string, error)            // ctx, event, path
-	GetFileInsideRepo(context.Context, *info.Event, string, bool) (string, error) // ctx, event, path, branch
+	GetTektonDir(context.Context, *info.Event, string) (string, error)              // ctx, event, path
+	GetFileInsideRepo(context.Context, *info.Event, string, string) (string, error) // ctx, event, path, branch
 	SetClient(context.Context, info.PacOpts)
 	GetCommitInfo(context.Context, *info.Event) error
 }


Test patch:
diff --git a/pkg/test/webvcs/testwebvcs.go b/pkg/test/webvcs/testwebvcs.go
index 56a638f2..49d4cbd6 100644
--- a/pkg/test/webvcs/testwebvcs.go
+++ b/pkg/test/webvcs/testwebvcs.go
@@ -47,9 +47,10 @@ func (v *TestWebVCSImp) GetTektonDir(ctx context.Context, event *info.Event, s s
 	return v.TektonDirTemplate, nil
 }
 
-func (v *TestWebVCSImp) GetFileInsideRepo(ctx context.Context, event *info.Event, s string, b bool) (string, error) {
-	if val, ok := v.FilesInsideRepo[s]; ok {
+func (v *TestWebVCSImp) GetFileInsideRepo(ctx context.Context, event *info.Event, file string,
+	targetBranch string) (string, error) {
+	if val, ok := v.FilesInsideRepo[file]; ok {
 		return val, nil
 	}
-	return "", fmt.Errorf("could not find %s in tests", s)
+	return "", fmt.Errorf("could not find %s in tests", file)
 }
diff --git a/pkg/webvcs/github/github_test.go b/pkg/webvcs/github/github_test.go
index 4054eb5b..a822a8f0 100644
--- a/pkg/webvcs/github/github_test.go
+++ b/pkg/webvcs/github/github_test.go
@@ -182,7 +182,7 @@ func TestGetFileInsideRepo(t *testing.T) {
 	}
 	for _, tt := range testGetTektonDir {
 		t.Run(tt.name, func(t *testing.T) {
-			got, err := gcvs.GetFileInsideRepo(ctx, &tt.args.runevent, tt.args.path, false)
+			got, err := gcvs.GetFileInsideRepo(ctx, &tt.args.runevent, tt.args.path, "")
 			tt.args.assertion(t, got, err)
 		})
 	}