-
Notifications
You must be signed in to change notification settings - Fork 576
feat(gmail): export filters as WebUI XML #553
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -805,7 +805,8 @@ gog gmail batch modify <messageId> <messageId> --add STARRED --remove INBOX | |
| gog gmail filters list | ||
| gog gmail filters create --from '[email protected]' --add-label 'Notifications' | ||
| gog gmail filters delete <filterId> | ||
| gog gmail filters export --out ./filters.json | ||
| gog gmail filters export --out ./mailFilters.xml # Gmail WebUI importable XML | ||
| gog gmail filters export --format json --out ./filters.json | ||
|
|
||
| # Settings | ||
| gog gmail autoforward get | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package cmd | |
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "encoding/xml" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
|
|
@@ -210,19 +211,52 @@ func TestGmailFiltersList_NoFilters(t *testing.T) { | |
|
|
||
| func TestGmailFiltersExport(t *testing.T) { | ||
| origNew := newGmailService | ||
| t.Cleanup(func() { newGmailService = origNew }) | ||
| origNow := nowGmailFiltersExport | ||
| t.Cleanup(func() { | ||
| newGmailService = origNew | ||
| nowGmailFiltersExport = origNow | ||
| }) | ||
| nowGmailFiltersExport = func() time.Time { return time.Date(2026, 5, 5, 1, 2, 3, 0, time.UTC) } | ||
|
|
||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if strings.Contains(r.URL.Path, "/gmail/v1/users/me/settings/filters") && r.Method == http.MethodGet { | ||
| switch { | ||
| case strings.Contains(r.URL.Path, "/gmail/v1/users/me/labels") && r.Method == http.MethodGet: | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _ = json.NewEncoder(w).Encode(map[string]any{ | ||
| "labels": []map[string]any{ | ||
| {"id": "Label_1", "name": "Notifications & Alerts"}, | ||
| }, | ||
| }) | ||
| return | ||
| case strings.Contains(r.URL.Path, "/gmail/v1/users/me/settings/filters") && r.Method == http.MethodGet: | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _ = json.NewEncoder(w).Encode(map[string]any{ | ||
| "filter": []map[string]any{ | ||
| {"id": "f1", "criteria": map[string]any{"from": "[email protected]"}}, | ||
| { | ||
| "id": "f1", | ||
| "criteria": map[string]any{ | ||
| "from": "[email protected]", | ||
| "to": "[email protected]", | ||
| "subject": "A&B", | ||
| "query": `from:alerts has:attachment`, | ||
| "negatedQuery": "category:promotions", | ||
| "hasAttachment": true, | ||
| "excludeChats": true, | ||
| "size": 1024, | ||
| "sizeComparison": "larger", | ||
| }, | ||
| "action": map[string]any{ | ||
| "addLabelIds": []string{"Label_1", "STARRED", "IMPORTANT", "CATEGORY_SOCIAL"}, | ||
| "removeLabelIds": []string{"INBOX", "UNREAD", "SPAM"}, | ||
| "forward": "[email protected]", | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| return | ||
| default: | ||
| http.NotFound(w, r) | ||
| } | ||
| http.NotFound(w, r) | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
|
|
@@ -243,12 +277,58 @@ func TestGmailFiltersExport(t *testing.T) { | |
| } | ||
| ctx := ui.WithUI(context.Background(), u) | ||
|
|
||
| t.Run("stdout json", func(t *testing.T) { | ||
| t.Run("stdout xml", func(t *testing.T) { | ||
| out := captureStdout(t, func() { | ||
| if err := runKong(t, &GmailFiltersExportCmd{}, []string{}, ctx, flags); err != nil { | ||
| t.Fatalf("export stdout: %v", err) | ||
| } | ||
| }) | ||
| if !strings.HasPrefix(out, xml.Header) { | ||
| t.Fatalf("missing XML header: %q", out) | ||
| } | ||
| if !strings.Contains(out, `xmlns:apps="http://schemas.google.com/apps/2006"`) { | ||
| t.Fatalf("missing apps namespace: %q", out) | ||
| } | ||
| if !strings.Contains(out, `name="label" value="Notifications & Alerts"`) { | ||
| t.Fatalf("missing escaped label name: %q", out) | ||
| } | ||
| for _, want := range []string{ | ||
| `name="from" value="[email protected]"`, | ||
| `name="subject" value="A&B"`, | ||
| `name="hasTheWord" value="from:alerts has:attachment"`, | ||
| `name="doesNotHaveTheWord" value="category:promotions"`, | ||
| `name="hasAttachment" value="true"`, | ||
| `name="excludeChats" value="true"`, | ||
| `name="size" value="1024"`, | ||
| `name="sizeUnit" value="s_sb"`, | ||
| `name="sizeOperator" value="s_sl"`, | ||
| `name="shouldStar" value="true"`, | ||
| `name="shouldAlwaysMarkAsImportant" value="true"`, | ||
| `name="smartLabelToApply" value="^smartlabel_social"`, | ||
| `name="shouldArchive" value="true"`, | ||
| `name="shouldMarkAsRead" value="true"`, | ||
| `name="shouldNeverSpam" value="true"`, | ||
| `name="forwardTo" value="[email protected]"`, | ||
| } { | ||
| if !strings.Contains(out, want) { | ||
| t.Fatalf("missing %s in XML:\n%s", want, out) | ||
| } | ||
| } | ||
| var parsed gmailFiltersXMLFeed | ||
| if err := xml.Unmarshal([]byte(out), &parsed); err != nil { | ||
| t.Fatalf("xml parse: %v", err) | ||
| } | ||
| if parsed.Author.Email != "[email protected]" || len(parsed.Entries) != 1 { | ||
| t.Fatalf("unexpected parsed feed: %#v", parsed) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("stdout json compatibility", func(t *testing.T) { | ||
| out := captureStdout(t, func() { | ||
| if err := runKong(t, &GmailFiltersExportCmd{}, []string{"--format", "json"}, ctx, flags); err != nil { | ||
| t.Fatalf("export stdout: %v", err) | ||
| } | ||
| }) | ||
| var payload map[string]any | ||
| if err := json.Unmarshal([]byte(out), &payload); err != nil { | ||
| t.Fatalf("json parse: %v", err) | ||
|
|
@@ -259,23 +339,52 @@ func TestGmailFiltersExport(t *testing.T) { | |
| } | ||
| }) | ||
|
|
||
| t.Run("file export", func(t *testing.T) { | ||
| path := t.TempDir() + "/filters.json" | ||
| t.Run("global json keeps old stdout json", func(t *testing.T) { | ||
| jsonCtx := outfmt.WithMode(ctx, outfmt.Mode{JSON: true}) | ||
| jsonFlags := *flags | ||
| jsonFlags.JSON = true | ||
| out := captureStdout(t, func() { | ||
| if err := runKong(t, &GmailFiltersExportCmd{}, []string{}, jsonCtx, &jsonFlags); err != nil { | ||
| t.Fatalf("export stdout: %v", err) | ||
| } | ||
| }) | ||
| var payload map[string]any | ||
| if err := json.Unmarshal([]byte(out), &payload); err != nil { | ||
| t.Fatalf("json parse: %v", err) | ||
| } | ||
| filters, ok := payload["filters"].([]any) | ||
| if !ok || len(filters) != 1 { | ||
| t.Fatalf("unexpected payload: %#v", payload) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("file xml export", func(t *testing.T) { | ||
| path := t.TempDir() + "/mailFilters.xml" | ||
| if err := runKong(t, &GmailFiltersExportCmd{}, []string{"--out", path}, ctx, flags); err != nil { | ||
| t.Fatalf("export file: %v", err) | ||
| } | ||
| b, err := os.ReadFile(path) | ||
| if err != nil { | ||
| t.Fatalf("read export: %v", err) | ||
| } | ||
| if !strings.Contains(string(b), "<feed") || !strings.Contains(string(b), "Mail Filters") { | ||
| t.Fatalf("unexpected XML export: %s", b) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("file json export", func(t *testing.T) { | ||
| path := t.TempDir() + "/filters.json" | ||
| if err := runKong(t, &GmailFiltersExportCmd{}, []string{"--format", "json", "--out", path}, ctx, flags); err != nil { | ||
| t.Fatalf("export file: %v", err) | ||
| } | ||
| b, err := os.ReadFile(path) | ||
| if err != nil { | ||
| t.Fatalf("read export: %v", err) | ||
| } | ||
| var payload map[string]any | ||
| if err := json.Unmarshal(b, &payload); err != nil { | ||
| t.Fatalf("json parse: %v", err) | ||
| } | ||
| filters, ok := payload["filters"].([]any) | ||
| if !ok || len(filters) != 1 { | ||
| t.Fatalf("unexpected payload: %#v", payload) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
When
--format json --out ...is used, this branch marshals withjson.MarshalIndentinstead ofoutfmt.WriteJSON, so JSON-mode transforms (--results-only/--select) are no longer applied to the exported file. Before this commit,gog gmail filters export --out ...always wrote throughoutfmt.WriteJSON, so scripts relying on projected/unwrapped output now get a different file payload even though the same global JSON flags are set.Useful? React with 👍 / 👎.