-
Notifications
You must be signed in to change notification settings - Fork 25
Add personal access token commands #157
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7d54448
Add personal access token commands
robzolkos 9b86152
Address PR review feedback on token commands
robzolkos 2ff3806
Add token command to human catalog
robzolkos f73886c
Fix token command validation and help
robzolkos 5b00475
Harden token e2e cleanup
robzolkos 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package clitests | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestAccessTokenCRUD(t *testing.T) { | ||
| h := newHarness(t) | ||
| description := "CLI Test Token " + strconv.FormatInt(time.Now().UnixNano(), 10) | ||
|
|
||
| create := h.Run("token", "create", "--description", description, "--permission", "read") | ||
| assertOK(t, create) | ||
| tokenID := create.GetDataString("id") | ||
| if tokenID == "" { | ||
| t.Fatal("no token ID in create response") | ||
| } | ||
| deleted := false | ||
| t.Cleanup(func() { | ||
| if !deleted { | ||
| newHarness(t).Run("token", "delete", tokenID) | ||
| } | ||
| }) | ||
| if create.GetDataString("token") == "" { | ||
| t.Fatal("expected raw token value in create response") | ||
| } | ||
|
|
||
| list := h.Run("token", "list") | ||
| assertOK(t, list) | ||
| found := false | ||
| for _, item := range list.GetDataArray() { | ||
| if mapValueString(asMap(item), "id") == tokenID { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| t.Fatalf("expected token list to include %q", tokenID) | ||
| } | ||
|
|
||
| deleteResult := h.Run("token", "delete", tokenID) | ||
| assertOK(t, deleteResult) | ||
| deleted = true | ||
| if !deleteResult.GetDataBool("deleted") { | ||
| t.Fatal("expected deleted=true") | ||
| } | ||
| } | ||
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,139 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/basecamp/fizzy-sdk/go/pkg/generated" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var tokenCmd = &cobra.Command{ | ||
| Use: "token", | ||
| Short: "Manage personal access tokens", | ||
| Long: "Commands for managing your personal access tokens.", | ||
| } | ||
|
|
||
| var tokenListCmd = &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List personal access tokens", | ||
| Long: "Lists your personal access tokens.", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := requireAuth(); err != nil { | ||
| return err | ||
| } | ||
| if err := requireSDK(); err != nil { | ||
| return err | ||
| } | ||
|
robzolkos marked this conversation as resolved.
|
||
|
|
||
| ac := getSDKClient() | ||
| data, _, err := ac.AccessTokens().List(cmd.Context()) | ||
| if err != nil { | ||
| return convertSDKError(err) | ||
| } | ||
|
|
||
| items := normalizeAny(data) | ||
|
|
||
| count := dataCount(items) | ||
| summary := fmt.Sprintf("%d access tokens", count) | ||
|
|
||
| breadcrumbs := []Breadcrumb{ | ||
| breadcrumb("create", "fizzy token create --description <desc> --permission <perm>", "Create a token"), | ||
| breadcrumb("delete", "fizzy token delete <id>", "Delete a token"), | ||
| } | ||
|
|
||
| printList(items, tokenColumns, summary, breadcrumbs) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| var ( | ||
| tokenCreateDescription string | ||
| tokenCreatePermission string | ||
| ) | ||
|
|
||
| var tokenCreateCmd = &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create a personal access token", | ||
| Long: "Creates a new personal access token. The token value is shown once at creation and cannot be retrieved later.", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := requireAuth(); err != nil { | ||
| return err | ||
| } | ||
| if err := requireSDK(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if tokenCreateDescription == "" { | ||
| return newRequiredFlagError("description") | ||
| } | ||
| if tokenCreatePermission == "" { | ||
| return newRequiredFlagError("permission") | ||
| } | ||
|
|
||
| ac := getSDKClient() | ||
| req := &generated.CreateAccessTokenRequest{ | ||
| Description: tokenCreateDescription, | ||
| Permission: tokenCreatePermission, | ||
| } | ||
| raw, _, err := ac.AccessTokens().Create(cmd.Context(), req) | ||
| if err != nil { | ||
| return convertSDKError(err) | ||
| } | ||
|
|
||
| result := normalizeAny(raw) | ||
| id := "" | ||
| if m, ok := result.(map[string]any); ok { | ||
| id = getStringField(m, "id") | ||
| } | ||
|
|
||
| breadcrumbs := []Breadcrumb{ | ||
| breadcrumb("list", "fizzy token list", "List tokens"), | ||
| } | ||
| if id != "" { | ||
| breadcrumbs = append(breadcrumbs, breadcrumb("delete", fmt.Sprintf("fizzy token delete %s", id), "Delete this token")) | ||
| } | ||
|
robzolkos marked this conversation as resolved.
|
||
|
|
||
| notice := "Save the token now — it will not be shown again." | ||
| printMutation(result, notice, breadcrumbs) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| var tokenDeleteCmd = &cobra.Command{ | ||
| Use: "delete TOKEN_ID", | ||
| Short: "Delete a personal access token", | ||
| Long: "Deletes a personal access token by ID.", | ||
| Args: cobra.ExactArgs(1), | ||
|
robzolkos marked this conversation as resolved.
|
||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := requireAuth(); err != nil { | ||
| return err | ||
| } | ||
| if err := requireSDK(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| ac := getSDKClient() | ||
| if _, err := ac.AccessTokens().Delete(cmd.Context(), args[0]); err != nil { | ||
| return convertSDKError(err) | ||
| } | ||
|
|
||
| breadcrumbs := []Breadcrumb{ | ||
| breadcrumb("list", "fizzy token list", "List remaining tokens"), | ||
| } | ||
|
|
||
| printMutation(map[string]any{"deleted": true, "id": args[0]}, "", breadcrumbs) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(tokenCmd) | ||
|
|
||
| tokenCmd.AddCommand(tokenListCmd) | ||
|
|
||
|
robzolkos marked this conversation as resolved.
|
||
| tokenCreateCmd.Flags().StringVar(&tokenCreateDescription, "description", "", "Token description (required)") | ||
| tokenCreateCmd.Flags().StringVar(&tokenCreatePermission, "permission", "", "Token permission (required)") | ||
| tokenCmd.AddCommand(tokenCreateCmd) | ||
|
|
||
| tokenCmd.AddCommand(tokenDeleteCmd) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.