85aa3ac0aa
* feat(github): add /github page for PRs and issues Dedicated page under /github lists pull requests and issues across the authenticated user's repos with preset filters, custom GitHub search queries, per-repo narrowing, saved queries, and one-click task launching for any PR or issue. Performance: - Cache user PR/issue search responses for 30s with singleflight to coalesce concurrent misses, so preset switching and pagination avoid upstream round trips. - Replace the per-row PR status fetch (N+1) with a batch endpoint POST /api/v1/github/prs/statuses that fans out concurrently on the backend with a semaphore and caches per-PR. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * style: apply Prettier formatting Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: address review feedback on PR cache + status batching - Harden searchCacheKey against separator collisions from user-controllable customQuery (length-prefix encoding). - Drop repositories.loaded hydration when inner workspace fetch fails so the client refetches instead of trusting a partial snapshot. - Guard useGitHubSearch with a monotonic request counter to drop stale responses when filter+page changes overlap. - Fix usePRStatuses teardown: depend only on composed key so a new array identity per render doesn't cancel the in-flight batch. - Correct godocs (duplicate, outdated state:open claim). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: address additional review feedback - Restore state:open for issue watcher: buildIssueSearchQuery no longer injects it as a default, so existing watches were starting to surface closed issues. Inject state:open in buildIssueFilter so only the watcher path is affected; the /github page presets already set their own state qualifiers. - countCheckResults now excludes in-progress checks from the denominator so a `1/3` badge reflects pass rate on completed checks, not a mix of running and failed. The pending state is still carried via ChecksState. - usePRStatuses retries after a transient fetch error (only mark the key as completed once the fetch resolves). This also fixes missing badges under React Strict Mode's intentional double-mount, since the first effect's cleanup fires before the response lands and the guard stays unset for the second mount to retry. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * test(github): add tests for my-github hooks Covers use-committed-query, buildParams from use-github-search, use-saved-presets readStorage validation, use-known-repos recordForKey accumulator semantics, and use-pr-statuses batch fetch lifecycle. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: address review feedback on search cache and repo accumulator - Clamp perPage before composing the search cache key so identical pages (e.g. perPage=150 and =100) share a cache entry instead of creating duplicates keyed on the raw value. - Add resetKnownReposStore and call it on /github unmount so the module-level repo accumulator doesn't carry across navigations. - Rename use-pr-statuses test to match its assertion (it verifies stale statuses are cleared when the key changes and fetch fails). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * style: apply Prettier formatting Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(web): distinguish blocked from dirty in PR mergeable badge GitHub's blocked state (required reviews/checks/branch protection) is not a merge conflict, so don't label it "Conflicts". Surface it as "Blocked" with the pending tone; keep "Conflicts" for dirty. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix: address remaining /github code review comments - Route PR status + batch through handleSearchError so ErrNoClient surfaces as 503 instead of 500 (matches the search endpoints). - Sort workflow steps by position in the quick-task-launcher default pick, matching the convention in issue-watch-dialog. - Make PresetItem keyboard-accessible: role=button, tabIndex, and Enter/Space handling on the wrapping div (it contains a nested delete button, so it can't itself be a button). - Extract useRepoOptions out of useGitHubPageState to bring the hook back under the 100-line complexity limit. - Honor ctx.Done() when acquiring the PR-status batch semaphore so a cancelled HTTP request releases queued goroutines instead of running each upstream fetch to completion. - Clarify parsePaginationQuery doc: the upper bound (100) is enforced by clampSearchPage in the client layer, not here. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(web): address /github review follow-ups - Merge duplicate @/lib/utils imports in pr-list. - Use rel="noopener noreferrer" on external PR/issue links (codebase convention) and add cursor-pointer per the UI guidelines. - Rewrite onOpenSaveDialog as an explicit if-block so the callback returns void rather than boolean | void. - Use PaginationEllipsis in its own PaginationItem so the ellipsis and the adjacent page link are separate list items instead of visually fused into a single cell. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor(web): derive QuickTaskLauncher dialog state from props Replaces the useEffect+setState anti-pattern with useMemo-derived state driven directly by the payload prop. Drops the eslint suppression that was needed for react-hooks/set-state-in-effect. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
162 lines
3.8 KiB
Go
162 lines
3.8 KiB
Go
package github
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
)
|
|
|
|
func TestTTLCache_HitAndMiss(t *testing.T) {
|
|
cache := newTTLCache()
|
|
var calls int
|
|
fetch := func() (any, error) {
|
|
calls++
|
|
return "result", nil
|
|
}
|
|
for i := 0; i < 3; i++ {
|
|
v, err := cache.doOrFetch("k", fetch)
|
|
if err != nil {
|
|
t.Fatalf("unexpected err: %v", err)
|
|
}
|
|
if v != "result" {
|
|
t.Fatalf("unexpected value: %v", v)
|
|
}
|
|
}
|
|
if calls != 1 {
|
|
t.Fatalf("expected fetch called once, got %d", calls)
|
|
}
|
|
}
|
|
|
|
func TestTTLCache_Expiry(t *testing.T) {
|
|
synctest.Test(t, func(t *testing.T) {
|
|
cache := newTTLCache()
|
|
cache.ttl = 30 * time.Second
|
|
var calls int
|
|
fetch := func() (any, error) {
|
|
calls++
|
|
return calls, nil
|
|
}
|
|
if _, err := cache.doOrFetch("k", fetch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
time.Sleep(29 * time.Second)
|
|
if _, err := cache.doOrFetch("k", fetch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls != 1 {
|
|
t.Fatalf("expected cache hit before TTL; calls=%d", calls)
|
|
}
|
|
time.Sleep(2 * time.Second)
|
|
v, err := cache.doOrFetch("k", fetch)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if calls != 2 {
|
|
t.Fatalf("expected refetch after TTL; calls=%d", calls)
|
|
}
|
|
if v != 2 {
|
|
t.Fatalf("expected fresh value 2, got %v", v)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestTTLCache_ErrorNotCached(t *testing.T) {
|
|
cache := newTTLCache()
|
|
var calls int
|
|
sentinel := errors.New("boom")
|
|
fetch := func() (any, error) {
|
|
calls++
|
|
if calls == 1 {
|
|
return nil, sentinel
|
|
}
|
|
return "ok", nil
|
|
}
|
|
if _, err := cache.doOrFetch("k", fetch); !errors.Is(err, sentinel) {
|
|
t.Fatalf("expected sentinel error, got %v", err)
|
|
}
|
|
v, err := cache.doOrFetch("k", fetch)
|
|
if err != nil {
|
|
t.Fatalf("unexpected err: %v", err)
|
|
}
|
|
if v != "ok" {
|
|
t.Fatalf("unexpected value: %v", v)
|
|
}
|
|
if calls != 2 {
|
|
t.Fatalf("expected 2 fetch calls (error not cached), got %d", calls)
|
|
}
|
|
}
|
|
|
|
func TestSearchCacheKey_KeyIsolation(t *testing.T) {
|
|
if searchCacheKey("pr", "", "is:open", 1, 25) == searchCacheKey("issue", "", "is:open", 1, 25) {
|
|
t.Fatal("kind should be part of key")
|
|
}
|
|
if searchCacheKey("pr", "", "is:open", 1, 25) == searchCacheKey("pr", "", "is:open", 2, 25) {
|
|
t.Fatal("page should be part of key")
|
|
}
|
|
if searchCacheKey("pr", "", "is:open", 1, 25) == searchCacheKey("pr", "", "is:closed", 1, 25) {
|
|
t.Fatal("customQuery should be part of key")
|
|
}
|
|
// A user-controllable customQuery must not be able to collide with
|
|
// adjacent fields by embedding the separator.
|
|
collisions := [][2][5]any{
|
|
{{"pr", "a", "b", 1, 25}, {"pr", "a|b", "", 1, 25}},
|
|
{{"pr", "", "a|b|1|25", 0, 0}, {"pr", "a", "b", 1, 25}},
|
|
}
|
|
for _, c := range collisions {
|
|
a := searchCacheKey(c[0][0].(string), c[0][1].(string), c[0][2].(string), c[0][3].(int), c[0][4].(int))
|
|
b := searchCacheKey(c[1][0].(string), c[1][1].(string), c[1][2].(string), c[1][3].(int), c[1][4].(int))
|
|
if a == b {
|
|
t.Fatalf("separator collision: %q == %q", a, b)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTTLCache_Singleflight(t *testing.T) {
|
|
cache := newTTLCache()
|
|
var calls atomic.Int32
|
|
release := make(chan struct{})
|
|
started := make(chan struct{}, 1)
|
|
fetch := func() (any, error) {
|
|
calls.Add(1)
|
|
select {
|
|
case started <- struct{}{}:
|
|
default:
|
|
}
|
|
<-release
|
|
return "v", nil
|
|
}
|
|
|
|
const n = 8
|
|
var wg sync.WaitGroup
|
|
wg.Add(n)
|
|
for i := 0; i < n; i++ {
|
|
go func() {
|
|
defer wg.Done()
|
|
if _, err := cache.doOrFetch("k", fetch); err != nil {
|
|
t.Errorf("fetch err: %v", err)
|
|
}
|
|
}()
|
|
}
|
|
<-started
|
|
close(release)
|
|
wg.Wait()
|
|
if got := calls.Load(); got != 1 {
|
|
t.Fatalf("expected singleflight to coalesce; calls=%d", got)
|
|
}
|
|
}
|
|
|
|
func TestTTLCache_MaxSizeEviction(t *testing.T) {
|
|
cache := newTTLCache()
|
|
cache.maxSize = 3
|
|
for i := 0; i < 5; i++ {
|
|
cache.set(fmt.Sprintf("k%d", i), i)
|
|
}
|
|
if len(cache.entries) > cache.maxSize {
|
|
t.Fatalf("cache exceeded maxSize: %d", len(cache.entries))
|
|
}
|
|
}
|