beads/cmd/bd/github.go
Sam bcf4da3c65
feat(github): sync epic/child and blocks relationships to GitHub (#5971)
* feat(github): sync epic/child and blocks relationships to GitHub

bd github sync only ever pushed issue content (title/body/labels/state);
Epic-child links and beads "blocks" dependencies never reached GitHub's
relationships panel. Adds a GitHub relationship-sync pass, mirroring the
existing GitLab dependency-link push, using GitHub's sub-issues and issue
dependencies REST APIs:

- internal/github/client.go: AddSubIssue/ListSubIssues and
  AddBlockedBy/ListBlockedBy against GitHub's sub_issues and
  dependencies/blocked_by endpoints.
- internal/github/links.go: DependencyLink + LinkResolver, converting beads
  epic/child parent-child dependencies into GitHub sub-issues and beads
  "blocks" dependencies into GitHub blocked_by relationships. Idempotent
  (lists current relationships before creating) and additive (stale remote
  relationships are left untouched).
- cmd/bd/github.go, cmd/bd/sync_push_pull.go: wires the push pass into both
  `bd github sync` and `bd github push`.

Verified against a live GitHub repo (create, list, and idempotent re-push
all confirmed via the real API), in addition to httptest-mocked unit tests.

Fixes gastownhall/beads#5970

* fix(github): update last_sync after relationship push to prevent sync churn

When AddSubIssue or AddBlockedBy is called, GitHub updates the issue's
updated_at timestamp as a side effect. Without updating last_sync after
the relationship push completes, the next sync would see those issues as
'changed' and pull/push them again, causing unnecessary churn.

This fix updates last_sync to the current time after a successful
relationship push (only if links were created), accounting for any
timestamp changes GitHub made during relationship creation.

Fixes continuous sync churn observed when running bd github sync
multiple times with no local changes.

* fix(github): use relationship reader for link sync

* fix(bazel): enumerate links.go and its tests in internal/github BUILD.bazel

The base advance added internal/github/BUILD.bazel with explicitly
enumerated srcs (gazelle-generated). This PR adds links.go,
links_test.go and review_fixes_test.go, which the base-generated
enumeration cannot know about, so //internal/github:github failed to
build:

  internal/github/tracker.go:41:35: undefined: LinkResolver
  internal/github/tracker.go:244:30: undefined: RefScope

Regenerated with `bazel run //:gazelle` (not hand-edited). Purely
additive; no deps change is needed because links.go only imports
//internal/types, already declared.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(github): scope the relationship pass to --parent and report skipped links

Maintainer review fixup for PR #5971, adopt-pr review iterations 1 and 3.

filterGitHubLinkScopedIssues reimplemented four of the SyncOptions
selectors and dropped ParentID, so `bd github sync --push --parent X`
narrowed the content push to the subtree and then created GitHub
sub-issue and blocked-by links for every GitHub-linked issue in the
workspace -- outward API writes on issues the user had excluded. It now
resolves the subtree and requires membership in it. The traversal moves
from gitlab.go to sync_flags.go as buildSyncDescendantSet, beside the
--parent flag that populates the field, and the GitLab and GitHub link
passes share the one copy. The doc comments that claimed parity with the
engine's push loop now enumerate the selectors this path actually
applies, and name the three it does not (State, ExcludeIDPrefix,
ExcludeIDPatterns -- no GitHub command sets them today).

PushLinkResult.UnsupportedSkipped had no reader anywhere in the repo, so
a host without the sub-issue/dependency APIs reported nothing at all.
pushGitHubDependencyLinks now emits one curated line for it, and a 404
on the source issue is classified apart from a 404 on the relationship
endpoint: collapsing the two reported a deleted, renumbered or
permission-hidden issue as an unsupported host. The classifying probe is
memoized per issue number, so a source carrying both link types costs
one extra read.

`bd github push --dry-run` printed "Synced N relationship links" for
links it had only planned, because PushLinks counts planned links in
Created. The `bd github sync` twin already gated that line on its
dry-run flag; the push side now does too.

Tests: a table test over filterGitHubLinkScopedIssues with a row per
selector -- its three ParentID rows fail without the scope fix -- plus a
direct pin that a blocks-edge dependent is not a descendant, and
httptest cases driving 404 through the list call, the create call and
the source-issue probe.

Not fixed here: the PR body still describes a `lastSync` advance that
commit ee6d65692 removed from this PR. That is a PR-surface edit, not a
code change, and is left to the finalize step.

Also not fixed here, and named in buildSyncDescendantSet's doc comment
so the shared helper does not read as covering every tracker: the ADO
link pass has the same scope hole this commit closes for GitHub.
pushADOLinks takes no sync options, so `bd ado sync --push --parent X`
still writes ADO relations for the whole workspace. That predates this
PR, and ADO is outside its diff.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Julian Knutsen <julian@gastownhall.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
2026-10-01 01:22:00 -07:00

780 lines
26 KiB
Go

// Package main provides the bd CLI commands.
package main
import (
"context"
"fmt"
"io"
"os"
"slices"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/github"
"github.com/steveyegge/beads/internal/metrics"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/tracker"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/issueops"
)
// GitHubConfig holds GitHub connection configuration.
type GitHubConfig struct {
Token string // Personal access token
Owner string // Repository owner (user or organization)
Repo string // Repository name
Repository string // Combined "owner/repo" format
URL string // Custom API URL (for GitHub Enterprise)
}
// githubCmd is the root command for GitHub operations.
var githubCmd = &cobra.Command{
Use: "github",
Short: "GitHub integration commands",
Long: `Commands for syncing issues between beads and GitHub.
Configuration can be set via 'bd config' or environment variables:
github.token / GITHUB_TOKEN - Personal access token
github.owner / GITHUB_OWNER - Repository owner
github.repo / GITHUB_REPO - Repository name
github.repository / GITHUB_REPOSITORY - Combined "owner/repo" format
github.url / GITHUB_API_URL - Custom API URL (GitHub Enterprise)`,
}
// githubSyncCmd synchronizes issues between beads and GitHub.
var githubSyncCmd = &cobra.Command{
Use: "sync",
Short: "Sync issues with GitHub",
Long: `Synchronize issues between beads and GitHub.
By default, performs bidirectional sync:
- Pulls new/updated issues from GitHub to beads
- Pushes local beads issues to GitHub
Use --pull-only or --push-only to limit direction.`,
SilenceUsage: true,
SilenceErrors: true,
RunE: runGitHubSync,
}
// githubStatusCmd displays GitHub configuration and sync status.
var githubStatusCmd = &cobra.Command{
Use: "status",
Short: "Show GitHub sync status",
Long: `Display current GitHub configuration and sync status.`,
SilenceUsage: true,
SilenceErrors: true,
RunE: runGitHubStatus,
}
// githubReposCmd lists accessible GitHub repositories.
var githubReposCmd = &cobra.Command{
Use: "repos",
Short: "List accessible GitHub repositories",
Long: `List GitHub repositories that the configured token has access to.`,
SilenceUsage: true,
SilenceErrors: true,
RunE: runGitHubRepos,
}
var (
githubSyncDryRun bool
githubSyncPullOnly bool
githubSyncPushOnly bool
githubPreferLocal bool
githubPreferGitHub bool
githubPreferNewer bool
)
// GitHubConflictStrategy defines how to resolve conflicts between local and GitHub versions.
type GitHubConflictStrategy string
const (
// GitHubConflictPreferNewer uses the most recently updated version (default).
GitHubConflictPreferNewer GitHubConflictStrategy = "prefer-newer"
// GitHubConflictPreferLocal always keeps the local beads version.
GitHubConflictPreferLocal GitHubConflictStrategy = "prefer-local"
// GitHubConflictPreferGitHub always uses the GitHub version.
GitHubConflictPreferGitHub GitHubConflictStrategy = "prefer-github"
)
// getGitHubConflictStrategy determines the conflict strategy from flag values.
// Returns error if multiple conflicting flags are set.
func getGitHubConflictStrategy(preferLocal, preferGitHub, preferNewer bool) (GitHubConflictStrategy, error) {
flagsSet := 0
if preferLocal {
flagsSet++
}
if preferGitHub {
flagsSet++
}
if preferNewer {
flagsSet++
}
if flagsSet > 1 {
return "", fmt.Errorf("cannot use multiple conflict resolution flags")
}
if preferLocal {
return GitHubConflictPreferLocal, nil
}
if preferGitHub {
return GitHubConflictPreferGitHub, nil
}
return GitHubConflictPreferNewer, nil
}
// parseGitHubSourceSystem parses a source system string like "github:https://...:42"
// Returns the issue number and ok (whether it's a valid GitHub source).
func parseGitHubSourceSystem(sourceSystem string) (number int, ok bool) {
if !strings.HasPrefix(sourceSystem, "github:") {
return 0, false
}
// Find last ":" and parse the number after it
lastColon := strings.LastIndex(sourceSystem, ":")
if lastColon == -1 || lastColon == len(sourceSystem)-1 {
return 0, false
}
var err error
number, err = strconv.Atoi(sourceSystem[lastColon+1:])
if err != nil {
return 0, false
}
return number, true
}
func init() {
// Add subcommands to github
githubCmd.AddCommand(githubSyncCmd)
githubCmd.AddCommand(githubStatusCmd)
githubCmd.AddCommand(githubReposCmd)
// Add flags to sync command
githubSyncCmd.Flags().BoolVar(&githubSyncDryRun, "dry-run", false, "Show what would be synced without making changes")
githubSyncCmd.Flags().BoolVar(&githubSyncPullOnly, "pull-only", false, "Only pull issues from GitHub")
githubSyncCmd.Flags().BoolVar(&githubSyncPushOnly, "push-only", false, "Only push issues to GitHub")
// Conflict resolution flags (mutually exclusive)
githubSyncCmd.Flags().BoolVar(&githubPreferLocal, "prefer-local", false, "On conflict, keep local beads version")
githubSyncCmd.Flags().BoolVar(&githubPreferGitHub, "prefer-github", false, "On conflict, use GitHub version")
githubSyncCmd.Flags().BoolVar(&githubPreferNewer, "prefer-newer", false, "On conflict, use most recent version (default)")
registerSelectiveSyncFlags(githubSyncCmd)
// Register github command with root
rootCmd.AddCommand(githubCmd)
}
// getGitHubConfig returns GitHub configuration from bd config or environment.
func getGitHubConfig() GitHubConfig {
ctx := context.Background()
config := GitHubConfig{}
config.Token = getGitHubConfigValue(ctx, "github.token")
config.Owner = getGitHubConfigValue(ctx, "github.owner")
config.Repo = getGitHubConfigValue(ctx, "github.repo")
config.Repository = getGitHubConfigValue(ctx, "github.repository")
config.URL = getGitHubConfigValue(ctx, "github.url")
// Parse combined owner/repo format if individual fields are empty
if (config.Owner == "" || config.Repo == "") && config.Repository != "" {
parts := strings.SplitN(config.Repository, "/", 2)
if len(parts) == 2 {
if config.Owner == "" {
config.Owner = parts[0]
}
if config.Repo == "" {
config.Repo = parts[1]
}
}
}
return config
}
// getGitHubConfigValue reads a GitHub configuration value from store or environment.
func getGitHubConfigValue(ctx context.Context, key string) string {
// Secret keys (e.g. github.token) are stored in config.yaml, not the
// Dolt database, to avoid leaking secrets when pushing to remotes.
if config.IsYamlOnlyKey(key) {
if value := config.GetString(key); value != "" {
return value
}
// Fall back to environment variable
envKey := githubConfigToEnvVar(key)
if envKey != "" {
if value := os.Getenv(envKey); value != "" {
return value
}
}
return ""
}
// Try to read from store (works in direct mode)
if store != nil {
value, _ := store.GetConfig(ctx, key)
if value != "" {
return value
}
} else if dbPath != "" {
tempStore, err := openReadOnlyStoreForDBPath(ctx, dbPath)
if err == nil {
defer func() { _ = tempStore.Close() }()
value, _ := tempStore.GetConfig(ctx, key)
if value != "" {
return value
}
}
}
// Fall back to environment variable
envKey := githubConfigToEnvVar(key)
if envKey != "" {
if value := os.Getenv(envKey); value != "" {
return value
}
}
return ""
}
// githubConfigToEnvVar maps GitHub config keys to their environment variable names.
func githubConfigToEnvVar(key string) string {
switch key {
case "github.token":
return "GITHUB_TOKEN"
case "github.owner":
return "GITHUB_OWNER"
case "github.repo":
return "GITHUB_REPO"
case "github.repository":
return "GITHUB_REPOSITORY"
case "github.url":
return "GITHUB_API_URL"
default:
return ""
}
}
// validateGitHubConfig checks that required configuration is present.
func validateGitHubConfig(config GitHubConfig) error {
if config.Token == "" {
return fmt.Errorf("github.token is not configured. Set via 'bd config set github.token <token>' or GITHUB_TOKEN environment variable")
}
if config.Owner == "" {
return fmt.Errorf("github.owner is not configured. Set via 'bd config set github.owner <owner>' or GITHUB_OWNER environment variable")
}
if config.Repo == "" {
return fmt.Errorf("github.repo is not configured. Set via 'bd config set github.repo <repo>' or GITHUB_REPO environment variable")
}
return nil
}
// maskGitHubToken masks a token for safe display.
// Shows only the first 4 characters to aid identification without
// revealing enough to reduce brute-force entropy.
func maskGitHubToken(token string) string {
if token == "" {
return "(not set)"
}
if len(token) <= 4 {
return "****"
}
return token[:4] + "****"
}
// getGitHubClient creates a GitHub client from the current configuration.
func getGitHubClient(config GitHubConfig) *github.Client {
client := github.NewClient(config.Token, config.Owner, config.Repo)
if config.URL != "" {
client = client.WithBaseURL(config.URL)
}
return client
}
// runGitHubStatus implements the github status command.
func runGitHubStatus(cmd *cobra.Command, args []string) error {
if usesProxiedServer() {
return HandleErrorRespectJSON("github status is not supported in proxied-server mode")
}
evt := metrics.NewCommandEvent("github-status")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
config := getGitHubConfig()
out := cmd.OutOrStdout()
_, _ = fmt.Fprintln(out, "GitHub Configuration")
_, _ = fmt.Fprintln(out, "====================")
_, _ = fmt.Fprintf(out, "Token: %s\n", maskGitHubToken(config.Token))
_, _ = fmt.Fprintf(out, "Owner: %s\n", config.Owner)
_, _ = fmt.Fprintf(out, "Repository: %s\n", config.Repo)
if config.URL != "" {
_, _ = fmt.Fprintf(out, "API URL: %s\n", config.URL)
}
// Validate configuration
if err := validateGitHubConfig(config); err != nil {
_, _ = fmt.Fprintf(out, "\nStatus: ❌ Not configured\n")
_, _ = fmt.Fprintf(out, "Error: %v\n", err)
return nil
}
_, _ = fmt.Fprintf(out, "\nStatus: ✓ Configured\n")
return nil
}
// runGitHubRepos implements the github repos command.
func runGitHubRepos(cmd *cobra.Command, args []string) error {
if usesProxiedServer() {
return HandleErrorRespectJSON("github repos is not supported in proxied-server mode")
}
evt := metrics.NewCommandEvent("github-repos")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
config := getGitHubConfig()
if config.Token == "" {
return HandleError("github.token is not configured. Set via 'bd config set github.token <token>' or GITHUB_TOKEN environment variable")
}
out := cmd.OutOrStdout()
client := getGitHubClient(config)
ctx := context.Background()
repos, err := client.ListRepositories(ctx)
if err != nil {
return HandleError("failed to fetch repositories: %v", err)
}
_, _ = fmt.Fprintln(out, "Accessible GitHub Repositories")
_, _ = fmt.Fprintln(out, "==============================")
for _, r := range repos {
_, _ = fmt.Fprintf(out, " %s\n", r.FullName)
if r.Description != "" {
_, _ = fmt.Fprintf(out, " %s\n", r.Description)
}
_, _ = fmt.Fprintf(out, " %s\n", r.HTMLURL)
_, _ = fmt.Fprintln(out)
}
if len(repos) == 0 {
_, _ = fmt.Fprintln(out, "No repositories found")
}
return nil
}
// runGitHubSync implements the github sync command.
// Uses the tracker.Engine for all sync operations.
func runGitHubSync(cmd *cobra.Command, args []string) error {
if usesProxiedServer() {
return HandleErrorRespectJSON("github sync is not supported in proxied-server mode")
}
evt := metrics.NewCommandEvent("github-sync")
defer func() {
if c := metrics.Global(); c != nil {
c.CloseEventAndAdd(evt)
}
}()
config := getGitHubConfig()
if err := validateGitHubConfig(config); err != nil {
return HandleError("%v", err)
}
if !githubSyncDryRun {
CheckReadonly("github sync")
}
if githubSyncPullOnly && githubSyncPushOnly {
return HandleError("cannot use both --pull-only and --push-only")
}
conflictStrategy, err := getGitHubConflictStrategy(githubPreferLocal, githubPreferGitHub, githubPreferNewer)
if err != nil {
return HandleError("%v (--prefer-local, --prefer-github, --prefer-newer)", err)
}
if err := ensureStoreActive(); err != nil {
return HandleError("database not available: %v", err)
}
out := cmd.OutOrStdout()
ctx := context.Background()
gt := &github.Tracker{}
if err := gt.Init(ctx, store); err != nil {
return HandleError("initializing GitHub tracker: %v", err)
}
// Create the sync engine
engine := tracker.NewEngine(gt, store, actor)
engine.OnMessage = func(msg string) { _, _ = fmt.Fprintln(out, " "+msg) }
engine.OnWarning = func(msg string) { _, _ = fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
// Set up GitHub-specific pull and push hooks
engine.PullHooks = buildGitHubPullHooks(ctx)
engine.PushHooks = buildGitHubPushHooks(gt)
// Build sync options from CLI flags
pull := !githubSyncPushOnly
push := !githubSyncPullOnly
opts := tracker.SyncOptions{
Pull: pull,
Push: push,
DryRun: githubSyncDryRun,
}
if err := applySelectiveSyncFlags(cmd, &opts, push); err != nil {
return HandleError("%v", err)
}
switch conflictStrategy {
case GitHubConflictPreferLocal:
opts.ConflictResolution = tracker.ConflictLocal
case GitHubConflictPreferGitHub:
opts.ConflictResolution = tracker.ConflictExternal
default:
opts.ConflictResolution = tracker.ConflictTimestamp
}
if githubSyncDryRun {
_, _ = fmt.Fprintln(out, "Dry run mode - no changes will be made")
_, _ = fmt.Fprintln(out)
}
result, err := engine.Sync(ctx, opts)
if err != nil {
return HandleError("%v", err)
}
// Relationship push pass: converge beads parent/child links and "blocks"
// dependencies into GitHub sub-issues and issue dependencies. Runs after
// the content sync so relationships stay correct even when issue content
// itself was unchanged and the main push loop skipped the issue.
//
// This pass deliberately leaves github.last_sync alone. That cursor is the
// engine's record of when local rows were last reconciled, and
// DetectConflicts treats every issue updated after it as a local edit;
// advancing it here would hide edits made since the engine set it from the
// next sync's conflict detection. The engine owns the cursor
// (internal/tracker/engine.go), and this pass writes no local rows.
var linksPushed int
if push {
var linkWarnings []string
warnLink := func(msg string) {
linkWarnings = append(linkWarnings, msg)
_, _ = fmt.Fprintf(os.Stderr, "Warning: %s\n", msg)
}
linksPushed = pushGitHubDependencyLinks(ctx, gt, store, opts, githubSyncDryRun, out, warnLink)
result.Warnings = append(result.Warnings, linkWarnings...)
}
// Output results
if !githubSyncDryRun {
if result.Stats.Pulled > 0 {
_, _ = fmt.Fprintf(out, "✓ Pulled %d issues (%d created, %d updated)\n",
result.Stats.Pulled, result.Stats.Created, result.Stats.Updated)
}
if result.Stats.Pushed > 0 {
_, _ = fmt.Fprintf(out, "✓ Pushed %d issues\n", result.Stats.Pushed)
}
if linksPushed > 0 {
_, _ = fmt.Fprintf(out, "✓ Synced %d relationship links\n", linksPushed)
}
if result.Stats.Conflicts > 0 {
_, _ = fmt.Fprintf(out, "→ Resolved %d conflicts\n", result.Stats.Conflicts)
}
}
if githubSyncDryRun {
_, _ = fmt.Fprintln(out)
_, _ = fmt.Fprintln(out, "Run without --dry-run to apply changes")
}
return nil
}
// githubLinkSyncData holds the desired GitHub relationship links collected
// from beads dependency state for the issues in scope of a sync/push pass.
type githubLinkSyncData struct {
DesiredLinks []github.DependencyLink
}
// collectGitHubLinkSyncData walks the beads issues in scope of opts and
// derives the GitHub relationship links (sub-issue for epic/child,
// blocked_by for beads "blocks" dependencies) that should exist remotely.
// Only issues already linked to GitHub (an ExternalRef that scope resolves to
// an issue number in the configured repository) can contribute or receive a
// link; refs pointing at another repository or host are skipped, since the
// relationship endpoints take bare issue numbers scoped to one repo.
//
// The workspace read is issueops.Reader's, reached through the store's own
// accessor so it carries whatever layers that store carries; the request it
// takes is githubLinkSyncListRequest below.
func collectGitHubLinkSyncData(ctx context.Context, st storage.Storage, scope github.RefScope, opts tracker.SyncOptions) (githubLinkSyncData, []string) {
if st == nil {
return githubLinkSyncData{}, []string{"GitHub relationship sync skipped: database not available"}
}
reader, err := st.IssueReader()
if err != nil {
return githubLinkSyncData{}, []string{fmt.Sprintf("GitHub relationship sync skipped: %v", err)}
}
page, err := reader.List(ctx, githubLinkSyncListRequest())
if err != nil {
return githubLinkSyncData{}, []string{fmt.Sprintf("GitHub relationship sync skipped: %v", err)}
}
allIssues := make([]*types.Issue, 0, len(page.Items))
for _, item := range page.Items {
if item == nil || item.Issue == nil {
continue
}
allIssues = append(allIssues, item.Issue)
}
// --parent scopes the content push to a subtree; the relationship pass has
// to honor the same subtree or it writes GitHub links for issues the user
// excluded from the sync.
var descendantSet map[string]bool
if opts.ParentID != "" {
descendantSet, err = buildSyncDescendantSet(ctx, st, opts.ParentID)
if err != nil {
return githubLinkSyncData{}, []string{fmt.Sprintf("GitHub relationship sync skipped: resolving parent %s: %v", opts.ParentID, err)}
}
}
scopedIssues := filterGitHubLinkScopedIssues(allIssues, opts, descendantSet)
scopedIssueIDs := make(map[string]bool, len(scopedIssues))
for _, issue := range scopedIssues {
if issue != nil && issue.ID != "" {
scopedIssueIDs[issue.ID] = true
}
}
var warnings []string
var desired []github.DependencyLink
for _, issue := range scopedIssues {
if issue.ExternalRef == nil {
continue
}
deps, err := st.GetDependenciesWithMetadata(ctx, issue.ID)
if err != nil {
warnings = append(warnings, fmt.Sprintf("GitHub relationship sync skipped dependencies for %s: %v", issue.ID, err))
continue
}
for _, dep := range deps {
if !scopedIssueIDs[dep.ID] {
continue
}
switch dep.DependencyType {
case types.DepParentChild:
if link, ok := scope.SubIssueLinkFromParentChild(issue, dep); ok {
desired = append(desired, link)
}
case types.DepBlocks:
if link, ok := scope.BlockedByLinkFromBeadsDependency(issue, dep); ok {
desired = append(desired, link)
}
}
}
}
return githubLinkSyncData{DesiredLinks: desired}, warnings
}
// githubLinkSyncListRequest is the whole-workspace read the relationship pass
// runs: every bead in both planes, at any status, of any type, unpaged.
//
// SCOPE IS NOT THIS REQUEST'S JOB. filterGitHubLinkScopedIssues narrows to the
// tracker.SyncOptions selectors the GitHub commands can set — that function
// names them and is the place to extend — the way the engine's push loop
// narrows the same unfiltered read with shouldPushIssue
// (internal/tracker/engine.go). Expressing half that selection here in a second
// vocabulary is how the two would drift, so this lifts every default a listing
// applies rather than reproducing the engine's choices:
//
// AllFlag drops the default status exclusions, and with them the
// pinned predicate. A relationship is still true for a closed
// bead, and the engine pushes closed issues too.
// IncludeAllTypes drops the template, gate and infra type exclusions AND the
// plane decision, so the wisps table is read. opts.
// ExcludeEphemeral is what drops that plane, downstream, when
// the caller asked for it.
// Limit zero, not nil: nil takes the shared list default, which
// would silently cap relationship sync at one page.
//
// SkipLabels and SkipCounts choose what is HYDRATED, never which rows match.
// This pass reads ID, ExternalRef, IssueType and Ephemeral and nothing else,
// and the counts are per-row aggregate joins the scan has no use for.
func githubLinkSyncListRequest() issueops.ListRequest {
unlimited := 0
return issueops.ListRequest{
AllFlag: true,
IncludeAllTypes: true,
Limit: &unlimited,
SkipLabels: true,
SkipCounts: true,
}
}
// filterGitHubLinkScopedIssues narrows the unfiltered workspace read to the
// issues opts selects. descendantSet is the resolved --parent subtree, or nil
// when --parent was not given; a non-nil set is a membership requirement.
//
// The selectors applied here are the ones a GitHub command can put on
// SyncOptions today: ParentID (as descendantSet), IssueIDs, ExcludeEphemeral,
// TypeFilter and ExcludeTypes. SyncOptions carries further selectors that the
// engine's push loop honors and no GitHub command currently sets — State,
// ExcludeIDPrefix, ExcludeIDPatterns. Anything that starts setting one of
// those has to extend this function in the same change, or the relationship
// pass will write links for issues the content push skipped.
func filterGitHubLinkScopedIssues(issues []*types.Issue, opts tracker.SyncOptions, descendantSet map[string]bool) []*types.Issue {
var issueIDSet map[string]bool
if len(opts.IssueIDs) > 0 {
issueIDSet = make(map[string]bool, len(opts.IssueIDs))
for _, id := range opts.IssueIDs {
issueIDSet[id] = true
}
}
result := make([]*types.Issue, 0, len(issues))
for _, issue := range issues {
if issue == nil {
continue
}
if issueIDSet != nil && !issueIDSet[issue.ID] {
continue
}
if descendantSet != nil && !descendantSet[issue.ID] {
continue
}
if opts.ExcludeEphemeral && issue.Ephemeral {
continue
}
if len(opts.TypeFilter) > 0 && !slices.Contains(opts.TypeFilter, issue.IssueType) {
continue
}
if slices.Contains(opts.ExcludeTypes, issue.IssueType) {
continue
}
result = append(result, issue)
}
return result
}
// pushGitHubDependencyLinks runs the relationship push pass: it converts
// beads epic/child links and "blocks" dependencies among the scoped issues
// (per opts) into GitHub sub-issues and issue dependencies. Additive — stale
// remote relationships are left untouched. Shared by `bd github sync` and
// `bd github push` so both reach the same relationship parity. Dry-run plan
// lines are written to out (unless --json); warnings are delivered via warn.
// Returns the number of relationships created.
func pushGitHubDependencyLinks(ctx context.Context, gt *github.Tracker, st storage.Storage, opts tracker.SyncOptions, dryRun bool, out io.Writer, warn func(string)) int {
if gt == nil {
return 0
}
linkData, collectWarnings := collectGitHubLinkSyncData(ctx, st, gt.RefScope(), opts)
for _, warning := range collectWarnings {
warn(warning)
}
resolver := gt.LinkResolver()
if resolver == nil || len(linkData.DesiredLinks) == 0 {
return 0
}
res := resolver.PushLinks(ctx, linkData.DesiredLinks, github.PushLinkOptions{
DryRun: dryRun,
OnPlan: func(link github.DependencyLink) {
if !jsonOutput {
_, _ = fmt.Fprintf(out, " [dry-run] Would create GitHub %s relationship: #%d -> #%d\n",
link.LinkType, link.FromNumber, link.ToNumber)
}
},
})
for _, err := range res.Errors {
warn(fmt.Sprintf("GitHub relationship sync: %v", err))
}
// Both counters are a silent no-op otherwise: PushLinks deliberately does
// not raise a 404 per link, so this is the only place the user learns the
// relationships were not written. One line per class, never merged — an
// unavailable endpoint is a host capability the operator cannot fix per
// issue, a missing source issue is a stale local ref they can.
if res.UnsupportedSkipped > 0 {
warn(fmt.Sprintf("GitHub relationship sync: %d link(s) skipped; the sub-issue/dependency API is unavailable on this host", res.UnsupportedSkipped))
}
if res.SourceMissing > 0 {
warn(fmt.Sprintf("GitHub relationship sync: %d link(s) skipped; the source issue is not readable on GitHub (deleted, renumbered, or not visible to this token)", res.SourceMissing))
}
return res.Created
}
// buildGitHubPushHooks creates PushHooks for GitHub-specific push behavior.
// The ContentEqual hook lets the engine skip issues whose pushable fields
// already match GitHub, so repeated `github sync --push-only` / `github push`
// runs don't re-PATCH unchanged issues (gastownhall/beads#4214).
func buildGitHubPushHooks(gt *github.Tracker) *tracker.PushHooks {
config := gt.MappingConfig()
if config == nil {
config = github.DefaultMappingConfig()
}
return &tracker.PushHooks{
ContentEqual: func(local *types.Issue, remote *tracker.TrackerIssue) bool {
if remote == nil {
return false
}
gh, ok := remote.Raw.(*github.Issue)
if !ok || gh == nil {
return false
}
return github.PushFieldsEqual(local, gh, config)
},
// ContentHash lets the engine skip the per-issue GitHub fetch entirely
// when an issue is unchanged since its last push, so a no-op
// `github sync --push-only` makes ~zero REST calls instead of one GET
// per linked issue (gastownhall/beads#4214).
ContentHash: func(local *types.Issue) string {
return github.PushContentHash(local, config)
},
// TargetScope supplies the host and repository omitted by shorthand refs
// such as github:42, so changing GitHub target configuration invalidates
// the local no-op cache.
TargetScope: gt.PushTargetScope,
}
}
// buildGitHubPullHooks creates PullHooks for GitHub-specific pull behavior.
func buildGitHubPullHooks(ctx context.Context) *tracker.PullHooks {
prefix := "bd"
// YAML config takes precedence — in shared-server mode the DB
// may belong to a different project (GH#2469).
if p := config.GetString("issue-prefix"); p != "" {
prefix = p
} else if store != nil {
if p, err := store.GetConfig(ctx, "issue_prefix"); err == nil && p != "" {
prefix = p
}
}
return &tracker.PullHooks{
GenerateID: func(_ context.Context, issue *types.Issue) error {
if issue.ID == "" {
issue.ID = generateIssueID(prefix)
}
return nil
},
}
}