Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.48.1 - Unreleased

- AdSense: show the report's API warnings and a "showing N of M matched rows" note on stderr in `adsense reports query` table and `--plain` output (and `adsense reports saved query`), as JSON output already includes them. A report cut short by `--max` or by the 100,000-row limit previously looked complete. (#1324) — thanks @saariuslystoned.

## 0.48.0 - 2026-11-05

**Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates.
Expand Down
102 changes: 102 additions & 0 deletions internal/cmd/adsense_report_warnings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cmd

import (
"encoding/json"
"net/http"
"strings"
"testing"
)

// A truncated report (totalMatchedRows above the rows returned) and the API's
// own warnings reach JSON output; table and --plain output must not drop them.
func TestExecute_AdSenseReportsQuery_TableShowsTruncationAndWarnings(t *testing.T) {
svc := newAdSenseTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !(r.Method == http.MethodGet && strings.Contains(r.URL.Path, ":generate")) {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"headers": []map[string]any{
{"name": "DATE", "type": "DIMENSION"},
{"name": "ESTIMATED_EARNINGS", "type": "METRIC_CURRENCY"},
},
"rows": []map[string]any{
{"cells": []map[string]any{{"value": "2026-02-01"}, {"value": "1.23"}}},
{"cells": []map[string]any{{"value": "2026-02-02"}, {"value": "4.56"}}},
},
"totalMatchedRows": "250000",
"warnings": []string{"Some requested metrics are not available for this date range."},
})
}))
for _, mode := range []string{"--plain", ""} {
args := []string{"--account", "a@b.com", "adsense", "reports", "query", "pub-123", "--metrics", "estimated_earnings"}
if mode != "" {
args = append([]string{mode}, args...)
}
result := executeWithAdSenseTestService(t, args, svc)
if result.err != nil {
t.Fatalf("%q: Execute: %v", mode, result.err)
}
if !strings.Contains(result.stdout, "2026-02-02") {
t.Fatalf("%q: rows missing from stdout: %q", mode, result.stdout)
}
if strings.Contains(result.stdout, "250000") || strings.Contains(result.stdout, "not available") {
t.Fatalf("%q: notes must stay off stdout: %q", mode, result.stdout)
}
if !strings.Contains(result.stderr, "2 of 250000") {
t.Fatalf("%q: want truncation note on stderr, got %q", mode, result.stderr)
}
if !strings.Contains(result.stderr, "Some requested metrics are not available") {
t.Fatalf("%q: want API warning on stderr, got %q", mode, result.stderr)
}
}
}

// An empty report can still carry API warnings, which explain why no rows came back.
func TestExecute_AdSenseReportsQuery_EmptyTableStillShowsWarnings(t *testing.T) {
svc := newAdSenseTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"headers": []map[string]any{{"name": "ESTIMATED_EARNINGS", "type": "METRIC_CURRENCY"}},
"rows": []any{},
"warnings": []string{"Data for this date range is not available yet."},
})
}))
for _, mode := range []string{"--plain", ""} {
args := []string{"--account", "a@b.com", "adsense", "reports", "query", "pub-123", "--metrics", "estimated_earnings"}
if mode != "" {
args = append([]string{mode}, args...)
}
result := executeWithAdSenseTestService(t, args, svc)
if result.err != nil {
t.Fatalf("%q: Execute: %v", mode, result.err)
}
if !strings.Contains(result.stderr, "Data for this date range is not available yet.") || !strings.Contains(result.stderr, "No AdSense report rows") {
t.Fatalf("%q: want the warning and the empty-report note on stderr, got %q", mode, result.stderr)
}
}
}

// Saved reports have no --max flag, so their truncation note must not suggest one.
func TestExecute_AdSenseSavedReportTruncationHintHasNoMaxFlag(t *testing.T) {
svc := newAdSenseTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.URL.Path, "/saved:generate") {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"headers": []map[string]any{{"name": "DATE", "type": "DIMENSION"}},
"rows": []map[string]any{{"cells": []map[string]any{{"value": "2026-02-01"}}}},
"totalMatchedRows": "250000",
})
}))
result := executeWithAdSenseTestService(t, []string{"--plain", "--account", "a@b.com", "adsense", "reports", "saved", "query", "accounts/pub-123/reports/report-1"}, svc)
if result.err != nil {
t.Fatalf("Execute: %v", result.err)
}
if !strings.Contains(result.stderr, "1 of 250000") || strings.Contains(result.stderr, "--max") {
t.Fatalf("want a truncation note without --max, got %q", result.stderr)
}
}
16 changes: 14 additions & 2 deletions internal/cmd/adsense_reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (c *AdSenseReportsQueryCmd) Run(ctx context.Context, flags *RootFlags) erro
return err
}

return writeAdSenseReportResult(ctx, u, plan.Account, plan.Dimensions, plan.Metrics, resp, c.FailEmpty)
return writeAdSenseReportResult(ctx, u, plan.Account, plan.Dimensions, plan.Metrics, resp, c.FailEmpty, "raise --max (up to 100000) or narrow the report")
}

func applyAdSenseReportDateRange(call *adsenseapi.AccountsReportsGenerateCall, plan adSenseReportPlan) *adsenseapi.AccountsReportsGenerateCall {
Expand All @@ -112,6 +112,7 @@ func writeAdSenseReportResult(
metrics []string,
resp *adsenseapi.ReportResult,
failEmpty bool,
moreRowsHint string,
) error {
if outfmt.IsJSON(ctx) {
if err := outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{
Expand All @@ -133,6 +134,17 @@ func writeAdSenseReportResult(
return nil
}

// JSON carries these fields; keep them visible here too, on stderr so
// stdout stays parseable.
for _, warning := range resp.Warnings {
if warning = strings.TrimSpace(warning); warning != "" {
u.Err().Linef("# Warning: %s", warning)
}
}
if resp.TotalMatchedRows > int64(len(resp.Rows)) {
u.Err().Linef("# Showing %d of %d matched rows; %s to see more", len(resp.Rows), resp.TotalMatchedRows, moreRowsHint)
}

if len(resp.Rows) == 0 {
u.Err().Println("No AdSense report rows")
return failEmptyExit(failEmpty)
Expand Down Expand Up @@ -307,5 +319,5 @@ func (c *AdSenseReportsSavedQueryCmd) Run(ctx context.Context, flags *RootFlags)
return err
}

return writeAdSenseReportResult(ctx, u, name, nil, nil, resp, c.FailEmpty)
return writeAdSenseReportResult(ctx, u, name, nil, nil, resp, c.FailEmpty, "narrow the saved report, for example its date range")
}