Skip to content

Commit 56a5f51

Browse files
committed
fix: harden github scm provider integration
1 parent f82f723 commit 56a5f51

10 files changed

Lines changed: 440 additions & 46 deletions

File tree

‎backend/internal/adapters/scm/github/provider.go‎

Lines changed: 89 additions & 37 deletions
Large diffs are not rendered by default.

‎backend/internal/adapters/scm/github/provider_test.go‎

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestRotatedETagOn304UpdatesCacheOnly(t *testing.T) {
9494
t.Fatal(err)
9595
}
9696
p := NewProvider(ProviderOptions{RESTBase: ts.URL, GraphQLURL: ts.URL + "/graphql", Token: StaticTokenSource("token")})
97-
changed, _, err := p.checkOpenPullListChanged(ctx, st, scope, "o", "r", time.Now())
97+
changed, _, _, err := p.checkOpenPullListChanged(ctx, st, scope, "o", "r", time.Now())
9898
if err != nil || changed {
9999
t.Fatalf("changed=%v err=%v", changed, err)
100100
}
@@ -321,6 +321,9 @@ func TestTruncatedGraphQLCIContextsFetchFullCheckRuns(t *testing.T) {
321321
})
322322
_ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"repository": map[string]any{"pr5": payload}}})
323323
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/check-runs"):
324+
if got := r.URL.Query().Get("per_page"); got != "100" {
325+
t.Fatalf("check-runs per_page=%q", got)
326+
}
324327
checkRunsCalls.Add(1)
325328
_, _ = w.Write([]byte(`{"check_runs":[{"name":"late-failure","status":"completed","conclusion":"failure","html_url":"https://checks/late"}]}`))
326329
case strings.Contains(r.URL.Path, "/comments"):
@@ -356,6 +359,9 @@ func TestFailingCIWithoutFailedContextFetchesFullCheckRuns(t *testing.T) {
356359
})
357360
_ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"repository": map[string]any{"pr5": payload}}})
358361
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/check-runs"):
362+
if got := r.URL.Query().Get("per_page"); got != "100" {
363+
t.Fatalf("check-runs per_page=%q", got)
364+
}
359365
checkRunsCalls.Add(1)
360366
_, _ = w.Write([]byte(`{"check_runs":[{"name":"hidden-failure","status":"completed","conclusion":"failure","html_url":"https://checks/hidden"}]}`))
361367
case strings.Contains(r.URL.Path, "/comments"):
@@ -444,6 +450,185 @@ func TestETagGuardsReuseLatestSnapshotAndSkipGraphQL(t *testing.T) {
444450
}
445451
}
446452

