-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SUP-2587 | Support inviting users via the CLI (#367)
* Allow users to be invited to the org. * Remove RunE from base User command so help prints. * Add USER as usage option in README
- Loading branch information
Showing
7 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,6 @@ | ||
query GetOrganizationID ($slug: ID!) { | ||
organization(slug: $slug){ | ||
id | ||
} | ||
} | ||
|
This file contains 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,7 @@ | ||
mutation InviteUser($organization: ID!, $emails: [String!]!) { | ||
organizationInvitationCreate( | ||
input: { organizationID: $organization, emails: $emails } | ||
) { | ||
clientMutationId | ||
} | ||
} |
This file contains 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 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,84 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/MakeNowJust/heredoc" | ||
"github.com/buildkite/cli/v3/internal/graphql" | ||
"github.com/buildkite/cli/v3/pkg/cmd/factory" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func CommandUserInvite(f *factory.Factory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "invite [emails]", | ||
Short: "Invite users to your organization", | ||
Long: heredoc.Doc(` | ||
Invite 1 or many users to your organization. | ||
`), | ||
Example: heredoc.Doc(` | ||
# Invite a single user to your organization | ||
$ bk user invite bob@supercoolorg.com | ||
# Invite multiple users to your organization | ||
$ bk user invite bob@supercoolorg.com bobs_mate@supercoolorg.com | ||
`), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 { | ||
return fmt.Errorf("at least one email address is required") | ||
} | ||
|
||
orgID, err := graphql.GetOrganizationID(cmd.Context(), f.GraphQLClient, f.Config.OrganizationSlug()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return createInvite(cmd.Context(), f, orgID.Organization.GetId(), args...) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func createInvite(ctx context.Context, f *factory.Factory, orgID string, emails ...string) error { | ||
if len(emails) == 0 { | ||
return nil | ||
} | ||
|
||
errChan := make(chan error, len(emails)) | ||
var wg sync.WaitGroup | ||
|
||
for _, email := range emails { | ||
wg.Add(1) | ||
go func(email string) { | ||
defer wg.Done() | ||
_, err := graphql.InviteUser(ctx, f.GraphQLClient, orgID, []string{email}) | ||
if err != nil { | ||
errChan <- fmt.Errorf("error creating user invite for %s: %w", email, err) | ||
} | ||
}(email) | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(errChan) | ||
}() | ||
|
||
var errs []error | ||
for err := range errChan { | ||
errs = append(errs, err) | ||
} | ||
|
||
if len(errs) > 0 { | ||
return fmt.Errorf("errors creating user invites: %v", errs) | ||
} | ||
|
||
message := "Invite sent to" | ||
if len(emails) > 1 { | ||
message = "Invites sent to" | ||
} | ||
|
||
fmt.Printf("%s: %v\n", message, emails) | ||
return nil | ||
} |
This file contains 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,26 @@ | ||
package user | ||
|
||
import ( | ||
"github.com/MakeNowJust/heredoc" | ||
"github.com/buildkite/cli/v3/pkg/cmd/factory" | ||
"github.com/buildkite/cli/v3/pkg/cmd/validation" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func CommandUser(f *factory.Factory) *cobra.Command { | ||
cmd := cobra.Command{ | ||
Use: "user <command>", | ||
Short: "Invite users to the organization", | ||
Long: heredoc.Doc(` | ||
Manage organization users via the CLI. | ||
To invite a user: | ||
bk user invite [email address] | ||
`), | ||
PersistentPreRunE: validation.CheckValidConfiguration(f.Config), | ||
} | ||
|
||
cmd.AddCommand(CommandUserInvite(f)) | ||
|
||
return &cmd | ||
} |