diff --git a/pkg/resolve/remote.go b/pkg/resolve/remote.go index 501a836a2..f3ad62358 100644 --- a/pkg/resolve/remote.go +++ b/pkg/resolve/remote.go @@ -3,6 +3,8 @@ package resolve import ( "context" "fmt" + "net/url" + "strings" "github.com/openshift-pipelines/pipelines-as-code/pkg/matcher" tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1" @@ -19,6 +21,38 @@ func alreadyFetchedResource[T NamedItem](resources map[string]T, resourceName st return false } +func createTaskURL(eventPipelines map[string]*tektonv1.Pipeline, pipeline *tektonv1.Pipeline, tasks []string) ([]string, error) { + var pURL *url.URL + var err error + for ep := range eventPipelines { + // ensure the URL to be parsed matches the + // remote pipeline of the current PipelineRun + if eventPipelines[ep] != pipeline { + continue + } + pURL, err = url.Parse(ep) + if err != nil { + return tasks, err + } + pPath := strings.SplitAfter(pURL.Path, "/") + // pop the pipeline target path from the URL + pPath = pPath[:len(pPath)-1] + pURL.Path = strings.Join(pPath, "") + } + taskURLS := make([]string, len(tasks)) + for i, t := range tasks { + tURL, err := url.Parse(t) + if err == nil && tURL.Scheme != "" && tURL.Host != "" { + taskURLS[i] = t + continue // it's already an absolute URL + } + tURL = pURL + tURL = tURL.JoinPath(t) + taskURLS[i] = tURL.String() + } + return taskURLS, nil +} + // resolveRemoteResources will get remote tasks or Pipelines from annotations. // // It already has some tasks or pipeline coming from the tekton directory stored in [types] @@ -98,6 +132,11 @@ func resolveRemoteResources(ctx context.Context, rt *matcher.RemoteTasks, types if err != nil { return []*tektonv1.PipelineRun{}, fmt.Errorf("error getting remote task from pipeline annotations: %w", err) } + // check for relative task references and assemble FQDNs + pipelineTasks, err = createTaskURL(fetchedResourcesForEvent.Pipelines, fetchedResourcesForPipelineRun.Pipeline, pipelineTasks) + if err != nil { + return []*tektonv1.PipelineRun{}, err + } } }