-
Notifications
You must be signed in to change notification settings - Fork 70
Support team assignments when creating and copying pipelines #998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lizrabuya
wants to merge
6
commits into
main
Choose a base branch
from
feat/pipeline-team-assignments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d65abef
Preserve team assignments when copying pipelines
lizrabuya a47b17b
Resolve pipeline team assignments by team name
lizrabuya 1348a27
Resolve pipeline team assignments by slug
lizrabuya cc1b5ff
Test pipeline create and copy without team assignments
lizrabuya 8695f1f
Merge branch 'main' into feat/pipeline-team-assignments
lizrabuya 8ddd8a6
Require explicit team assignments when copying pipelines
lizrabuya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,72 @@ | ||
| package pipeline | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/buildkite/cli/v3/pkg/cmd/factory" | ||
| buildkite "github.com/buildkite/go-buildkite/v5" | ||
| ) | ||
|
|
||
| func validateTeams(teams map[string]string) error { | ||
| for slug, access := range teams { | ||
| if strings.TrimSpace(slug) == "" { | ||
| return fmt.Errorf("--team requires a non-empty team slug") | ||
| } | ||
| switch access { | ||
| case "read_only", "build_and_read", "manage_build_and_read": | ||
| default: | ||
| return fmt.Errorf("invalid --team access level %q: use read_only, build_and_read, or manage_build_and_read", access) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func resolveTeamSlugs(ctx context.Context, client *buildkite.Client, org string, slugs map[string]string) (map[string]string, error) { | ||
| if len(slugs) == 0 { | ||
| return nil, nil | ||
| } | ||
| assignments := make(map[string]string, len(slugs)) | ||
| matched := make(map[string]bool, len(slugs)) | ||
| opts := &buildkite.TeamsListOptions{ListOptions: buildkite.ListOptions{Page: 1, PerPage: 100}} | ||
| for { | ||
| teams, resp, err := client.Teams.List(ctx, org, opts) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not resolve team slugs in organization %q: %w", org, err) | ||
| } | ||
| for _, team := range teams { | ||
| access, requested := slugs[team.Slug] | ||
| if !requested { | ||
| continue | ||
| } | ||
| matched[team.Slug] = true | ||
| assignments[team.ID] = access | ||
| } | ||
| if resp.NextPage == 0 { | ||
| break | ||
| } | ||
| opts.Page = resp.NextPage | ||
| } | ||
| for slug := range slugs { | ||
| if !matched[slug] { | ||
| return nil, fmt.Errorf("team slug %q not found in organization %q; use the exact team slug from bk team list", slug, org) | ||
| } | ||
| } | ||
| return assignments, nil | ||
| } | ||
|
|
||
| func (c *CopyCmd) resolveTeams(ctx context.Context, f *factory.Factory, sourceOrg, targetOrg string) (map[string]string, error) { | ||
| if len(c.Teams) == 0 { | ||
| return nil, nil | ||
| } | ||
| client := f.RestAPIClient | ||
| if targetOrg != sourceOrg { | ||
| var err error | ||
| client, err = c.getClientForOrg(f, targetOrg) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| return resolveTeamSlugs(ctx, client, targetOrg, c.Teams) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we stop paginating once all requested slugs have been found? Currently, even if every requested team is on page 1, this continues through every remaining page. That adds unnecessary API calls and means a later page failing or being rate-limited can prevent pipeline creation despite all assignments already being resolved. Checking
len(matched) == len(slugs)before followingresp.NextPagewould avoid that.