diff --git a/docs/assets/qa/github-sync-count/after.png b/docs/assets/qa/github-sync-count/after.png new file mode 100644 index 00000000..b5e10ee0 Binary files /dev/null and b/docs/assets/qa/github-sync-count/after.png differ diff --git a/docs/assets/qa/github-sync-count/before.png b/docs/assets/qa/github-sync-count/before.png new file mode 100644 index 00000000..d703cd6b Binary files /dev/null and b/docs/assets/qa/github-sync-count/before.png differ diff --git a/internal/tracker/github_test.go b/internal/tracker/github_test.go index 1fe8b888..55d01eef 100644 --- a/internal/tracker/github_test.go +++ b/internal/tracker/github_test.go @@ -177,3 +177,51 @@ func TestGitHubMergeBranchShortCircuitsMergedPullAndReopensClosed(t *testing.T) t.Fatalf("closed unmerged pull must be re-opened then merged: %v", writes) } } + +func TestGitHubPullAllLogsProcessedIssues(t *testing.T) { + for _, tc := range []struct { + name string + pageSize int + wantCount int + wantStatus string + }{ + {"empty repository", 0, 0, "success"}, + {"issues and pull requests", 2, 1, "success"}, + {"error after the first page", 100, 1, "failed"}, + } { + t.Run(tc.name, func(t *testing.T) { + store := &memIssueStore{ + issues: map[string]domain.Issue{}, + repositories: map[string]domain.Repository{ + "repo": {ID: "repo", Provider: "github", ProjectID: "orders", GongfengProjectID: "github:example/orders"}, + }, + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query().Get("page") == "2" { + http.Error(w, "temporary error", http.StatusServiceUnavailable) + return + } + page := make([]map[string]any, tc.pageSize) + for i := range page { + page[i] = map[string]any{"id": i + 1, "number": i + 1, "title": "Example", "state": "open"} + if i > 0 { + page[i]["pull_request"] = map[string]any{} + } + } + _ = json.NewEncoder(w).Encode(page) + })) + t.Cleanup(server.Close) + s := NewService() + s.githubAPIBase = server.URL + err := s.PullAll(store) + if (err != nil) != (tc.wantStatus == "failed") { + t.Fatalf("unexpected sync result: %v", err) + } + logs := s.Logs() + if len(logs) != 1 || logs[0].Status != tc.wantStatus || logs[0].Count != tc.wantCount || + logs[0].ProjectID != "orders" || len(store.issues) != tc.wantCount { + t.Fatalf("stored %d issues, logs = %+v", len(store.issues), logs) + } + }) + } +} diff --git a/internal/tracker/service.go b/internal/tracker/service.go index 18e43a37..d2386802 100644 --- a/internal/tracker/service.go +++ b/internal/tracker/service.go @@ -311,7 +311,11 @@ func (s *Service) PullAll(store IssueStoreAdapter) error { status = "failed" message = err.Error() } - s.addLog(domain.SyncLog{ID: fmt.Sprintf("sync-%d", started.UnixNano()), Direction: "pull", Source: "github", Action: "sync_issues", Status: status, Message: message, ProjectID: repo.ProjectID, StartedAt: started, FinishedAt: time.Now().UTC()}) + s.addLog(domain.SyncLog{ + ID: fmt.Sprintf("sync-%d", started.UnixNano()), Direction: "pull", Source: "github", + Action: "sync_issues", Status: status, Count: n, Message: message, + ProjectID: repo.ProjectID, StartedAt: started, FinishedAt: time.Now().UTC(), + }) syncCount += n if err != nil { lastErr = err