Skip to content

Commit

Permalink
Add PipelinePicker to ResolveFromRepository (#245)
Browse files Browse the repository at this point in the history
* Add picker argument to resolver

* Update resolver to use the picker

* Add documentation for ResolveFromRepository

* Add docs for PipelinePicker

* Simplify

* Add picker to config resolver
  • Loading branch information
jradtilbrook authored May 13, 2024
1 parent ea8864b commit 1018ed9
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 24 deletions.
8 changes: 4 additions & 4 deletions internal/pipeline/resolver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"github.com/buildkite/cli/v3/internal/pipeline"
)

func ResolveFromConfig(conf *config.Config) PipelineResolverFn {
func ResolveFromConfig(conf *config.Config, picker PipelinePicker) PipelineResolverFn {
return func(context.Context) (*pipeline.Pipeline, error) {
preferredPipelines := conf.PreferredPipelines()
pipelines := conf.PreferredPipelines()

if len(preferredPipelines) == 0 {
if len(pipelines) == 0 {
return nil, nil
}

return &preferredPipelines[0], nil
return picker(pipelines), nil
}
}
6 changes: 3 additions & 3 deletions internal/pipeline/resolver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestResolvePipelineFromConfig(t *testing.T) {
t.Parallel()

conf := config.New(afero.NewMemMapFs(), nil)
resolve := ResolveFromConfig(conf)
resolve := ResolveFromConfig(conf, PassthruPicker)
selected, err := resolve(context.Background())
if err != nil {
t.Errorf("failed to resolve from config")
Expand All @@ -33,7 +33,7 @@ func TestResolvePipelineFromConfig(t *testing.T) {
pipelines := []pipeline.Pipeline{{Name: "pipeline1"}}
conf := config.New(afero.NewMemMapFs(), nil)
conf.SetPreferredPipelines(pipelines)
resolve := ResolveFromConfig(conf)
resolve := ResolveFromConfig(conf, PassthruPicker)
selected, err := resolve(context.Background())
if err != nil {
t.Errorf("failed to resolve from config")
Expand All @@ -54,7 +54,7 @@ func TestResolvePipelineFromConfig(t *testing.T) {
pipelines := []pipeline.Pipeline{{Name: "pipeline1"}, {Name: "pipeline2"}, {Name: "pipeline3"}}
conf := config.New(afero.NewMemMapFs(), nil)
conf.SetPreferredPipelines(pipelines)
resolve := ResolveFromConfig(conf)
resolve := ResolveFromConfig(conf, PassthruPicker)
selected, err := resolve(context.Background())
if err != nil {
t.Errorf("failed to resolve from config")
Expand Down
3 changes: 3 additions & 0 deletions internal/pipeline/resolver/picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"github.com/buildkite/cli/v3/internal/pipeline"
)

// PipelinePicker is a function used to pick a pipeline from a list.
//
// It is indended to be used from pipeline resolvers that resolve multiple pipelines.
type PipelinePicker func([]pipeline.Pipeline) *pipeline.Pipeline

func PassthruPicker(p []pipeline.Pipeline) *pipeline.Pipeline {
Expand Down
19 changes: 10 additions & 9 deletions internal/pipeline/resolver/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
"github.com/go-git/go-git/v5"
)

func ResolveFromRepository(f *factory.Factory) PipelineResolverFn {
// ResolveFromRepository finds pipelines based on the current repository.
//
// It queries the API for all pipelines in the organization that match the repository's URL.
// It delegates picking one from the list of matches to the `picker`.
func ResolveFromRepository(f *factory.Factory, picker PipelinePicker) PipelineResolverFn {
return func(context.Context) (*pipeline.Pipeline, error) {
pipelines, err := resolveFromRepository(f)
if err != nil {
Expand All @@ -21,23 +25,20 @@ func ResolveFromRepository(f *factory.Factory) PipelineResolverFn {
return nil, nil
}

return &pipeline.Pipeline{
Name: pipelines[0],
Org: f.Config.OrganizationSlug(),
}, nil
return picker(pipelines), nil
}
}

func resolveFromRepository(f *factory.Factory) ([]string, error) {
func resolveFromRepository(f *factory.Factory) ([]pipeline.Pipeline, error) {
repos, err := getRepoURLs(f.GitRepository)
if err != nil {
return nil, err
}
return filterPipelines(repos, f.Config.OrganizationSlug(), f.RestAPIClient)
}

func filterPipelines(repoURLs []string, org string, client *buildkite.Client) ([]string, error) {
var currentPipelines []string
func filterPipelines(repoURLs []string, org string, client *buildkite.Client) ([]pipeline.Pipeline, error) {
var currentPipelines []pipeline.Pipeline
page := 1
per_page := 30
for more_pipelines := true; more_pipelines; {
Expand All @@ -56,7 +57,7 @@ func filterPipelines(repoURLs []string, org string, client *buildkite.Client) ([
for _, u := range repoURLs {
gitUrl := u[strings.LastIndex(u, "/")+1:]
if strings.Contains(*p.Repository, gitUrl) {
currentPipelines = append(currentPipelines, *p.Slug)
currentPipelines = append(currentPipelines, pipeline.Pipeline{Name: *p.Slug, Org: org})
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/build/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func NewCmdBuildCancel(f *factory.Factory) *cobra.Command {
buildId := args[0]
resolvers := resolver.NewAggregateResolver(
resolver.ResolveFromPositionalArgument(args, 1, f.Config),
resolver.ResolveFromConfig(f.Config),
resolver.ResolveFromRepository(f),
resolver.ResolveFromConfig(f.Config, resolver.PassthruPicker),
resolver.ResolveFromRepository(f, resolver.PassthruPicker),
)
var pipeline pipeline.Pipeline
r := io.NewPendingCommand(func() tea.Msg {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/build/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func NewCmdBuildNew(f *factory.Factory) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
resolvers := resolver.NewAggregateResolver(
resolver.ResolveFromPositionalArgument(args, 0, f.Config),
resolver.ResolveFromConfig(f.Config),
resolver.ResolveFromRepository(f),
resolver.ResolveFromConfig(f.Config, resolver.PassthruPicker),
resolver.ResolveFromRepository(f, resolver.PassthruPicker),
)
var pipeline pipeline.Pipeline
r := io.NewPendingCommand(func() tea.Msg {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/build/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func NewCmdBuildRebuild(f *factory.Factory) *cobra.Command {
buildId := args[0]
resolvers := resolver.NewAggregateResolver(
resolver.ResolveFromPositionalArgument(args, 1, f.Config),
resolver.ResolveFromConfig(f.Config),
resolver.ResolveFromRepository(f),
resolver.ResolveFromConfig(f.Config, resolver.PassthruPicker),
resolver.ResolveFromRepository(f, resolver.PassthruPicker),
)
var pipeline pipeline.Pipeline
r := io.NewPendingCommand(func() tea.Msg {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/build/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func NewCmdBuildView(f *factory.Factory) *cobra.Command {

resolvers := resolver.NewAggregateResolver(
resolver.ResolveFromPositionalArgument(args, 1, f.Config),
resolver.ResolveFromConfig(f.Config),
resolver.ResolveFromRepository(f),
resolver.ResolveFromConfig(f.Config, resolver.PassthruPicker),
resolver.ResolveFromRepository(f, resolver.PassthruPicker),
)

var pipeline pipeline.Pipeline
Expand Down

0 comments on commit 1018ed9

Please sign in to comment.