beads/internal/github/tracker.go
Kevin Glynn 9827c66319
fix(github): content-based dedup on push to stop re-PATCHing every issue (#4214) (#4329)
* fix(github): content-based dedup on push to stop re-PATCHing every issue

GitHub sync never wired engine.PushHooks, so `doPush` fell back to
timestamp-only dedup. Because linking and any local write bump a beads
issue's updated_at past GitHub's second-granularity updated_at, every
`github sync --push-only` / `github push` reconciled and PATCHed the
entire backlog on every run — O(N) REST calls that drain the shared
installation's primary rate limit even when nothing changed.

Add github.PushFieldsEqual, comparing only the fields a push mutates
(title, body, state, and the label set BeadsIssueToGitHubFields sends,
order-insensitive). Wire a ContentEqual push hook (buildGitHubPushHooks)
into both runGitHubSync and runGitHubPush so the engine skips issues
already in sync — mirroring the existing Linear/Jira push hooks.

Dry-run still makes 0 REST calls (unchanged); this targets the live
re-PATCH storm. A push-side incremental cursor to also avoid the
per-issue fetch is a noted follow-up.

Fixes #4214

Co-authored-by: Cursor <cursoragent@cursor.com>

* perf(sync): skip per-issue fetch on no-op push via persisted content hash

The ContentEqual hook added for #4214 stops the redundant PATCH but still
fetches every linked issue (one GET each), so a no-op `github sync --push-only`
still drains the REST rate limit. Add a ContentHash push hook: the engine
persists each issue's pushed content hash in the dolt-ignored local_metadata
table and consults it before FetchIssue. When the local content is unchanged
since the last push, the fetch and update are skipped entirely.

Also record the hash when ContentEqual reports a match (so the first
post-restart pass repopulates hashes) and consult it in dry-run, so an
unchanged issue previews as a skip instead of always reporting "Would update".

Refs gastownhall/beads#4214

Co-authored-by: Cursor <cursoragent@cursor.com>

* shrink(github): simplify PushContentHash framing and labelSetsEqual

Replace length-prefix binary framing in PushContentHash with NUL-separated
fields — the hash is only self-compared, so the simpler scheme is sufficient.
Replace hand-rolled counting map in labelSetsEqual with slices.Sort +
slices.Equal. Drop unused encoding/binary, strconv, sort imports in favor
of slices.

Agent-Signature: cursor-opus-4.6-unknown-reasoning on behalf of kevglynn
Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(sync): bind push cache to remote target

Agent-Signature: codex-gpt-5.6-sol-medium on behalf of matt wilkie

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-08-02 22:53:06 -07:00

300 lines
8 KiB
Go

package github
import (
"context"
"fmt"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/tracker"
"github.com/steveyegge/beads/internal/types"
)
func init() {
tracker.Register("github", func() tracker.IssueTracker {
return &Tracker{}
})
}
// issueNumberPattern matches GitHub issue URLs: .../issues/42
var issueNumberPattern = regexp.MustCompile(`/issues/(\d+)`)
// ghShorthandPattern matches the "github:{digits}" shorthand produced by BuildExternalRef
// when a full URL is unavailable.
var ghShorthandPattern = regexp.MustCompile(`^github:([1-9]\d*)$`)
// Tracker implements tracker.IssueTracker for GitHub.
type Tracker struct {
client *Client
config *MappingConfig
store storage.Storage
}
func (t *Tracker) Name() string { return "github" }
func (t *Tracker) DisplayName() string { return "GitHub" }
func (t *Tracker) ConfigPrefix() string { return "github" }
func (t *Tracker) Init(ctx context.Context, store storage.Storage) error {
t.store = store
token := t.getConfig(ctx, "github.token", "GITHUB_TOKEN")
if token == "" {
return fmt.Errorf("GitHub token not configured (set github.token or GITHUB_TOKEN)")
}
owner := t.getConfig(ctx, "github.owner", "GITHUB_OWNER")
repo := t.getConfig(ctx, "github.repo", "GITHUB_REPO")
// Try combined owner/repo format: "owner/repo"
if owner == "" || repo == "" {
ownerRepo := t.getConfig(ctx, "github.repository", "GITHUB_REPOSITORY")
if ownerRepo != "" {
parts := strings.SplitN(ownerRepo, "/", 2)
if len(parts) == 2 {
owner = parts[0]
repo = parts[1]
}
}
}
if owner == "" {
return fmt.Errorf("GitHub owner not configured (set github.owner or GITHUB_OWNER)")
}
if repo == "" {
return fmt.Errorf("GitHub repo not configured (set github.repo or GITHUB_REPO)")
}
t.client = NewClient(token, owner, repo)
// Allow custom base URL for GitHub Enterprise
baseURL := t.getConfig(ctx, "github.url", "GITHUB_API_URL")
if baseURL != "" {
t.client = t.client.WithBaseURL(baseURL)
}
t.config = DefaultMappingConfig()
return nil
}
func (t *Tracker) Validate() error {
if t.client == nil {
return fmt.Errorf("GitHub tracker not initialized")
}
return nil
}
func (t *Tracker) Close() error { return nil }
func (t *Tracker) FetchIssues(ctx context.Context, opts tracker.FetchOptions) ([]tracker.TrackerIssue, error) {
var issues []Issue
var err error
state := opts.State
if state == "" {
state = "all"
}
if opts.Since != nil {
issues, err = t.client.FetchIssuesSince(ctx, state, *opts.Since)
} else {
issues, err = t.client.FetchIssues(ctx, state)
}
if err != nil {
return nil, err
}
result := make([]tracker.TrackerIssue, 0, len(issues))
for _, gh := range issues {
result = append(result, githubToTrackerIssue(&gh))
}
return result, nil
}
func (t *Tracker) FetchIssue(ctx context.Context, identifier string) (*tracker.TrackerIssue, error) {
number, err := strconv.Atoi(identifier)
if err != nil {
return nil, fmt.Errorf("invalid GitHub issue number %q: %w", identifier, err)
}
gh, err := t.client.FetchIssueByNumber(ctx, number)
if err != nil {
return nil, err
}
if gh == nil {
return nil, nil
}
ti := githubToTrackerIssue(gh)
return &ti, nil
}
func (t *Tracker) CreateIssue(ctx context.Context, issue *types.Issue) (*tracker.TrackerIssue, error) {
fields := BeadsIssueToGitHubFields(issue, t.config)
labels, _ := fields["labels"].([]string)
created, err := t.client.CreateIssue(ctx, issue.Title, issue.Description, labels)
if err != nil {
return nil, err
}
ti := githubToTrackerIssue(created)
return &ti, nil
}
func (t *Tracker) UpdateIssue(ctx context.Context, externalID string, issue *types.Issue) (*tracker.TrackerIssue, error) {
number, err := strconv.Atoi(externalID)
if err != nil {
return nil, fmt.Errorf("invalid GitHub issue number %q: %w", externalID, err)
}
updates := BeadsIssueToGitHubFields(issue, t.config)
updated, err := t.client.UpdateIssue(ctx, number, updates)
if err != nil {
return nil, err
}
ti := githubToTrackerIssue(updated)
return &ti, nil
}
func (t *Tracker) FieldMapper() tracker.FieldMapper {
return &githubFieldMapper{config: t.config}
}
// MappingConfig exposes the tracker's field-mapping configuration so callers
// (e.g. push-hook content comparison) can mirror push field semantics.
func (t *Tracker) MappingConfig() *MappingConfig {
return t.config
}
// PushTargetScope returns the canonical repository endpoint under which issue
// identifiers are resolved. It lets the sync engine distinguish repo-less refs
// such as github:42 when the configured host, owner, or repository changes.
func (t *Tracker) PushTargetScope() string {
if t.client == nil {
return ""
}
return fmt.Sprintf("%s/repos/%s/%s",
canonicalGitHubBaseURL(t.client.BaseURL),
strings.ToLower(strings.TrimSpace(t.client.Owner)),
strings.ToLower(strings.TrimSpace(t.client.Repo)),
)
}
// canonicalGitHubBaseURL normalizes the URL components that are
// case-insensitive while preserving case-sensitive path semantics. Malformed
// and relative custom values fall back to deterministic whitespace/slash
// trimming instead of being rejected here (Tracker.Init/client validation owns
// their usability).
func canonicalGitHubBaseURL(raw string) string {
trimmed := strings.TrimRight(strings.TrimSpace(raw), "/")
u, err := url.Parse(trimmed)
if err != nil || u.Scheme == "" || u.Host == "" {
return trimmed
}
u.Scheme = strings.ToLower(u.Scheme)
u.Host = strings.ToLower(u.Host)
u.Path = strings.TrimRight(u.Path, "/")
if u.RawPath != "" {
u.RawPath = strings.TrimRight(u.RawPath, "/")
}
return u.String()
}
// IsExternalRef checks if a ref belongs to this GitHub tracker.
// It recognizes both full GitHub URLs and the "github:{id}" shorthand format
// produced by BuildExternalRef when a URL is unavailable.
func (t *Tracker) IsExternalRef(ref string) bool {
if ghShorthandPattern.MatchString(ref) {
return true
}
return strings.Contains(ref, "github.com") && issueNumberPattern.MatchString(ref)
}
// ExtractIdentifier extracts the issue number from a GitHub URL or shorthand ref.
func (t *Tracker) ExtractIdentifier(ref string) string {
if m := ghShorthandPattern.FindStringSubmatch(ref); len(m) >= 2 {
return m[1]
}
matches := issueNumberPattern.FindStringSubmatch(ref)
if len(matches) < 2 {
return ""
}
return matches[1]
}
func (t *Tracker) BuildExternalRef(issue *tracker.TrackerIssue) string {
if issue.URL != "" {
return issue.URL
}
return fmt.Sprintf("github:%s", issue.Identifier)
}
// getConfig reads a config value from storage, falling back to env var.
// For yaml-only keys (e.g. github.token), reads from config.yaml first
// to match the behavior of cmd/bd/github.go:getGitHubConfigValue().
func (t *Tracker) getConfig(ctx context.Context, key, envVar string) string {
// Secret keys are stored in config.yaml, not the Dolt database,
// to avoid leaking secrets when pushing to remotes.
if config.IsYamlOnlyKey(key) {
if val := config.GetString(key); val != "" {
return val
}
if envVar != "" {
if envVal := os.Getenv(envVar); envVal != "" {
return envVal
}
}
return ""
}
val, err := t.store.GetConfig(ctx, key)
if err == nil && val != "" {
return val
}
if envVar != "" {
if envVal := os.Getenv(envVar); envVal != "" {
return envVal
}
}
return ""
}
// githubToTrackerIssue converts a github.Issue to a tracker.TrackerIssue.
func githubToTrackerIssue(gh *Issue) tracker.TrackerIssue {
ti := tracker.TrackerIssue{
ID: strconv.Itoa(gh.ID),
Identifier: strconv.Itoa(gh.Number),
URL: gh.HTMLURL,
Title: gh.Title,
Description: gh.Body,
Labels: gh.LabelNames(),
Raw: gh,
}
if gh.State != "" {
ti.State = gh.State
}
if gh.Assignee != nil {
ti.Assignee = gh.Assignee.Login
ti.AssigneeID = strconv.Itoa(gh.Assignee.ID)
}
if gh.CreatedAt != nil {
ti.CreatedAt = *gh.CreatedAt
}
if gh.UpdatedAt != nil {
ti.UpdatedAt = *gh.UpdatedAt
}
if gh.ClosedAt != nil {
ti.CompletedAt = gh.ClosedAt
}
return ti
}