-
Notifications
You must be signed in to change notification settings - Fork 23
Add restore and mark-spam commands #186
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
code-monger-givenall
wants to merge
1
commit into
basecamp:main
Choose a base branch
from
code-monger-givenall:agent/restore-mark-spam-refresh
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
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
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,46 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type markSpamCommand struct { | ||
| cmd *cobra.Command | ||
| } | ||
|
|
||
| func newMarkSpamCommand() *markSpamCommand { | ||
| command := &markSpamCommand{} | ||
| command.cmd = &cobra.Command{ | ||
| Use: "mark-spam <entry-id>", | ||
| Short: "Mark an email entry as spam", | ||
| Long: "Mark one email entry as spam. This changes mailbox state, so confirm the exact entry ID before running it.", | ||
| Example: " hey mark-spam 12345", | ||
| Annotations: map[string]string{ | ||
| "agent_notes": "State-changing action. Confirm the exact entry ID before marking it as spam.", | ||
| }, | ||
| Args: usageExactOneArg(), | ||
| RunE: command.run, | ||
| } | ||
| return command | ||
| } | ||
|
|
||
| func (c *markSpamCommand) run(cmd *cobra.Command, args []string) error { | ||
| entryID, err := parsePositiveControlID(args[0], "entry") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := rejectControlListFormats(); err != nil { | ||
| return err | ||
| } | ||
| if err := requireAuth(); err != nil { | ||
| return err | ||
| } | ||
| if err := sdk.Entries().MarkSpam(cmd.Context(), entryID); err != nil { | ||
| return convertSDKError(err) | ||
| } | ||
|
|
||
| result := topicControlResult{EntryID: entryID, Action: "marked_spam"} | ||
| return writeTopicControlResult(cmd, result, fmt.Sprintf("Entry %d marked as spam", entryID)) | ||
| } |
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,46 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type restoreCommand struct { | ||
| cmd *cobra.Command | ||
| } | ||
|
|
||
| func newRestoreCommand() *restoreCommand { | ||
| command := &restoreCommand{} | ||
| command.cmd = &cobra.Command{ | ||
| Use: "restore <topic-id>", | ||
| Short: "Restore a topic to active mail", | ||
| Long: "Restore a topic from Trash to active mail. This changes mailbox state, so confirm the exact topic ID before running it.", | ||
| Example: " hey restore 12345", | ||
| Annotations: map[string]string{ | ||
| "agent_notes": "State-changing action. Confirm the exact topic ID before restoring it from Trash.", | ||
| }, | ||
| Args: usageExactOneArg(), | ||
| RunE: command.run, | ||
| } | ||
| return command | ||
| } | ||
|
|
||
| func (c *restoreCommand) run(cmd *cobra.Command, args []string) error { | ||
| topicID, err := parsePositiveControlID(args[0], "topic") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := rejectControlListFormats(); err != nil { | ||
| return err | ||
| } | ||
| if err := requireAuth(); err != nil { | ||
| return err | ||
| } | ||
| if err := sdk.Topics().Restore(cmd.Context(), topicID); err != nil { | ||
| return convertSDKError(err) | ||
| } | ||
|
|
||
| result := topicControlResult{TopicID: topicID, Action: "restored"} | ||
| return writeTopicControlResult(cmd, result, fmt.Sprintf("Topic %d restored to active mail", topicID)) | ||
| } |
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,43 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/basecamp/hey-cli/internal/output" | ||
| ) | ||
|
|
||
| type topicControlResult struct { | ||
| TopicID int64 `json:"topic_id,omitempty"` | ||
| EntryID int64 `json:"entry_id,omitempty"` | ||
| Action string `json:"action"` | ||
| } | ||
|
|
||
| func parsePositiveControlID(value, kind string) (int64, error) { | ||
| id, err := strconv.ParseInt(value, 10, 64) | ||
| if err != nil || id <= 0 { | ||
| return 0, output.ErrUsage(fmt.Sprintf("invalid %s ID: %s", kind, value)) | ||
| } | ||
| return id, nil | ||
| } | ||
|
|
||
| func rejectControlListFormats() error { | ||
| switch writer.EffectiveFormat() { | ||
| case output.FormatIDs: | ||
| return output.ErrUsage("--ids-only requires list data") | ||
| case output.FormatCount: | ||
| return output.ErrUsage("--count requires list data") | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func writeTopicControlResult(cmd *cobra.Command, result topicControlResult, summary string) error { | ||
| if writer.IsStyled() { | ||
| fmt.Fprintln(cmd.OutOrStdout(), summary+".") | ||
| return nil | ||
| } | ||
| return writeOK(result, output.WithSummary(summary)) | ||
| } |
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,134 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/basecamp/hey-cli/internal/output" | ||
| ) | ||
|
|
||
| func runTopicControl(t *testing.T, server *httptest.Server, args ...string) (output.Response, error) { | ||
| t.Helper() | ||
| t.Setenv("HEY_TOKEN", "test-token") | ||
| t.Setenv("HEY_NO_KEYRING", "1") | ||
| t.Setenv("HEY_BASE_URL", "") | ||
| tmpDir := t.TempDir() | ||
| t.Setenv("XDG_CONFIG_HOME", tmpDir) | ||
| t.Setenv("XDG_STATE_HOME", tmpDir) | ||
| t.Setenv("XDG_CACHE_HOME", tmpDir) | ||
|
|
||
| root := newRootCmd() | ||
| var buf bytes.Buffer | ||
| root.SetOut(&buf) | ||
| root.SetErr(&buf) | ||
| root.SetArgs(append([]string{"--json", "--base-url", server.URL}, args...)) | ||
|
|
||
| err := root.Execute() | ||
| var resp output.Response | ||
| if buf.Len() > 0 { | ||
| if decodeErr := json.Unmarshal(buf.Bytes(), &resp); decodeErr != nil { | ||
| t.Fatalf("decode response: %v\n%s", decodeErr, buf.String()) | ||
| } | ||
| } | ||
| return resp, err | ||
| } | ||
|
|
||
| func TestTopicControlActions(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| args []string | ||
| method string | ||
| path string | ||
| summary string | ||
| }{ | ||
| {name: "restore", args: []string{"restore", "123"}, method: http.MethodPut, path: "/topics/123/status/active.json", summary: "Topic 123 restored to active mail"}, | ||
| {name: "mark spam", args: []string{"mark-spam", "456"}, method: http.MethodPut, path: "/entries/456/status/spam.json", summary: "Entry 456 marked as spam"}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != test.method || r.URL.Path != test.path { | ||
| t.Errorf("request = %s %s", r.Method, r.URL.Path) | ||
| } | ||
| w.WriteHeader(http.StatusNoContent) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| resp, err := runTopicControl(t, server, test.args...) | ||
| if err != nil { | ||
| t.Fatalf("execute: %v", err) | ||
| } | ||
| if resp.Summary != test.summary { | ||
| t.Errorf("summary = %q, want %q", resp.Summary, test.summary) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTopicControlHelpWarnsAboutMailboxChanges(t *testing.T) { | ||
| root := newRootCmd() | ||
| for _, name := range []string{"restore", "mark-spam"} { | ||
| command, _, err := root.Find([]string{name}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !strings.Contains(command.Long, "changes mailbox state") || !strings.Contains(command.Long, "confirm the exact") { | ||
| t.Errorf("%s help does not explain the state change: %q", name, command.Long) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestTopicControlsRejectInvalidInputWithoutRequest(t *testing.T) { | ||
| var requests atomic.Int64 | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| requests.Add(1) | ||
| w.WriteHeader(http.StatusNoContent) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| tests := []struct { | ||
| args []string | ||
| message string | ||
| }{ | ||
| {args: []string{"restore", "0"}, message: "invalid topic ID"}, | ||
| {args: []string{"mark-spam", "nope"}, message: "invalid entry ID"}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| _, err := runTopicControl(t, server, test.args...) | ||
| if err == nil || !strings.Contains(err.Error(), test.message) { | ||
| t.Fatalf("%v error = %v, want %q", test.args, err, test.message) | ||
| } | ||
| } | ||
| if requests.Load() != 0 { | ||
| t.Fatalf("requests = %d, want 0", requests.Load()) | ||
| } | ||
| } | ||
|
|
||
| func TestTopicControlsRejectListOnlyFormatsBeforeRequest(t *testing.T) { | ||
| var requests atomic.Int64 | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| requests.Add(1) | ||
| w.WriteHeader(http.StatusNoContent) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| for _, args := range [][]string{ | ||
| {"restore", "123", "--ids-only"}, | ||
| {"mark-spam", "456", "--count"}, | ||
| } { | ||
| _, err := runTopicControl(t, server, args...) | ||
| if err == nil || !strings.Contains(err.Error(), "requires list data") { | ||
| t.Fatalf("%v error = %v, want list format rejection", args, err) | ||
| } | ||
| } | ||
| if requests.Load() != 0 { | ||
| t.Fatalf("requests = %d, want 0", requests.Load()) | ||
| } | ||
| } |
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
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.