453+
func TestPRListGuardETagIsNotAdvancedWhenGraphQLFails(t *testing.T) {
454+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
455+
switch {
456+
case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/pulls":
457+
if got := r.Header.Get("If-None-Match"); got != `"old"` {
458+
t.Fatalf("If-None-Match=%q", got)
459+
}
460+
w.Header().Set("ETag", `"new"`)
461+
_, _ = w.Write([]byte(`[{"number":5}]`))
462+
case r.Method == http.MethodPost && r.URL.Path == "/graphql":
463+
w.WriteHeader(http.StatusInternalServerError)
464+
_, _ = w.Write([]byte(`{"message":"boom"}`))
465+
default:
466+
t.Fatalf("unexpected %s %s", r.Method, r.URL.Path)
467+
}
468+
}))
469+
defer ts.Close()
470+
ctx := context.Background()
471+
st := store.NewMemoryStore()
472+
subj := domain.SCMSubject{SessionID: "s1", ProjectID: "p1", Provider: domain.SCMProviderGitHub, Host: "github.com", Repo: "o/r", Branch: "feat/27", PRNumber: 5, CredentialHash: "cred"}
473+
snap := domain.SCMSnapshot{SessionID: "s1", Subject: subj, Freshness: domain.SCMFreshnessFresh, PR: &domain.SCMPullRequest{Number: 5, State: domain.PROpen, HeadSHA: "sha"}, CI: domain.SCMCI{Summary: "passing"}}
474+
if _, _, err := st.SaveSnapshot(ctx, snap); err != nil {
475+
t.Fatal(err)
476+
}
477+
key := domain.SCMProviderCacheKey{SCMProviderCacheScope: subj.CacheScope(), Namespace: cachePRList, Key: "open-guard"}
478+
if err := st.PutProviderCache(ctx, domain.SCMProviderCacheEntry{Key: key, ETag: `"old"`, Value: []byte(`[{"number":5}]`)}); err != nil {
479+
t.Fatal(err)
480+
}
481+
p := NewProvider(ProviderOptions{RESTBase: ts.URL, GraphQLURL: ts.URL + "/graphql", Token: StaticTokenSource("token")})
482+
if _, err := p.ObserveSessions(ctx, observeReq(subj), st); err == nil {
483+
t.Fatal("expected GraphQL error")
484+
}
485+
got, ok, err := st.GetProviderCache(ctx, key)
486+
if err != nil || !ok {
487+
t.Fatalf("cache ok=%v err=%v", ok, err)
488+
}
489+
if got.ETag != `"old"` {
490+
t.Fatalf("etag advanced on failed GraphQL: %q", got.ETag)
491+
}
492+
}
493+
494+
func TestReviewGuardETagIsNotAdvancedWhenReviewThreadsFail(t *testing.T) {
495+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
496+
switch {
497+
case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/pulls":
498+
w.Header().Set("ETag", `"pulls"`)
499+
w.WriteHeader(http.StatusNotModified)
500+
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/check-runs"):
501+
w.Header().Set("ETag", `"checks"`)
502+
w.WriteHeader(http.StatusNotModified)
503+
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/comments"):
504+
w.Header().Set("ETag", `"reviews-new"`)
505+
_, _ = w.Write([]byte(`[{"id":1}]`))
506+
case r.Method == http.MethodPost && r.URL.Path == "/graphql":
507+
var req struct {
508+
Query string `json:"query"`
509+
}
510+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
511+
t.Fatal(err)
512+
}
513+
if strings.Contains(req.Query, "reviewThreads") {
514+
w.WriteHeader(http.StatusInternalServerError)
515+
_, _ = w.Write([]byte(`{"message":"review down"}`))
516+
return
517+
}
518+
writeGraphQLPR(t, w, 5, "feat/27", "SUCCESS", "APPROVED", nil)
519+
default:
520+
t.Fatalf("unexpected %s %s", r.Method, r.URL.Path)
521+
}
522+
}))
523+
defer ts.Close()
524+
ctx := context.Background()
525+
st := store.NewMemoryStore()
526+
subj := domain.SCMSubject{SessionID: "s1", ProjectID: "p1", Provider: domain.SCMProviderGitHub, Host: "github.com", Repo: "o/r", Branch: "feat/27", PRNumber: 5, CredentialHash: "cred"}
527+
snap := domain.SCMSnapshot{SessionID: "s1", Subject: subj, Freshness: domain.SCMFreshnessFresh, PR: &domain.SCMPullRequest{Number: 5, State: domain.PROpen, HeadSHA: "sha"}, CI: domain.SCMCI{Summary: "passing"}, Review: domain.SCMReview{UnresolvedThreads: []domain.SCMReviewThread{{ID: "old"}}}}
528+
if _, _, err := st.SaveSnapshot(ctx, snap); err != nil {
529+
t.Fatal(err)
530+
}
531+
for _, entry := range []domain.SCMProviderCacheEntry{
532+
{Key: domain.SCMProviderCacheKey{SCMProviderCacheScope: subj.CacheScope(), Namespace: cachePRList, Key: "open-guard"}, ETag: `"pulls"`, Value: []byte(`[{"number":5}]`)},
533+
{Key: domain.SCMProviderCacheKey{SCMProviderCacheScope: subj.CacheScope(), Namespace: cacheCheckGuard, Key: "sha"}, ETag: `"checks"`},
534+
{Key: domain.SCMProviderCacheKey{SCMProviderCacheScope: subj.CacheScope(), Namespace: cacheReviews, Key: "5"}, ETag: `"reviews-old"`, Value: []byte(`[{"id":0}]`)},
535+
} {
536+
if err := st.PutProviderCache(ctx, entry); err != nil {
537+
t.Fatal(err)
538+
}
539+
}
540+
p := NewProvider(ProviderOptions{RESTBase: ts.URL, GraphQLURL: ts.URL + "/graphql", Token: StaticTokenSource("token")})
541+
res, err := p.ObserveSessions(ctx, observeReq(subj), st)
542+
if err != nil {
543+
t.Fatal(err)
544+
}
545+
if len(res.Snapshots) != 1 || len(res.Snapshots[0].Review.UnresolvedThreads) != 1 || res.Snapshots[0].Review.UnresolvedThreads[0].ID != "old" {
546+
t.Fatalf("expected previous review threads, got %+v", res.Snapshots)
547+
}
548+
key := domain.SCMProviderCacheKey{SCMProviderCacheScope: subj.CacheScope(), Namespace: cacheReviews, Key: "5"}
549+
got, ok, err := st.GetProviderCache(ctx, key)
550+
if err != nil || !ok {
551+
t.Fatalf("cache ok=%v err=%v", ok, err)
552+
}
553+
if got.ETag != `"reviews-old"` {
554+
t.Fatalf("review etag advanced on failed reviewThreads: %q", got.ETag)
555+
}
556+
}
557+
558+
func TestDiscoveryParseFailureDoesNotAdvanceCache(t *testing.T) {
559+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
560+
if r.Method != http.MethodGet || r.URL.Path != "/repos/o/r/pulls" {
561+
t.Fatalf("unexpected %s %s", r.Method, r.URL.Path)
562+
}
563+
w.Header().Set("ETag", `"new"`)
564+
_, _ = w.Write([]byte(`not-json`))
565+
}))
566+
defer ts.Close()
567+
ctx := context.Background()
568+
st := store.NewMemoryStore()
569+
scope := domain.SCMProviderCacheScope{Provider: domain.SCMProviderGitHub, Host: "github.com", Repo: "o/r", CredentialHash: "cred"}
570+
key := domain.SCMProviderCacheKey{SCMProviderCacheScope: scope, Namespace: cachePRList, Key: "open-discovery"}
571+
if err := st.PutProviderCache(ctx, domain.SCMProviderCacheEntry{Key: key, ETag: `"old"`, Value: []byte(`[]`)}); err != nil {
572+
t.Fatal(err)
573+
}
574+
p := NewProvider(ProviderOptions{RESTBase: ts.URL, GraphQLURL: ts.URL + "/graphql", Token: StaticTokenSource("token")})
575+
subj := domain.SCMSubject{SessionID: "s1", ProjectID: "p1", Provider: domain.SCMProviderGitHub, Host: "github.com", Repo: "o/r", Branch: "feat/27", CredentialHash: "cred"}
576+
if _, err := p.ObserveSessions(ctx, observeReq(subj), st); err == nil {
577+
t.Fatal("expected parse error")
578+
}
579+
got, ok, err := st.GetProviderCache(ctx, key)
580+
if err != nil || !ok {
581+
t.Fatalf("cache ok=%v err=%v", ok, err)
582+
}
583+
if got.ETag != `"old"` {
584+
t.Fatalf("etag advanced on parse failure: %q", got.ETag)
585+
}
586+
}
587+
588+
func TestPollStateConsecutiveFailuresIncrementAndReset(t *testing.T) {
589+
var fail atomic.Bool
590+
fail.Store(true)
591+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
592+
switch {
593+
case r.Method == http.MethodPost && r.URL.Path == "/graphql" && fail.Load():
594+
w.WriteHeader(http.StatusInternalServerError)
595+
_, _ = w.Write([]byte(`{"message":"down"}`))
596+
return
597+
case r.Method == http.MethodPost && r.URL.Path == "/graphql":
598+
writeGraphQLPR(t, w, 5, "feat/27", "SUCCESS", "APPROVED", nil)
599+
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/comments"):
600+
_, _ = w.Write([]byte(`[]`))
601+
default:
602+
t.Fatalf("unexpected %s %s", r.Method, r.URL.Path)
603+
}
604+
}))
605+
defer ts.Close()
606+
ctx := context.Background()
607+
st := store.NewMemoryStore()
608+
p := NewProvider(ProviderOptions{RESTBase: ts.URL, GraphQLURL: ts.URL + "/graphql", Token: StaticTokenSource("token")})
609+
subj := domain.SCMSubject{SessionID: "s1", ProjectID: "p1", Provider: domain.SCMProviderGitHub, Host: "github.com", Repo: "o/r", Branch: "feat/27", PRNumber: 5}
610+
for want := 1; want <= 2; want++ {
611+
res, err := p.ObserveSessions(ctx, observeReq(subj), st)
612+
if err == nil {
613+
t.Fatal("expected failure")
614+
}
615+
if got := res.PollStates[0].ConsecutiveFail; got != want {
616+
t.Fatalf("failure count=%d want=%d", got, want)
617+
}
618+
if err := st.PutPollState(ctx, res.PollStates[0]); err != nil {
619+
t.Fatal(err)
620+
}
621+
}
622+
fail.Store(false)
623+
res, err := p.ObserveSessions(ctx, observeReq(subj), st)
624+
if err != nil {
625+
t.Fatal(err)
626+
}
627+
if got := res.PollStates[0].ConsecutiveFail; got != 0 {
628+
t.Fatalf("failure count after success=%d", got)
629+
}
630+
}
631+
447632
func TestGraphQLBatchIsCappedAtTwentyFivePRs(t *testing.T) {
448633
var graphQLCalls atomic.Int32
449634
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

‎backend/internal/lifecycle/decide_bridge.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lifecycle
22

33
import (
4+
"strings"
45
"time"
56

67
"github.com/aoagents/agent-orchestrator/backend/internal/domain"
@@ -128,7 +129,16 @@ func classifyPendingComments(comments []ports.ReviewComment) (hasBot, hasHuman b
128129
}
129130

130131
func hasMergeConflicts(m ports.Mergeability) bool {
131-
return !m.Mergeable && !m.NoConflicts && (m.CIPassing || m.Approved || len(m.Blockers) > 0)
132+
if m.Conflict || m.BehindBase {
133+
return true
134+
}
135+
for _, blocker := range m.Blockers {
136+
b := strings.ToLower(blocker)
137+
if strings.Contains(b, "merge conflict") || strings.Contains(b, "behind base") {
138+
return true
139+
}
140+
}
141+
return false
132142
}
133143

134144
// ---- activity -> session axis mapping (activity owns working/idle/waiting) ----

‎backend/internal/lifecycle/manager_test.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ func TestApplySCMObservation(t *testing.T) {
325325
{"changes requested", ports.SCMFacts{Fetched: true, PRState: domain.PROpen, ReviewDecision: ports.ReviewChangesRequested}, domain.PRReasonChangesRequested, domain.StatusChangesRequested},
326326
{"pending human comments", ports.SCMFacts{Fetched: true, PRState: domain.PROpen, PendingComments: []ports.ReviewComment{{Author: "human", Body: "fix"}}}, domain.PRReasonChangesRequested, domain.StatusChangesRequested},
327327
{"pending bot comments", ports.SCMFacts{Fetched: true, PRState: domain.PROpen, PendingComments: []ports.ReviewComment{{Author: "bot", Body: "fix", IsBot: true}}}, domain.PRReasonBotComments, domain.StatusChangesRequested},
328-
{"merge conflicts", ports.SCMFacts{Fetched: true, PRState: domain.PROpen, Mergeability: ports.Mergeability{CIPassing: true, Approved: true, NoConflicts: false, Blockers: []string{"merge conflicts"}}}, domain.PRReasonMergeConflicts, domain.StatusPROpen},
328+
{"merge conflicts", ports.SCMFacts{Fetched: true, PRState: domain.PROpen, Mergeability: ports.Mergeability{CIPassing: true, Approved: true, NoConflicts: false, Conflict: true, Blockers: []string{"merge conflicts"}}}, domain.PRReasonMergeConflicts, domain.StatusPROpen},
329+
{"behind base", ports.SCMFacts{Fetched: true, PRState: domain.PROpen, Mergeability: ports.Mergeability{CIPassing: true, Approved: true, NoConflicts: false, BehindBase: true, Blockers: []string{"branch is behind base"}}}, domain.PRReasonMergeConflicts, domain.StatusPROpen},
330+
{"unknown mergeability is not conflict", ports.SCMFacts{Fetched: true, PRState: domain.PROpen, Mergeability: ports.Mergeability{NoConflicts: false, Unknown: true, Blockers: []string{"merge status unknown (GitHub is computing)"}}}, domain.PRReasonInProgress, domain.StatusPROpen},
329331
{"approved + mergeable", ports.SCMFacts{Fetched: true, PRState: domain.PROpen, ReviewDecision: ports.ReviewApproved, Mergeability: ports.Mergeability{Mergeable: true}}, domain.PRReasonMergeReady, domain.StatusMergeable},
330332
{"review pending", ports.SCMFacts{Fetched: true, PRState: domain.PROpen, ReviewDecision: ports.ReviewPending}, domain.PRReasonReviewPending, domain.StatusReviewPending},
331333
}

‎backend/internal/ports/facts.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ type Mergeability struct {
6161
CIPassing bool
6262
Approved bool
6363
NoConflicts bool
64+
Conflict bool
65+
BehindBase bool
66+
Unknown bool
6467
Blockers []string
6568
}
6669

‎backend/internal/scm/command/service.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ type AuditSink interface {
1616
}
1717

1818
// Refresher is satisfied by observer.Observer and intentionally mirrors the
19-
// small part commands need: invalidate caches and schedule a refresh.
19+
// small part commands need: schedule a refresh after provider-specific cache
20+
// invalidation has already been applied.
2021
type Refresher interface {
21-
Invalidate(ctx context.Context, subject domain.SCMSubject, reason string) error
22+
Refresh(ctx context.Context, subjects []domain.SCMSubject) error
2223
}
2324

2425
type cacheInvalidationProvider interface {
@@ -77,6 +78,9 @@ func (s *Service) run(ctx context.Context, sessionID domain.SessionID, cmd ports
7778
if !ok {
7879
return ports.SCMCommandResult{}, fmt.Errorf("scm command: subject %s not found", sessionID)
7980
}
81+
if subj.PRNumber == 0 {
82+
return ports.SCMCommandResult{}, &domain.SCMError{Kind: domain.SCMErrorNotFound, Operation: string(cmd), Message: "no change request bound to session"}
83+
}
8084
provider := s.Providers[subj.Provider]
8185
if provider == nil {
8286
return ports.SCMCommandResult{}, &domain.SCMError{Kind: domain.SCMErrorUnsupported, Operation: string(cmd), Message: fmt.Sprintf("provider %q not registered", subj.Provider)}
@@ -128,7 +132,7 @@ func (s *Service) run(ctx context.Context, sessionID domain.SessionID, cmd ports
128132
return res, err
129133
}
130134
if s.Refresh != nil {
131-
if err := s.Refresh.Invalidate(ctx, subj, string(cmd)); err != nil {
135+
if err := s.Refresh.Refresh(ctx, []domain.SCMSubject{subj}); err != nil {
132136
return res, err
133137
}
134138
}

‎backend/internal/scm/command/service_test.go‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
44
"context"
5+
"errors"
56
"testing"
67

78
"github.com/aoagents/agent-orchestrator/backend/internal/domain"
@@ -23,12 +24,15 @@ func (f *fakeCommandProvider) Merge(_ context.Context, r ports.SCMCommandRequest
2324
return ports.SCMCommandResult{Provider: domain.SCMProviderGitHub, Command: r.Command, ChangeRequest: r.ChangeRequest}, nil
2425
}
2526
func (f *fakeCommandProvider) Close(context.Context, ports.SCMCommandRequest) (ports.SCMCommandResult, error) {
27+
f.called = ports.SCMCommandClose
2628
return ports.SCMCommandResult{}, nil
2729
}
2830
func (f *fakeCommandProvider) Comment(context.Context, ports.SCMCommandRequest) (ports.SCMCommandResult, error) {
31+
f.called = ports.SCMCommandComment
2932
return ports.SCMCommandResult{}, nil
3033
}
3134
func (f *fakeCommandProvider) Assign(context.Context, ports.SCMCommandRequest) (ports.SCMCommandResult, error) {
35+
f.called = ports.SCMCommandAssign
3236
return ports.SCMCommandResult{}, nil
3337
}
3438
func (f *fakeCommandProvider) Checkout(_ context.Context, r ports.SCMCommandRequest) (ports.SCMCommandResult, error) {
@@ -41,7 +45,7 @@ func (f *fakeCommandProvider) CacheInvalidationPrefixes(domain.SCMSubject, ports
4145

4246
type fakeRefresh struct{ called bool }
4347

44-
func (f *fakeRefresh) Invalidate(context.Context, domain.SCMSubject, string) error {
48+
func (f *fakeRefresh) Refresh(context.Context, []domain.SCMSubject) error {
4549
f.called = true
4650
return nil
4751
}
@@ -102,3 +106,53 @@ func TestCheckoutDoesNotInvalidateOrRefreshProviderCache(t *testing.T) {
102106
t.Fatal("checkout should not trigger observer refresh")
103107
}
104108
}
109+
110+
func TestCommentInvalidatesOnlyProviderReviewCache(t *testing.T) {
111+
ctx := context.Background()
112+
st := store.NewMemoryStore()
113+
subj := domain.SCMSubject{SessionID: "s1", ProjectID: "p1", Provider: domain.SCMProviderGitHub, Host: "github.com", Repo: "o/r", Branch: "feat/27", PRNumber: 7, CredentialHash: "cred"}
114+
if err := st.UpsertSubject(ctx, subj); err != nil {
115+
t.Fatal(err)
116+
}
117+
reviewKey := domain.SCMProviderCacheKey{SCMProviderCacheScope: subj.CacheScope(), Namespace: "reviews", Key: "7"}
118+
checkKey := domain.SCMProviderCacheKey{SCMProviderCacheScope: subj.CacheScope(), Namespace: "checks", Key: "sha"}
119+
for _, key := range []domain.SCMProviderCacheKey{reviewKey, checkKey} {
120+
if err := st.PutProviderCache(ctx, domain.SCMProviderCacheEntry{Key: key, ETag: key.Namespace}); err != nil {
121+
t.Fatal(err)
122+
}
123+
}
124+
provider := &fakeCommandProvider{invalidations: []domain.SCMProviderCachePrefix{{SCMProviderCacheScope: subj.CacheScope(), Namespace: "reviews"}}}
125+
refresh := &fakeRefresh{}
126+
svc := New(st, refresh, provider)
127+
if _, err := svc.CommentOnChangeRequest(ctx, "s1", "hello"); err != nil {
128+
t.Fatal(err)
129+
}
130+
if _, ok, _ := st.GetProviderCache(ctx, reviewKey); ok {
131+
t.Fatal("review cache should be invalidated")
132+
}
133+
if _, ok, _ := st.GetProviderCache(ctx, checkKey); !ok {
134+
t.Fatal("check cache should remain after comment")
135+
}
136+
if !refresh.called {
137+
t.Fatal("comment should refresh after precise invalidation")
138+
}
139+
}
140+
141+
func TestCommandRejectsSessionWithoutBoundChangeRequest(t *testing.T) {
142+
ctx := context.Background()
143+
st := store.NewMemoryStore()
144+
subj := domain.SCMSubject{SessionID: "s1", ProjectID: "p1", Provider: domain.SCMProviderGitHub, Host: "github.com", Repo: "o/r", Branch: "feat/27"}
145+
if err := st.UpsertSubject(ctx, subj); err != nil {
146+
t.Fatal(err)
147+
}
148+
provider := &fakeCommandProvider{}
149+
svc := New(st, nil, provider)
150+
_, err := svc.MergeChangeRequest(ctx, "s1", ports.SCMCommandRequest{})
151+
var scmErr *domain.SCMError
152+
if !errors.As(err, &scmErr) || scmErr.Kind != domain.SCMErrorNotFound {
153+
t.Fatalf("err=%T %[1]v", err)
154+
}
155+
if provider.called != "" {
156+
t.Fatalf("provider should not be called, got %s", provider.called)
157+
}
158+
}

0 commit comments

Comments
 (0)