Files
kandev/apps/backend/internal/github/mock_client.go
T
João Salavisa 85aa3ac0aa feat(github): add /github page for PRs and issues (#687)
* 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>
2026-04-24 10:38:49 +01:00

372 lines
10 KiB
Go

package github
import (
"context"
"fmt"
"strings"
"sync"
"time"
)
const mockDefaultUser = "mock-user"
// prKey is a composite key for PR lookups by owner/repo/number.
type prKey struct {
Owner string
Repo string
Number int
}
// branchKey is a composite key for PR lookups by owner/repo/branch.
type branchKey struct {
Owner string
Repo string
Branch string
}
// checkKey is a composite key for check-run lookups by owner/repo/ref.
type checkKey struct {
Owner string
Repo string
Ref string
}
// submittedReview records a SubmitReview call for test assertions.
type submittedReview struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
Number int `json:"number"`
Event string `json:"event"`
Body string `json:"body"`
}
// repoKey is a composite key for per-repo lookups by owner/repo.
type repoKey struct {
Owner string
Repo string
}
// MockClient implements Client with in-memory configurable data for E2E testing.
// All data is protected by a sync.RWMutex for thread safety.
type MockClient struct {
mu sync.RWMutex
user string
prs map[prKey]*PR
prsByBranch map[branchKey]*PR
orgs []GitHubOrg
repos map[string][]GitHubRepo
branches map[repoKey][]RepoBranch
reviews map[prKey][]PRReview
comments map[prKey][]PRComment
checks map[checkKey][]CheckRun
files map[prKey][]PRFile
commits map[prKey][]PRCommitInfo
submittedReviews []submittedReview
}
// NewMockClient creates a new MockClient with default values.
func NewMockClient() *MockClient {
return &MockClient{
user: mockDefaultUser,
prs: make(map[prKey]*PR),
prsByBranch: make(map[branchKey]*PR),
repos: make(map[string][]GitHubRepo),
branches: make(map[repoKey][]RepoBranch),
reviews: make(map[prKey][]PRReview),
comments: make(map[prKey][]PRComment),
checks: make(map[checkKey][]CheckRun),
files: make(map[prKey][]PRFile),
commits: make(map[prKey][]PRCommitInfo),
}
}
// --- Client interface implementation ---
func (m *MockClient) IsAuthenticated(context.Context) (bool, error) {
return true, nil
}
func (m *MockClient) GetAuthenticatedUser(context.Context) (string, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.user, nil
}
func (m *MockClient) GetPR(_ context.Context, owner, repo string, number int) (*PR, error) {
m.mu.RLock()
defer m.mu.RUnlock()
pr, ok := m.prs[prKey{owner, repo, number}]
if !ok {
return nil, fmt.Errorf("mock: PR %s/%s#%d not found", owner, repo, number)
}
return pr, nil
}
func (m *MockClient) FindPRByBranch(_ context.Context, owner, repo, branch string) (*PR, error) {
m.mu.RLock()
defer m.mu.RUnlock()
pr := m.prsByBranch[branchKey{owner, repo, branch}]
return pr, nil
}
func (m *MockClient) ListAuthoredPRs(_ context.Context, owner, repo string) ([]*PR, error) {
m.mu.RLock()
defer m.mu.RUnlock()
var result []*PR
for k, pr := range m.prs {
if k.Owner == owner && k.Repo == repo && pr.AuthorLogin == m.user {
result = append(result, pr)
}
}
return result, nil
}
func (m *MockClient) ListReviewRequestedPRs(context.Context, string, string, string) ([]*PR, error) {
m.mu.RLock()
defer m.mu.RUnlock()
var result []*PR
for _, pr := range m.prs {
if len(pr.RequestedReviewers) > 0 {
result = append(result, pr)
}
}
return result, nil
}
func (m *MockClient) ListIssues(context.Context, string, string) ([]*Issue, error) {
return nil, nil
}
func (m *MockClient) ListIssuesPaged(context.Context, string, string, int, int) (*IssueSearchPage, error) {
return &IssueSearchPage{Issues: []*Issue{}, TotalCount: 0, Page: 1, PerPage: 50}, nil
}
func (m *MockClient) SearchPRs(context.Context, string, string) ([]*PR, error) {
m.mu.RLock()
defer m.mu.RUnlock()
result := make([]*PR, 0, len(m.prs))
for _, pr := range m.prs {
result = append(result, pr)
}
return result, nil
}
func (m *MockClient) SearchPRsPaged(ctx context.Context, _, _ string, page, perPage int) (*PRSearchPage, error) {
prs, err := m.SearchPRs(ctx, "", "")
if err != nil {
return nil, err
}
return &PRSearchPage{PRs: prs, TotalCount: len(prs), Page: page, PerPage: perPage}, nil
}
func (m *MockClient) GetIssueState(context.Context, string, string, int) (string, error) {
return defaultPRState, nil
}
func (m *MockClient) ListUserOrgs(context.Context) ([]GitHubOrg, error) {
m.mu.RLock()
defer m.mu.RUnlock()
if m.orgs == nil {
return []GitHubOrg{}, nil
}
return m.orgs, nil
}
func (m *MockClient) SearchOrgRepos(_ context.Context, org, query string, _ int) ([]GitHubRepo, error) {
m.mu.RLock()
defer m.mu.RUnlock()
repos := m.repos[org]
if query == "" {
return repos, nil
}
var filtered []GitHubRepo
for _, r := range repos {
if strings.Contains(strings.ToLower(r.FullName), strings.ToLower(query)) {
filtered = append(filtered, r)
}
}
return filtered, nil
}
func (m *MockClient) ListPRReviews(_ context.Context, owner, repo string, number int) ([]PRReview, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.reviews[prKey{owner, repo, number}], nil
}
func (m *MockClient) ListPRComments(_ context.Context, owner, repo string, number int, since *time.Time) ([]PRComment, error) {
m.mu.RLock()
defer m.mu.RUnlock()
all := m.comments[prKey{owner, repo, number}]
if since == nil {
return all, nil
}
var filtered []PRComment
for _, c := range all {
if c.UpdatedAt.After(*since) {
filtered = append(filtered, c)
}
}
return filtered, nil
}
func (m *MockClient) ListCheckRuns(_ context.Context, owner, repo, ref string) ([]CheckRun, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.checks[checkKey{owner, repo, ref}], nil
}
func (m *MockClient) GetPRFeedback(ctx context.Context, owner, repo string, number int) (*PRFeedback, error) {
return getPRFeedback(ctx, m, owner, repo, number)
}
func (m *MockClient) GetPRStatus(ctx context.Context, owner, repo string, number int) (*PRStatus, error) {
return getPRStatus(ctx, m, owner, repo, number)
}
func (m *MockClient) ListPRFiles(_ context.Context, owner, repo string, number int) ([]PRFile, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.files[prKey{owner, repo, number}], nil
}
func (m *MockClient) ListPRCommits(_ context.Context, owner, repo string, number int) ([]PRCommitInfo, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.commits[prKey{owner, repo, number}], nil
}
func (m *MockClient) ListRepoBranches(_ context.Context, owner, repo string) ([]RepoBranch, error) {
m.mu.RLock()
defer m.mu.RUnlock()
branches, ok := m.branches[repoKey{owner, repo}]
if !ok {
return nil, &GitHubAPIError{
StatusCode: 404,
Endpoint: fmt.Sprintf("/repos/%s/%s/branches", owner, repo),
Body: fmt.Sprintf("repository %s/%s not found", owner, repo),
}
}
out := make([]RepoBranch, len(branches))
copy(out, branches)
return out, nil
}
func (m *MockClient) SubmitReview(_ context.Context, owner, repo string, number int, event, body string) error {
m.mu.Lock()
defer m.mu.Unlock()
m.submittedReviews = append(m.submittedReviews, submittedReview{
Owner: owner, Repo: repo, Number: number, Event: event, Body: body,
})
return nil
}
// --- Setter methods for HTTP control endpoints ---
// SetUser sets the authenticated username.
func (m *MockClient) SetUser(username string) {
m.mu.Lock()
defer m.mu.Unlock()
m.user = username
}
// AddPR adds a PR to the mock data store, indexed by owner/repo/number and branch.
func (m *MockClient) AddPR(pr *PR) {
m.mu.Lock()
defer m.mu.Unlock()
m.prs[prKey{pr.RepoOwner, pr.RepoName, pr.Number}] = pr
if pr.HeadBranch != "" {
m.prsByBranch[branchKey{pr.RepoOwner, pr.RepoName, pr.HeadBranch}] = pr
}
}
// AddOrgs appends organizations to the mock data store.
func (m *MockClient) AddOrgs(orgs []GitHubOrg) {
m.mu.Lock()
defer m.mu.Unlock()
m.orgs = append(m.orgs, orgs...)
}
// AddBranches sets branches for a repository.
func (m *MockClient) AddBranches(owner, repo string, branches []RepoBranch) {
m.mu.Lock()
defer m.mu.Unlock()
cp := make([]RepoBranch, len(branches))
copy(cp, branches)
m.branches[repoKey{owner, repo}] = cp
}
// AddRepos appends repositories for an organization.
func (m *MockClient) AddRepos(org string, repos []GitHubRepo) {
m.mu.Lock()
defer m.mu.Unlock()
m.repos[org] = append(m.repos[org], repos...)
}
// AddReviews appends reviews for a PR.
func (m *MockClient) AddReviews(owner, repo string, number int, reviews []PRReview) {
m.mu.Lock()
defer m.mu.Unlock()
k := prKey{owner, repo, number}
m.reviews[k] = append(m.reviews[k], reviews...)
}
// AddComments appends comments for a PR.
func (m *MockClient) AddComments(owner, repo string, number int, comments []PRComment) {
m.mu.Lock()
defer m.mu.Unlock()
k := prKey{owner, repo, number}
m.comments[k] = append(m.comments[k], comments...)
}
// AddCheckRuns appends check runs for a ref.
func (m *MockClient) AddCheckRuns(owner, repo, ref string, checks []CheckRun) {
m.mu.Lock()
defer m.mu.Unlock()
k := checkKey{owner, repo, ref}
m.checks[k] = append(m.checks[k], checks...)
}
// AddPRFiles appends files for a PR.
func (m *MockClient) AddPRFiles(owner, repo string, number int, files []PRFile) {
m.mu.Lock()
defer m.mu.Unlock()
k := prKey{owner, repo, number}
m.files[k] = append(m.files[k], files...)
}
// AddPRCommits appends commits for a PR.
func (m *MockClient) AddPRCommits(owner, repo string, number int, commits []PRCommitInfo) {
m.mu.Lock()
defer m.mu.Unlock()
k := prKey{owner, repo, number}
m.commits[k] = append(m.commits[k], commits...)
}
// Reset clears all mock data and resets the user to the default.
func (m *MockClient) Reset() {
m.mu.Lock()
defer m.mu.Unlock()
m.user = mockDefaultUser
m.prs = make(map[prKey]*PR)
m.prsByBranch = make(map[branchKey]*PR)
m.orgs = nil
m.repos = make(map[string][]GitHubRepo)
m.branches = make(map[repoKey][]RepoBranch)
m.reviews = make(map[prKey][]PRReview)
m.comments = make(map[prKey][]PRComment)
m.checks = make(map[checkKey][]CheckRun)
m.files = make(map[prKey][]PRFile)
m.commits = make(map[prKey][]PRCommitInfo)
m.submittedReviews = nil
}
// SubmittedReviews returns all recorded SubmitReview calls.
func (m *MockClient) SubmittedReviews() []submittedReview {
m.mu.RLock()
defer m.mu.RUnlock()
result := make([]submittedReview, len(m.submittedReviews))
copy(result, m.submittedReviews)
return result
}