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>
This commit is contained in:
Sam 2026-10-01 18:22:00 +10:00 • committed by GitHub
commit bcf4da3c65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 1390 additions and 24 deletions

View file

@ -755,6 +755,7 @@ go_test(
"gc_embedded_test.go",
"gc_prune_test.go",
"git_repo_template_test.go",
"github_link_scope_test.go",
"gitlab_integration_test.go",
"gitlab_test.go",
"global_identity_integration_test.go",

View file

@ -4,7 +4,9 @@ package main
import (
"context"
"fmt"
"io"
"os"
"slices"
"strconv"
"strings"
@ -12,8 +14,10 @@ import (
"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.
@ -456,6 +460,28 @@ func runGitHubSync(cmd *cobra.Command, args []string) error {
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 {
@ -465,6 +491,9 @@ func runGitHubSync(cmd *cobra.Command, args []string) error {
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)
}
@ -478,6 +507,221 @@ func runGitHubSync(cmd *cobra.Command, args []string) error {
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`

View file

@ -0,0 +1,155 @@
package main
import (
"context"
"slices"
"testing"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/tracker"
"github.com/steveyegge/beads/internal/types"
)
// descendantFakeStore answers GetDependentsWithMetadata from an in-memory
// parent -> children map. Every other storage.Storage method is promoted from
// the nil embedded interface, so a reach for one panics rather than silently
// widening what this test covers.
type descendantFakeStore struct {
storage.Storage
children map[string][]string
// related are non-parent-child dependents, returned alongside the real
// children so the traversal's edge-type filter is exercised.
related map[string][]string
}
func (s *descendantFakeStore) GetDependentsWithMetadata(_ context.Context, issueID string) ([]*types.IssueWithDependencyMetadata, error) {
var out []*types.IssueWithDependencyMetadata
for _, childID := range s.children[issueID] {
out = append(out, &types.IssueWithDependencyMetadata{
Issue: types.Issue{ID: childID},
DependencyType: types.DepParentChild,
})
}
for _, otherID := range s.related[issueID] {
out = append(out, &types.IssueWithDependencyMetadata{
Issue: types.Issue{ID: otherID},
DependencyType: types.DepBlocks,
})
}
return out, nil
}
// TestFilterGitHubLinkScopedIssues pins one row per tracker.SyncOptions
// selector the GitHub commands can set. The --parent row is the one that
// matters most: before the relationship pass honoured opts.ParentID it kept
// every issue in the workspace, so `bd github sync --push --parent X` created
// GitHub links on issues the content push had excluded.
func TestFilterGitHubLinkScopedIssues(t *testing.T) {
issues := []*types.Issue{
{ID: "bd-epic", IssueType: types.TypeEpic},
{ID: "bd-child", IssueType: types.TypeTask},
{ID: "bd-grandchild", IssueType: types.TypeBug},
{ID: "bd-outside", IssueType: types.TypeTask},
{ID: "bd-wisp", IssueType: types.TypeTask, Ephemeral: true},
}
store := &descendantFakeStore{
children: map[string][]string{
"bd-epic": {"bd-child"},
"bd-child": {"bd-grandchild"},
},
// bd-outside blocks bd-epic: a dependent, but not a descendant.
related: map[string][]string{"bd-epic": {"bd-outside"}},
}
tests := []struct {
name string
opts tracker.SyncOptions
want []string
}{
{
name: "no selectors keeps everything",
opts: tracker.SyncOptions{},
want: []string{"bd-epic", "bd-child", "bd-grandchild", "bd-outside", "bd-wisp"},
},
{
name: "IssueIDs keeps only the listed beads",
opts: tracker.SyncOptions{IssueIDs: []string{"bd-child", "bd-wisp"}},
want: []string{"bd-child", "bd-wisp"},
},
{
name: "ParentID keeps the subtree and drops the rest",
opts: tracker.SyncOptions{ParentID: "bd-epic"},
want: []string{"bd-epic", "bd-child", "bd-grandchild"},
},
{
name: "ParentID on a leaf keeps only that leaf",
opts: tracker.SyncOptions{ParentID: "bd-grandchild"},
want: []string{"bd-grandchild"},
},
{
name: "TypeFilter keeps only the named types",
opts: tracker.SyncOptions{TypeFilter: []types.IssueType{types.TypeEpic, types.TypeBug}},
want: []string{"bd-epic", "bd-grandchild"},
},
{
name: "ExcludeTypes drops the named types",
opts: tracker.SyncOptions{ExcludeTypes: []types.IssueType{types.TypeTask}},
want: []string{"bd-epic", "bd-grandchild"},
},
{
name: "ExcludeEphemeral drops wisps",
opts: tracker.SyncOptions{ExcludeEphemeral: true},
want: []string{"bd-epic", "bd-child", "bd-grandchild", "bd-outside"},
},
{
name: "ParentID composes with the other selectors",
opts: tracker.SyncOptions{ParentID: "bd-epic", ExcludeTypes: []types.IssueType{types.TypeBug}},
want: []string{"bd-epic", "bd-child"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Resolve the subtree exactly the way collectGitHubLinkSyncData
// does, so the helper is under test too.
var descendantSet map[string]bool
if tt.opts.ParentID != "" {
var err error
descendantSet, err = buildSyncDescendantSet(context.Background(), store, tt.opts.ParentID)
if err != nil {
t.Fatalf("buildSyncDescendantSet(%q) error = %v", tt.opts.ParentID, err)
}
}
got := filterGitHubLinkScopedIssues(issues, tt.opts, descendantSet)
ids := make([]string, 0, len(got))
for _, issue := range got {
ids = append(ids, issue.ID)
}
if !slices.Equal(ids, tt.want) {
t.Errorf("filterGitHubLinkScopedIssues() = %v, want %v", ids, tt.want)
}
})
}
}
// TestBuildSyncDescendantSetIgnoresNonParentChildEdges pins the traversal's
// edge-type filter directly: a blocks dependent is not a descendant, so
// --parent must not drag it into scope.
func TestBuildSyncDescendantSetIgnoresNonParentChildEdges(t *testing.T) {
store := &descendantFakeStore{
children: map[string][]string{"bd-epic": {"bd-child"}},
related: map[string][]string{"bd-epic": {"bd-blocker"}},
}
got, err := buildSyncDescendantSet(context.Background(), store, "bd-epic")
if err != nil {
t.Fatalf("buildSyncDescendantSet error = %v", err)
}
if !got["bd-epic"] || !got["bd-child"] {
t.Errorf("descendant set = %v, want it to contain bd-epic and bd-child", got)
}
if got["bd-blocker"] {
t.Errorf("descendant set = %v, want it to exclude the blocks-edge dependent bd-blocker", got)
}
}

View file

@ -720,7 +720,7 @@ func collectGitLabLinkSyncData(ctx context.Context, st storage.Storage, opts tra
var warnings []string
var descendantSet map[string]bool
if opts.ParentID != "" {
descendantSet, err = buildGitLabDescendantSet(ctx, st, opts.ParentID)
descendantSet, err = buildSyncDescendantSet(ctx, st, opts.ParentID)
if err != nil {
return gitlabLinkSyncData{}, []string{fmt.Sprintf("GitLab dependency link sync skipped: resolving parent %s: %v", opts.ParentID, err)}
}
@ -803,26 +803,6 @@ func gitlabIssueAllowedByPushFilters(issue *types.Issue, opts tracker.SyncOption
return true
}
func buildGitLabDescendantSet(ctx context.Context, st storage.Storage, parentID string) (map[string]bool, error) {
result := map[string]bool{parentID: true}
queue := []string{parentID}
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
dependents, err := st.GetDependentsWithMetadata(ctx, current)
if err != nil {
return nil, fmt.Errorf("getting dependents of %s: %w", current, err)
}
for _, dep := range dependents {
if dep.DependencyType == types.DepParentChild && !result[dep.Issue.ID] {
result[dep.Issue.ID] = true
queue = append(queue, dep.Issue.ID)
}
}
}
return result, nil
}
func gitlabIssueIDSet(ids []string) map[string]bool {
if len(ids) == 0 {
return nil

View file

@ -1,10 +1,13 @@
package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/tracker"
"github.com/steveyegge/beads/internal/types"
)
// registerSelectiveSyncFlags adds --issues and --parent flags to a tracker sync command.
@ -44,3 +47,35 @@ func applySelectiveSyncFlags(cmd *cobra.Command, opts *tracker.SyncOptions, push
}
return nil
}
// buildSyncDescendantSet resolves opts.ParentID into the set of issue IDs
// --parent selects: the parent itself plus every bead reachable from it
// through parent-child dependency edges. A nil result means no subtree scope
// was requested; callers filtering by it must treat a non-nil set as a
// membership requirement.
//
// Shared by the GitLab and GitHub relationship passes so a subtree-scoped push
// and its relationship pass agree on what "in scope" means. The engine has its
// own copy for the content push (Engine.buildDescendantSet,
// internal/tracker/engine.go); these two must stay in agreement. The ADO
// relationship pass is not converted: pushADOLinks (ado.go) takes no sync
// options, so --parent and --issues do not narrow the ADO relations it writes.
func buildSyncDescendantSet(ctx context.Context, st storage.Storage, parentID string) (map[string]bool, error) {
result := map[string]bool{parentID: true}
queue := []string{parentID}
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
dependents, err := st.GetDependentsWithMetadata(ctx, current)
if err != nil {
return nil, fmt.Errorf("getting dependents of %s: %w", current, err)
}
for _, dep := range dependents {
if dep.DependencyType == types.DepParentChild && !result[dep.Issue.ID] {
result[dep.Issue.ID] = true
queue = append(queue, dep.Issue.ID)
}
}
}
return result, nil
}

View file

@ -611,15 +611,29 @@ func runGitHubPush(cmd *cobra.Command, args []string) error {
engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
engine.PushHooks = buildGitHubPushHooks(gt)
result, err := engine.Sync(ctx, tracker.SyncOptions{
opts := tracker.SyncOptions{
Push: true,
Pull: false,
DryRun: dryRun,
IssueIDs: args,
})
}
result, err := engine.Sync(ctx, opts)
if err != nil {
return err
}
// Relationship push pass, matching `bd github sync`: converge beads
// epic/child links and "blocks" dependencies among the requested issues
// into GitHub sub-issues and issue dependencies.
warnLink := func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) }
linksPushed := pushGitHubDependencyLinks(ctx, gt, store, opts, dryRun, os.Stdout, warnLink)
// Under --dry-run PushLinks counts planned links in Created, so this line
// would affirm writes that never happened; the `bd github sync` twin gates
// it the same way.
if linksPushed > 0 && !dryRun && !jsonOutput {
fmt.Printf("✓ Synced %d relationship links\n", linksPushed)
}
outputSyncResult(result, dryRun)
return nil
}

View file

@ -6,6 +6,7 @@ go_library(
"client.go",
"errors.go",
"fieldmapper.go",
"links.go",
"mapping.go",
"tracker.go",
"types.go",
@ -23,8 +24,10 @@ go_test(
name = "github_test",
srcs = [
"client_ratelimit_test.go",
"links_test.go",
"mapping_test.go",
"push_dedup_test.go",
"review_fixes_test.go",
"tracker_test.go",
"types_test.go",
],

View file

@ -144,7 +144,7 @@ func (c *Client) doRequest(ctx context.Context, method, urlStr string, body inte
continue
}
return nil, nil, fmt.Errorf("API error: %s (status %d)", string(respBody), resp.StatusCode)
return nil, nil, &APIError{StatusCode: resp.StatusCode, Body: string(respBody), URL: urlStr}
}
if lastRateLimit != nil {
@ -364,6 +364,92 @@ func (c *Client) FetchIssueByNumber(ctx context.Context, number int) (*Issue, er
return &issue, nil
}
// listIssuePages walks every page of an endpoint that returns a bare JSON
// array of issues (sub-issues, dependencies), following GitHub's Link header
// the same way FetchIssues and ListRepositories do. Relationship endpoints
// default to 30 items per page, so an unpaginated read silently truncates the
// existing-relationship set and makes the push pass re-POST links that are
// already there.
func (c *Client) listIssuePages(ctx context.Context, endpoint string) ([]Issue, error) {
var allIssues []Issue
page := 1
for {
select {
case <-ctx.Done():
return allIssues, ctx.Err()
default:
}
urlStr := fmt.Sprintf("%s?per_page=%d&page=%d", endpoint, MaxPerPage, page)
respBody, headers, err := c.doRequest(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return nil, err
}
var issues []Issue
if err := json.Unmarshal(respBody, &issues); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
allIssues = append(allIssues, issues...)
if nextPageURL(headers) == "" {
break
}
page++
if page > MaxPages {
return nil, fmt.Errorf("pagination limit exceeded: stopped after %d pages", MaxPages)
}
}
return allIssues, nil
}
// ListSubIssues retrieves the sub-issues of the given parent issue number,
// following pagination so callers see every existing sub-issue.
func (c *Client) ListSubIssues(ctx context.Context, parentNumber int) ([]Issue, error) {
endpoint := fmt.Sprintf("%s%s/issues/%d/sub_issues", c.BaseURL, c.repoPath(), parentNumber)
issues, err := c.listIssuePages(ctx, endpoint)
if err != nil {
return nil, fmt.Errorf("failed to list sub-issues of #%d: %w", parentNumber, err)
}
return issues, nil
}
// AddSubIssue makes subIssueID a sub-issue of the issue at parentNumber.
// subIssueID is the sub-issue's internal numeric ID (Issue.ID), not its number.
func (c *Client) AddSubIssue(ctx context.Context, parentNumber, subIssueID int) error {
urlStr := fmt.Sprintf("%s%s/issues/%d/sub_issues", c.BaseURL, c.repoPath(), parentNumber)
body := map[string]interface{}{"sub_issue_id": subIssueID}
if _, _, err := c.doRequest(ctx, http.MethodPost, urlStr, body); err != nil {
return fmt.Errorf("failed to add sub-issue %d to #%d: %w", subIssueID, parentNumber, err)
}
return nil
}
// ListBlockedBy retrieves the issues that block the given issue number,
// following pagination so callers see every existing dependency.
func (c *Client) ListBlockedBy(ctx context.Context, number int) ([]Issue, error) {
endpoint := fmt.Sprintf("%s%s/issues/%d/dependencies/blocked_by", c.BaseURL, c.repoPath(), number)
issues, err := c.listIssuePages(ctx, endpoint)
if err != nil {
return nil, fmt.Errorf("failed to list blocking issues for #%d: %w", number, err)
}
return issues, nil
}
// AddBlockedBy records that the issue at number is blocked by blockingIssueID.
// blockingIssueID is the blocking issue's internal numeric ID (Issue.ID), not its number.
func (c *Client) AddBlockedBy(ctx context.Context, number, blockingIssueID int) error {
urlStr := fmt.Sprintf("%s%s/issues/%d/dependencies/blocked_by", c.BaseURL, c.repoPath(), number)
body := map[string]interface{}{"issue_id": blockingIssueID}
if _, _, err := c.doRequest(ctx, http.MethodPost, urlStr, body); err != nil {
return fmt.Errorf("failed to add blocked_by %d to #%d: %w", blockingIssueID, number, err)
}
return nil
}
// ListRepositories retrieves repositories accessible to the authenticated user.
func (c *Client) ListRepositories(ctx context.Context) ([]Repository, error) {
var allRepos []Repository

View file

@ -3,6 +3,7 @@ package github
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
@ -23,6 +24,27 @@ const (
headerRateLimitResource = "X-RateLimit-Resource"
)
// APIError is a non-retryable GitHub API failure that carries the HTTP status
// code, so callers can branch on it instead of matching error strings (e.g.
// treating 404 from the relationship endpoints as "unavailable here" rather
// than as a hard failure). Its message is unchanged from the untyped error it
// replaces.
type APIError struct {
StatusCode int
Body string
URL string
}
func (e *APIError) Error() string {
return fmt.Sprintf("API error: %s (status %d)", e.Body, e.StatusCode)
}
// IsNotFound reports whether err is, or wraps, a GitHub 404.
func IsNotFound(err error) bool {
var apiErr *APIError
return errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusNotFound
}
// AuthError indicates a GitHub 403/401 that is not a rate limit (bad token,
// missing scopes, IP allowlist, etc.). Auth errors are not retried.
type AuthError struct {

448
internal/github/links.go Normal file
View file

@ -0,0 +1,448 @@
package github
import (
"context"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"github.com/steveyegge/beads/internal/types"
)
const (
githubLinkSubIssue = "sub_issue"
githubLinkBlockedBy = "blocked_by"
)
// RefScope resolves beads external refs to issue numbers in exactly one GitHub
// repository. GitHub's sub-issue and issue-dependency endpoints are
// repository-scoped and take bare issue numbers, so a ref pointing at another
// repository — or at another host entirely, e.g. a GitLab or GHES URL — must
// not yield a number here: it would link whichever unrelated issues happen to
// carry those numbers in the configured repository.
type RefScope struct {
apiHost string // canonical host[:port] of the REST API base URL
webHost string // canonical host[:port] used by issue HTML URLs
owner string
repo string
}
// SubIssueLinkFromParentChild is retained for existing callers. Relationship
// sync itself uses the scoped RefScope method below.
func SubIssueLinkFromParentChild(issue *types.Issue, parent *types.IssueWithDependencyMetadata) (DependencyLink, bool) {
return NewRefScope("https://github.com", "o", "r").SubIssueLinkFromParentChild(issue, parent)
}
// BlockedByLinkFromBeadsDependency is retained for existing callers.
func BlockedByLinkFromBeadsDependency(issue *types.Issue, dep *types.IssueWithDependencyMetadata) (DependencyLink, bool) {
return NewRefScope("https://github.com", "o", "r").BlockedByLinkFromBeadsDependency(issue, dep)
}
// NewRefScope builds a ref scope for a repository reachable at the given REST
// API base URL.
func NewRefScope(baseURL, owner, repo string) RefScope {
apiHost := hostFromURL(baseURL)
webHost := apiHost
// api.github.com serves the REST API for github.com, while the issue HTML
// URLs that BuildExternalRef stores use the bare host. GitHub Enterprise
// serves both from one host, so this trim is a no-op there.
if trimmed := strings.TrimPrefix(apiHost, "api."); trimmed != "" {
webHost = trimmed
}
return RefScope{
apiHost: apiHost,
webHost: webHost,
owner: strings.ToLower(strings.TrimSpace(owner)),
repo: strings.ToLower(strings.TrimSpace(repo)),
}
}
func hostFromURL(raw string) string {
trimmed := strings.TrimSpace(raw)
if trimmed == "" {
return ""
}
u, err := url.Parse(trimmed)
if err != nil || u.Host == "" {
return ""
}
return strings.ToLower(u.Host)
}
// IssueNumberFromRef extracts a repository-scoped GitHub issue number from a
// beads external ref, but only when the ref points at this scope's repository.
// A full issue URL must match the configured host and owner/repo; the
// repo-less "github:{digits}" shorthand that BuildExternalRef emits when no
// URL is available is read as the configured repository. Everything else —
// another repository, another host, a non-GitHub tracker URL — is rejected.
func (s RefScope) IssueNumberFromRef(ref string) (int, bool) {
ref = strings.TrimSpace(ref)
if ref == "" || s.owner == "" || s.repo == "" {
return 0, false
}
if m := ghShorthandPattern.FindStringSubmatch(ref); len(m) >= 2 {
n, err := strconv.Atoi(m[1])
return n, err == nil && n > 0
}
u, err := url.Parse(ref)
if err != nil || u.Host == "" {
return 0, false
}
if host := strings.ToLower(u.Host); host != s.apiHost && host != s.webHost {
return 0, false
}
owner, repo, number, ok := splitIssueURLPath(u.Path)
if !ok {
return 0, false
}
if !strings.EqualFold(owner, s.owner) || !strings.EqualFold(repo, s.repo) {
return 0, false
}
return number, true
}
// splitIssueURLPath pulls owner, repo, and issue number out of a GitHub issue
// URL path. It accepts both the HTML form (/{owner}/{repo}/issues/42) and the
// REST form (/repos/{owner}/{repo}/issues/42, optionally behind a GitHub
// Enterprise /api/v3 prefix), and requires the number to be the last segment.
func splitIssueURLPath(path string) (owner, repo string, number int, ok bool) {
segments := strings.Split(strings.Trim(path, "/"), "/")
if len(segments) < 4 {
return "", "", 0, false
}
last := len(segments) - 1
if segments[last-1] != "issues" {
return "", "", 0, false
}
n, err := strconv.Atoi(segments[last])
if err != nil || n <= 0 {
return "", "", 0, false
}
owner, repo = segments[last-3], segments[last-2]
if owner == "" || repo == "" {
return "", "", 0, false
}
return owner, repo, n, true
}
// DependencyLink is a GitHub relationship operation derived from a beads
// parent-child link or "blocks" dependency. FromNumber is the issue the
// relationship is created on (the parent for sub_issue, the blocked issue for
// blocked_by); ToNumber is the other side (the child, or the blocker).
type DependencyLink struct {
FromNumber int
ToNumber int
LinkType string // "sub_issue" or "blocked_by"
FromBeadsID string
ToBeadsID string
}
// PushLinkOptions configures dependency link push behavior.
type PushLinkOptions struct {
DryRun bool
OnPlan func(DependencyLink)
}
// PushLinkResult summarizes a PushLinks pass. UnsupportedSkipped counts
// relationships skipped because GitHub answered 404 for the relationship
// endpoint itself — the sub-issue and issue-dependency APIs are absent on
// older GitHub Enterprise Server versions. SourceMissing counts relationships
// skipped because the source issue 404s: deleted, renumbered, or not visible
// to this token. The two are counted apart because a host that cannot do
// relationships at all and a stale local ref call for different operator
// responses; the caller emits one curated line per class instead of a raw
// error per link. Errors holds genuine failures, at most one per source issue.
type PushLinkResult struct {
Created int
UnsupportedSkipped int
SourceMissing int
Errors []error
}
// LinkResolver handles GitHub relationship convergence (sub-issues and issue
// dependencies) for one repository.
type LinkResolver struct {
Client *Client
scope RefScope
}
// NewLinkResolver creates a GitHub dependency link resolver whose ref scope is
// the client's repository.
func NewLinkResolver(client *Client) *LinkResolver {
r := &LinkResolver{Client: client}
if client != nil {
r.scope = NewRefScope(client.BaseURL, client.Owner, client.Repo)
}
return r
}
// Scope returns the repository that external refs must point at to take part
// in relationship sync.
func (r *LinkResolver) Scope() RefScope {
if r == nil {
return RefScope{}
}
return r.scope
}
// SubIssueLinkFromParentChild converts one beads parent-child dependency into
// a GitHub sub-issue link. parent must be the issue that issue's
// GetDependenciesWithMetadata resolved via a DepParentChild edge. Both refs
// must resolve inside this scope's repository.
func (s RefScope) SubIssueLinkFromParentChild(issue *types.Issue, parent *types.IssueWithDependencyMetadata) (DependencyLink, bool) {
if issue == nil || parent == nil || issue.ExternalRef == nil || parent.ExternalRef == nil {
return DependencyLink{}, false
}
childNumber, ok := s.IssueNumberFromRef(*issue.ExternalRef)
if !ok {
return DependencyLink{}, false
}
parentNumber, ok := s.IssueNumberFromRef(*parent.ExternalRef)
if !ok || parentNumber == childNumber {
return DependencyLink{}, false
}
return DependencyLink{
FromNumber: parentNumber,
ToNumber: childNumber,
LinkType: githubLinkSubIssue,
FromBeadsID: parent.ID,
ToBeadsID: issue.ID,
}, true
}
// BlockedByLinkFromBeadsDependency converts one beads "blocks" dependency into
// a GitHub blocked_by link. For a beads blocks edge issue -> dep (issue
// depends on / is blocked by dep), GitHub records: issue is blocked_by dep.
// Both refs must resolve inside this scope's repository.
func (s RefScope) BlockedByLinkFromBeadsDependency(issue *types.Issue, dep *types.IssueWithDependencyMetadata) (DependencyLink, bool) {
if issue == nil || dep == nil || issue.ExternalRef == nil || dep.ExternalRef == nil {
return DependencyLink{}, false
}
if dep.DependencyType != types.DepBlocks {
return DependencyLink{}, false
}
issueNumber, ok := s.IssueNumberFromRef(*issue.ExternalRef)
if !ok {
return DependencyLink{}, false
}
depNumber, ok := s.IssueNumberFromRef(*dep.ExternalRef)
if !ok || depNumber == issueNumber {
return DependencyLink{}, false
}
return DependencyLink{
FromNumber: issueNumber,
ToNumber: depNumber,
LinkType: githubLinkBlockedBy,
FromBeadsID: issue.ID,
ToBeadsID: dep.ID,
}, true
}
type githubLinkKey struct {
FromNumber int
ToNumber int
LinkType string
}
func (l DependencyLink) key() githubLinkKey {
return githubLinkKey{FromNumber: l.FromNumber, ToNumber: l.ToNumber, LinkType: l.LinkType}
}
type githubLinkSourceKey struct {
Number int
LinkType string
}
// DeduplicateLinks removes duplicate desired GitHub links and returns them in
// a deterministic order.
func DeduplicateLinks(links []DependencyLink) []DependencyLink {
if len(links) == 0 {
return nil
}
result := make([]DependencyLink, 0, len(links))
seen := make(map[githubLinkKey]struct{}, len(links))
for _, link := range links {
key := link.key()
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
result = append(result, link)
}
sort.Slice(result, func(i, j int) bool {
if result[i].FromNumber != result[j].FromNumber {
return result[i].FromNumber < result[j].FromNumber
}
if result[i].ToNumber != result[j].ToNumber {
return result[i].ToNumber < result[j].ToNumber
}
return result[i].LinkType < result[j].LinkType
})
return result
}
// PushLinks creates missing GitHub relationships (sub-issues, issue
// dependencies) for the desired link set. It is additive and idempotent:
// each source issue's current relationships of the relevant type are fetched
// once and consulted before any create call, so re-running a sync does not
// re-POST relationships that already exist. Stale remote relationships are
// left untouched.
func (r *LinkResolver) PushLinks(ctx context.Context, desired []DependencyLink, opts PushLinkOptions) PushLinkResult {
if r == nil || r.Client == nil {
return PushLinkResult{Errors: []error{fmt.Errorf("GitHub link resolver has no client")}}
}
desired = DeduplicateLinks(desired)
if len(desired) == 0 {
return PushLinkResult{}
}
sources := make(map[githubLinkSourceKey]*githubLinkSourceState)
idByNumber := make(map[int]int)
// One source issue can carry both link types, so the 404 classification
// probe below is cached by issue number rather than by source key.
missingByNumber := make(map[int]bool)
var result PushLinkResult
for _, link := range desired {
srcKey := githubLinkSourceKey{Number: link.FromNumber, LinkType: link.LinkType}
state, ok := sources[srcKey]
if !ok {
// One list call per (source issue, link type), and one error per
// failed source rather than one per link hanging off it.
targets, err := r.fetchCurrentTargets(ctx, link.FromNumber, link.LinkType)
state = &githubLinkSourceState{targets: targets}
if err != nil {
state.failed = true
if IsNotFound(err) {
// A 404 here has two causes that need different reporting:
// the relationship API is absent on this host, or the
// source issue itself is gone — deleted, renumbered, or
// hidden from this token. Reading the issue separates
// them. Only a clean 404 from that read proves the issue
// is the missing half; anything else (including a probe
// that succeeds or fails for another reason) leaves the
// skip classified as an unavailable endpoint.
state.missing = r.sourceIssueMissing(ctx, link.FromNumber, missingByNumber)
state.notFound = !state.missing
} else {
result.Errors = append(result.Errors, fmt.Errorf("fetch GitHub %s for #%d: %w", link.LinkType, link.FromNumber, err))
}
}
sources[srcKey] = state
}
if state.failed {
switch {
case state.missing:
result.SourceMissing++
case state.notFound:
result.UnsupportedSkipped++
}
continue
}
current := state.targets
// current is keyed by issue number (present on the list response), so
// the existence check needs no extra API call. The target's internal
// numeric ID is only resolved when we're about to actually create the
// link.
if _, exists := current[link.ToNumber]; exists {
continue
}
if opts.DryRun {
if opts.OnPlan != nil {
opts.OnPlan(link)
}
result.Created++
continue
}
targetID, ok := idByNumber[link.ToNumber]
if !ok {
issue, err := r.Client.FetchIssueByNumber(ctx, link.ToNumber)
if err != nil {
result.Errors = append(result.Errors, fmt.Errorf("resolve GitHub issue #%d: %w", link.ToNumber, err))
continue
}
targetID = issue.ID
idByNumber[link.ToNumber] = targetID
}
var err error
switch link.LinkType {
case githubLinkSubIssue:
err = r.Client.AddSubIssue(ctx, link.FromNumber, targetID)
case githubLinkBlockedBy:
err = r.Client.AddBlockedBy(ctx, link.FromNumber, targetID)
default:
continue
}
if err != nil {
if IsNotFound(err) {
// Unambiguously the endpoint here, unlike the list call above:
// this source issue's relationships listed successfully and
// the target was just fetched by number, so neither issue can
// be the missing half. Counted, not error-spammed.
result.UnsupportedSkipped++
continue
}
result.Errors = append(result.Errors, fmt.Errorf("create GitHub %s link #%d -> #%d: %w", link.LinkType, link.FromNumber, link.ToNumber, err))
continue
}
current[link.ToNumber] = struct{}{}
result.Created++
}
return result
}
// githubLinkSourceState caches one source issue's existing relationships of a
// single link type, or the fact that listing them failed. On a 404, notFound
// and missing record which of the two causes the probe settled on.
type githubLinkSourceState struct {
targets map[int]struct{}
failed bool
notFound bool
missing bool
}
// sourceIssueMissing reports whether issue number itself 404s, which is what
// separates a stale local ref from a host whose relationship endpoints are
// absent. Results are memoized in cache so a source issue carrying both link
// types is probed once.
func (r *LinkResolver) sourceIssueMissing(ctx context.Context, number int, cache map[int]bool) bool {
if missing, ok := cache[number]; ok {
return missing
}
_, err := r.Client.FetchIssueByNumber(ctx, number)
missing := err != nil && IsNotFound(err)
cache[number] = missing
return missing
}
func (r *LinkResolver) fetchCurrentTargets(ctx context.Context, number int, linkType string) (map[int]struct{}, error) {
var issues []Issue
var err error
switch linkType {
case githubLinkSubIssue:
issues, err = r.Client.ListSubIssues(ctx, number)
case githubLinkBlockedBy:
issues, err = r.Client.ListBlockedBy(ctx, number)
default:
return nil, fmt.Errorf("unknown GitHub link type %q", linkType)
}
if err != nil {
return nil, err
}
result := make(map[int]struct{}, len(issues))
for _, iss := range issues {
result[iss.Number] = struct{}{}
}
return result, nil
}

View file

@ -0,0 +1,298 @@
package github
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
func TestSubIssueLinkFromParentChild(t *testing.T) {
child := githubIssue("bd-child", "https://github.com/o/r/issues/10", types.TypeTask)
parent := githubDep("bd-epic", "https://github.com/o/r/issues/5", types.DepParentChild)
link, ok := SubIssueLinkFromParentChild(child, parent)
if !ok {
t.Fatal("SubIssueLinkFromParentChild returned false")
}
if link.FromNumber != 5 || link.ToNumber != 10 || link.LinkType != githubLinkSubIssue {
t.Fatalf("link = %+v, want parent #5 sub_issue #10", link)
}
// Same issue number should not produce a self-referential link.
same := githubDep("bd-epic", "https://github.com/o/r/issues/10", types.DepParentChild)
if _, ok := SubIssueLinkFromParentChild(child, same); ok {
t.Fatal("self-referential parent link should not be produced")
}
}
func TestBlockedByLinkFromBeadsDependency(t *testing.T) {
issue := githubIssue("bd-a", "https://github.com/o/r/issues/10", types.TypeTask)
blocker := githubDep("bd-b", "https://github.com/o/r/issues/20", types.DepBlocks)
link, ok := BlockedByLinkFromBeadsDependency(issue, blocker)
if !ok {
t.Fatal("BlockedByLinkFromBeadsDependency returned false")
}
if link.FromNumber != 10 || link.ToNumber != 20 || link.LinkType != githubLinkBlockedBy {
t.Fatalf("link = %+v, want #10 blocked_by #20", link)
}
related := githubDep("bd-c", "https://github.com/o/r/issues/30", types.DepRelatesTo)
if _, ok := BlockedByLinkFromBeadsDependency(issue, related); ok {
t.Fatal("non-blocks dependency type should not produce a blocked_by link")
}
}
func TestDeduplicateLinks(t *testing.T) {
links := []DependencyLink{
{FromNumber: 10, ToNumber: 20, LinkType: githubLinkBlockedBy},
{FromNumber: 10, ToNumber: 20, LinkType: githubLinkBlockedBy},
{FromNumber: 5, ToNumber: 10, LinkType: githubLinkSubIssue},
}
got := DeduplicateLinks(links)
if len(got) != 2 {
t.Fatalf("deduped links len = %d, want 2", len(got))
}
}
func TestPushLinksAddsMissing(t *testing.T) {
var posted bool
var capturedBody map[string]interface{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/dependencies/blocked_by"):
_ = json.NewEncoder(w).Encode([]Issue{})
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/issues/20"):
_ = json.NewEncoder(w).Encode(Issue{ID: 2000, Number: 20})
case r.Method == http.MethodPost && strings.Contains(r.URL.Path, "/issues/10/dependencies/blocked_by"):
posted = true
_ = json.NewDecoder(r.Body).Decode(&capturedBody)
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(Issue{ID: 2000, Number: 20})
default:
t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)
}
}))
defer server.Close()
resolver := NewLinkResolver(NewClient("token", "o", "r").WithBaseURL(server.URL))
res := resolver.PushLinks(context.Background(), []DependencyLink{
{FromNumber: 10, ToNumber: 20, LinkType: githubLinkBlockedBy},
}, PushLinkOptions{})
if len(res.Errors) != 0 {
t.Fatalf("PushLinks errors = %v", res.Errors)
}
if res.Created != 1 || !posted {
t.Fatalf("Created = %d, posted = %v, want one created POST", res.Created, posted)
}
if int(capturedBody["issue_id"].(float64)) != 2000 {
t.Fatalf("issue_id = %v, want 2000", capturedBody["issue_id"])
}
}
func TestPushLinksIdempotentExistingLink(t *testing.T) {
var posts int
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/sub_issues"):
_ = json.NewEncoder(w).Encode([]Issue{{ID: 2000, Number: 20}})
case r.Method == http.MethodPost:
posts++
t.Fatalf("unexpected POST for existing link")
default:
t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)
}
}))
defer server.Close()
resolver := NewLinkResolver(NewClient("token", "o", "r").WithBaseURL(server.URL))
res := resolver.PushLinks(context.Background(), []DependencyLink{
{FromNumber: 5, ToNumber: 20, LinkType: githubLinkSubIssue},
}, PushLinkOptions{})
if len(res.Errors) != 0 {
t.Fatalf("PushLinks errors = %v", res.Errors)
}
if res.Created != 0 || posts != 0 {
t.Fatalf("Created = %d, posts = %d, want no created links", res.Created, posts)
}
}
func TestPushLinksDryRunDoesNotPost(t *testing.T) {
var posts int
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/sub_issues"):
_ = json.NewEncoder(w).Encode([]Issue{})
case r.Method == http.MethodPost:
posts++
t.Fatalf("unexpected POST during dry-run")
default:
t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)
}
}))
defer server.Close()
var planned []DependencyLink
resolver := NewLinkResolver(NewClient("token", "o", "r").WithBaseURL(server.URL))
res := resolver.PushLinks(context.Background(), []DependencyLink{
{FromNumber: 5, ToNumber: 20, LinkType: githubLinkSubIssue},
}, PushLinkOptions{
DryRun: true,
OnPlan: func(link DependencyLink) {
planned = append(planned, link)
},
})
if len(res.Errors) != 0 {
t.Fatalf("PushLinks errors = %v", res.Errors)
}
if res.Created != 1 || posts != 0 || len(planned) != 1 {
t.Fatalf("Created = %d, posts = %d, planned = %d; want one dry-run plan and no POST", res.Created, posts, len(planned))
}
}
func githubIssue(id, ref string, issueType types.IssueType) *types.Issue {
return &types.Issue{
ID: id,
ExternalRef: &ref,
IssueType: issueType,
Status: types.StatusOpen,
}
}
func githubDep(id, ref string, depType types.DependencyType) *types.IssueWithDependencyMetadata {
return &types.IssueWithDependencyMetadata{
Issue: *githubIssue(id, ref, types.TypeTask),
DependencyType: depType,
}
}
// The 404 degradation path: a relationship endpoint that is absent on this
// host and a source issue that is gone both surface as 404 on the list call,
// but they are different operator problems and must not be counted together.
func TestPushLinksUnavailableEndpointCountsUnsupported(t *testing.T) {
var probes int
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/dependencies/blocked_by"):
w.WriteHeader(http.StatusNotFound)
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/issues/10"):
// The source issue is readable, so the 404 above is the endpoint.
probes++
_ = json.NewEncoder(w).Encode(Issue{ID: 1000, Number: 10})
default:
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusInternalServerError)
}
}))
defer server.Close()
resolver := NewLinkResolver(NewClient("token", "o", "r").WithBaseURL(server.URL))
res := resolver.PushLinks(context.Background(), []DependencyLink{
{FromNumber: 10, ToNumber: 20, LinkType: githubLinkBlockedBy},
}, PushLinkOptions{})
if len(res.Errors) != 0 {
t.Fatalf("PushLinks errors = %v, want none (degradation is counted, not raised)", res.Errors)
}
if res.UnsupportedSkipped != 1 {
t.Fatalf("UnsupportedSkipped = %d, want 1", res.UnsupportedSkipped)
}
if res.SourceMissing != 0 {
t.Fatalf("SourceMissing = %d, want 0 - the source issue was readable", res.SourceMissing)
}
if res.Created != 0 {
t.Fatalf("Created = %d, want 0", res.Created)
}
if probes != 1 {
t.Fatalf("source issue probed %d times, want 1", probes)
}
}
func TestPushLinksMissingSourceIssueIsNotUnsupported(t *testing.T) {
var probes int
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/dependencies/blocked_by"):
w.WriteHeader(http.StatusNotFound)
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/sub_issues"):
w.WriteHeader(http.StatusNotFound)
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/issues/10"):
// The source issue itself is gone: deleted, renumbered, or hidden.
probes++
w.WriteHeader(http.StatusNotFound)
default:
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusInternalServerError)
}
}))
defer server.Close()
resolver := NewLinkResolver(NewClient("token", "o", "r").WithBaseURL(server.URL))
res := resolver.PushLinks(context.Background(), []DependencyLink{
{FromNumber: 10, ToNumber: 20, LinkType: githubLinkBlockedBy},
{FromNumber: 10, ToNumber: 30, LinkType: githubLinkSubIssue},
}, PushLinkOptions{})
if len(res.Errors) != 0 {
t.Fatalf("PushLinks errors = %v, want none", res.Errors)
}
if res.SourceMissing != 2 {
t.Fatalf("SourceMissing = %d, want 2", res.SourceMissing)
}
if res.UnsupportedSkipped != 0 {
t.Fatalf("UnsupportedSkipped = %d, want 0 - the endpoint was never shown to be absent", res.UnsupportedSkipped)
}
// Both link types hang off issue #10, and the classification is a property
// of the issue, so one probe answers for both.
if probes != 1 {
t.Fatalf("source issue probed %d times, want 1", probes)
}
}
func TestPushLinksCreateNotFoundCountsUnsupported(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/dependencies/blocked_by"):
_ = json.NewEncoder(w).Encode([]Issue{})
case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/issues/20"):
_ = json.NewEncoder(w).Encode(Issue{ID: 2000, Number: 20})
case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/dependencies/blocked_by"):
// Listing worked and the target resolved, so a 404 here can only
// be the create endpoint.
w.WriteHeader(http.StatusNotFound)
default:
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusInternalServerError)
}
}))
defer server.Close()
resolver := NewLinkResolver(NewClient("token", "o", "r").WithBaseURL(server.URL))
res := resolver.PushLinks(context.Background(), []DependencyLink{
{FromNumber: 10, ToNumber: 20, LinkType: githubLinkBlockedBy},
}, PushLinkOptions{})
if len(res.Errors) != 0 {
t.Fatalf("PushLinks errors = %v, want none", res.Errors)
}
if res.UnsupportedSkipped != 1 {
t.Fatalf("UnsupportedSkipped = %d, want 1", res.UnsupportedSkipped)
}
if res.SourceMissing != 0 {
t.Fatalf("SourceMissing = %d, want 0", res.SourceMissing)
}
if res.Created != 0 {
t.Fatalf("Created = %d, want 0", res.Created)
}
}

View file

@ -0,0 +1,59 @@
package github
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestListRelationshipPages(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
page := r.URL.Query().Get("page")
if page == "1" {
w.Header().Set("Link", "<"+"http://"+r.Host+r.URL.Path+"?page=2&per_page=100>; rel=\"next\"")
}
if page == "2" {
_ = json.NewEncoder(w).Encode([]Issue{{Number: 2}})
return
}
_ = json.NewEncoder(w).Encode([]Issue{{Number: 1}})
}))
defer server.Close()
client := NewClient("token", "owner", "repo").WithBaseURL(server.URL)
for _, get := range []struct {
name string
fn func(context.Context) ([]Issue, error)
}{
{"sub-issues", func(ctx context.Context) ([]Issue, error) { return client.ListSubIssues(ctx, 7) }},
{"blocked-by", func(ctx context.Context) ([]Issue, error) { return client.ListBlockedBy(ctx, 7) }},
} {
t.Run(get.name, func(t *testing.T) {
issues, err := get.fn(context.Background())
if err != nil {
t.Fatal(err)
}
if len(issues) != 2 || issues[0].Number != 1 || issues[1].Number != 2 {
t.Fatalf("issues = %+v, want both pages", issues)
}
})
}
}
func TestRefScopeRejectsForeignHostAndRepository(t *testing.T) {
scope := NewRefScope("https://api.github.com", "owner", "repo")
for _, ref := range []string{
"https://gitlab.com/owner/repo/issues/12",
"https://github.com/other/repo/issues/12",
"https://github.com/owner/other/issues/12",
} {
if number, ok := scope.IssueNumberFromRef(ref); ok {
t.Fatalf("IssueNumberFromRef(%q) = %d, true; want rejected", ref, number)
}
}
if number, ok := scope.IssueNumberFromRef("https://github.com/owner/repo/issues/12"); !ok || number != 12 {
t.Fatalf("configured ref = %d, %v; want 12, true", number, ok)
}
}

View file

@ -34,6 +34,17 @@ type Tracker struct {
store tracker.Store
}
// LinkResolver returns a relationship resolver bound to this tracker's
// repository, or nil when the tracker has not been initialized. Callers get
// the resolver (and, through it, the repository ref scope) rather than the raw
// REST client, so relationship sync cannot reach past the endpoints it needs.
func (t *Tracker) LinkResolver() *LinkResolver {
if t.client == nil {
return nil
}
return NewLinkResolver(t.client)
}
func (t *Tracker) Name() string { return "github" }
func (t *Tracker) DisplayName() string { return "GitHub" }
func (t *Tracker) ConfigPrefix() string { return "github" }
@ -227,6 +238,16 @@ func (t *Tracker) ExtractIdentifier(ref string) string {
return matches[1]
}
// RefScope returns the repository scope used to resolve external refs to issue
// numbers for relationship sync. Refs are deliberately not resolved by issue
// number alone: see RefScope.
func (t *Tracker) RefScope() RefScope {
if t.client == nil {
return RefScope{}
}
return NewRefScope(t.client.BaseURL, t.client.Owner, t.client.Repo)
}
func (t *Tracker) BuildExternalRef(issue *tracker.TrackerIssue) string {
if issue.URL != "" {
return issue.URL