Skip to content

Commit 199e049

Browse files
committed
perf: remove the loop matching Pipeline URLs
By introducing a new field in the FetchedResources struct to track all remote pipeline URLs, the loop used to match the correct Pipeline URL based on the Pipeline value for the current PipelineRun becomes unnecessary. Now, the value is always the last element in the FetchedResources.PipelineURLs slice. This also prevents any nil dereferencing issues.
1 parent 3997b0a commit 199e049

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

pkg/resolve/remote.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/url"
7-
"strings"
7+
"path"
88

99
"github.com/openshift-pipelines/pipelines-as-code/pkg/matcher"
1010
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
@@ -21,24 +21,14 @@ func alreadyFetchedResource[T NamedItem](resources map[string]T, resourceName st
2121
return false
2222
}
2323

24-
func createTaskURL(eventPipelines map[string]*tektonv1.Pipeline, pipeline *tektonv1.Pipeline, tasks []string) ([]string, error) {
25-
var pURL *url.URL
26-
var err error
27-
for ep := range eventPipelines {
28-
// ensure the URL to be parsed matches the
29-
// remote pipeline of the current PipelineRun
30-
if eventPipelines[ep] != pipeline {
31-
continue
32-
}
33-
pURL, err = url.Parse(ep)
34-
if err != nil {
35-
return tasks, err
36-
}
37-
pPath := strings.SplitAfter(pURL.Path, "/")
38-
// pop the pipeline target path from the URL
39-
pPath = pPath[:len(pPath)-1]
40-
pURL.Path = strings.Join(pPath, "")
24+
func assembleTaskFQDNs(remotePipelineURL string, tasks []string) ([]string, error) {
25+
pURL, err := url.Parse(remotePipelineURL)
26+
if err != nil {
27+
return tasks, err
4128
}
29+
// pop the pipeline file path from the URL
30+
pURL.Path = path.Dir(pURL.Path)
31+
4232
taskURLS := make([]string, len(tasks))
4333
for i, t := range tasks {
4434
tURL, err := url.Parse(t)
@@ -68,8 +58,9 @@ func createTaskURL(eventPipelines map[string]*tektonv1.Pipeline, pipeline *tekto
6858
func resolveRemoteResources(ctx context.Context, rt *matcher.RemoteTasks, types TektonTypes, ropt *Opts) ([]*tektonv1.PipelineRun, error) {
6959
// contain Resources fetched for the event
7060
fetchedResourcesForEvent := FetchedResources{
71-
Tasks: map[string]*tektonv1.Task{},
72-
Pipelines: map[string]*tektonv1.Pipeline{},
61+
Tasks: map[string]*tektonv1.Task{},
62+
Pipelines: map[string]*tektonv1.Pipeline{},
63+
PipelineURLs: []string{},
7364
}
7465
pipelineRuns := []*tektonv1.PipelineRun{}
7566
for _, pipelinerun := range types.PipelineRuns {
@@ -110,6 +101,7 @@ func resolveRemoteResources(ctx context.Context, rt *matcher.RemoteTasks, types
110101
}
111102
// add the pipeline to the Resources fetched for the Event
112103
fetchedResourcesForEvent.Pipelines[remotePipeline] = pipeline
104+
fetchedResourcesForEvent.PipelineURLs = append(fetchedResourcesForEvent.PipelineURLs, remotePipeline)
113105
}
114106
}
115107
}
@@ -132,8 +124,11 @@ func resolveRemoteResources(ctx context.Context, rt *matcher.RemoteTasks, types
132124
if err != nil {
133125
return []*tektonv1.PipelineRun{}, fmt.Errorf("error getting remote task from pipeline annotations: %w", err)
134126
}
127+
// this will always work because the slice cannot be empty (there is a check
128+
// for empty strings; even with an empty string it will work), and cannot be nil
129+
remotePipelineURL := fetchedResourcesForEvent.PipelineURLs[len(fetchedResourcesForEvent.PipelineURLs)-1]
135130
// check for relative task references and assemble FQDNs
136-
pipelineTasks, err = createTaskURL(fetchedResourcesForEvent.Pipelines, fetchedResourcesForPipelineRun.Pipeline, pipelineTasks)
131+
pipelineTasks, err = assembleTaskFQDNs(remotePipelineURL, pipelineTasks)
137132
if err != nil {
138133
return []*tektonv1.PipelineRun{}, err
139134
}

pkg/resolve/resolve.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ type TektonTypes struct {
3232

3333
// Contains Fetched Resources for Event, with key equals to annotation value.
3434
type FetchedResources struct {
35-
Tasks map[string]*tektonv1.Task
36-
Pipelines map[string]*tektonv1.Pipeline
35+
Tasks map[string]*tektonv1.Task
36+
Pipelines map[string]*tektonv1.Pipeline
37+
PipelineURLs []string
3738
}
3839

3940
// Contains Fetched Resources for Run, with key equals to resource name from metadata.name field.

0 commit comments

Comments
 (0)