Repository navigation
refactor(#5988): introduce tracker.Client interface with forge adapter #5993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c0d07a4
feat(#5988): introduce tracker.Client interface with forge adapter
ralphbean 690c9a6
refactor(#5988): add number to tracker.Client.UpdateComment
ralphbean 70a15d6
fix(#5988): validate project string, fix tracker doc inconsistencies
ralphbean ebd1662
feat(#5988): add tracker.ErrNotFound to define Client's NotFound cont…
ralphbean fbcb35e
Merge branch 'main' into agent/5988-tracker-client
waynesun09 082fee8
fix: address review feedback on PR #5993
fullsend-ai-coder[bot] bed9407
fix: address review feedback on PR #5993
fullsend-ai-coder[bot] 2ec6124
fix(#5988): correct GitLab UpdateComment doc claim in ForgeClient
ralphbean 30a739b
fix(#5988): return NotFound from FakeClient.UpdateIssueComment, add t…
ralphbean 4633d49
fix(#5988): avoid stuttered "not found: not found" in wrapNotFound
ralphbean 4ebdbea
fix(#5988): give tracker.Comment/Issue.Body a named Body type
ralphbean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package tracker | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/fullsend-ai/fullsend/internal/forge" | ||
| ) | ||
|
|
||
| // ForgeClient adapts a forge.Client to the tracker.Client interface. It | ||
| // works for both the GitHub and GitLab forge.Client implementations, since | ||
| // forge.Client already abstracts over the two — this adapter only needs to | ||
| // split the tracker's single "project" string back into the owner/repo | ||
| // pair that forge.Client expects, and convert forge.IssueComment's numeric | ||
| // ID to the string form tracker.Comment uses. | ||
| type ForgeClient struct { | ||
| forge forge.Client | ||
| } | ||
|
|
||
| // NewForgeClient returns a tracker.Client backed by fc. | ||
| func NewForgeClient(fc forge.Client) *ForgeClient { | ||
| return &ForgeClient{forge: fc} | ||
| } | ||
|
|
||
| // GetIssue implements Client by splitting project into owner/repo for the | ||
| // underlying forge call. | ||
| func (c *ForgeClient) GetIssue(ctx context.Context, project string, number int) (*Issue, error) { | ||
| owner, repo, err := splitProject(project) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| issue, err := c.forge.GetIssue(ctx, owner, repo, number) | ||
| if err != nil { | ||
| return nil, wrapNotFound(err) | ||
| } | ||
| return &Issue{ | ||
| Number: issue.Number, | ||
| Title: issue.Title, | ||
| Body: issue.Body, | ||
| URL: issue.URL, | ||
| Labels: issue.Labels, | ||
| }, nil | ||
| } | ||
|
|
||
| // ListComments implements Client by splitting project into owner/repo for | ||
| // the underlying forge call. | ||
| func (c *ForgeClient) ListComments(ctx context.Context, project string, number int) ([]Comment, error) { | ||
| owner, repo, err := splitProject(project) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| comments, err := c.forge.ListIssueComments(ctx, owner, repo, number) | ||
| if err != nil { | ||
| return nil, wrapNotFound(err) | ||
| } | ||
| result := make([]Comment, len(comments)) | ||
| for i, fc := range comments { | ||
| result[i] = fromForgeComment(fc) | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| // CreateComment implements Client by splitting project into owner/repo for | ||
| // the underlying forge call. | ||
| func (c *ForgeClient) CreateComment(ctx context.Context, project string, number int, body string) (*Comment, error) { | ||
| owner, repo, err := splitProject(project) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| comment, err := c.forge.CreateIssueComment(ctx, owner, repo, number, body) | ||
| if err != nil { | ||
| return nil, wrapNotFound(err) | ||
| } | ||
| result := fromForgeComment(*comment) | ||
| return &result, nil | ||
| } | ||
|
|
||
| // UpdateComment implements Client. number is unused: forge.Client's | ||
| // UpdateIssueComment identifies the comment by ID alone (GitHub/GitLab | ||
| // comment IDs are globally unique within the repo), unlike Jira which | ||
| // needs the issue key too. | ||
| func (c *ForgeClient) UpdateComment(ctx context.Context, project string, number int, commentID string, body string) error { | ||
| owner, repo, err := splitProject(project) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| id, err := strconv.Atoi(commentID) | ||
| if err != nil { | ||
| return fmt.Errorf("tracker: comment ID %q is not numeric: %w", commentID, err) | ||
| } | ||
| return wrapNotFound(c.forge.UpdateIssueComment(ctx, owner, repo, id, body)) | ||
| } | ||
|
|
||
| // wrapNotFound translates a forge.ErrNotFound-satisfying error into one | ||
| // that also satisfies tracker.ErrNotFound, so ForgeClient upholds the | ||
| // Client interface's NotFound contract without leaking forge as part of | ||
| // tracker.Client's error surface. Non-NotFound errors, including nil, | ||
| // pass through unchanged. | ||
| func wrapNotFound(err error) error { | ||
| if !forge.IsNotFound(err) { | ||
| return err | ||
| } | ||
| return fmt.Errorf("%w: %w", ErrNotFound, err) | ||
|
ralphbean marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| func fromForgeComment(c forge.IssueComment) Comment { | ||
| return Comment{ | ||
| ID: strconv.Itoa(c.ID), | ||
| HTMLURL: c.HTMLURL, | ||
| Body: c.Body, | ||
| Author: c.Author, | ||
| CreatedAt: c.CreatedAt, | ||
| } | ||
| } | ||
|
|
||
| // splitProject splits "group/subgroup/project" into owner="group/subgroup" | ||
| // and repo="project". GitHub projects are always single-level | ||
| // ("owner/repo"), which this also handles correctly since there's only one | ||
| // "/". GitLab projects may be nested under subgroups, hence splitting on | ||
| // the last "/" rather than the first. | ||
| // | ||
| // It returns an error if project doesn't split into a non-empty owner and | ||
| // a non-empty repo, so callers don't silently forward malformed values | ||
| // (e.g. missing owner or repo) into forge.Client calls that require both. | ||
| func splitProject(project string) (owner, repo string, err error) { | ||
|
ralphbean marked this conversation as resolved.
Resolved
|
||
| idx := strings.LastIndex(project, "/") | ||
| if idx < 0 { | ||
| return "", "", fmt.Errorf("tracker: invalid project %q: expected \"owner/repo\"", project) | ||
| } | ||
| owner, repo = project[:idx], project[idx+1:] | ||
| if owner == "" || repo == "" { | ||
| return "", "", fmt.Errorf("tracker: invalid project %q: owner and repo must both be non-empty", project) | ||
| } | ||
| return owner, repo, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Package tracker defines a narrow, forge-agnostic interface for reading | ||
|
ralphbean marked this conversation as resolved.
Resolved
ralphbean marked this conversation as resolved.
Resolved
|
||
| // and writing issue content (title, body, comments), keyed by | ||
| // (project string, number int) rather than the (owner, repo string, number | ||
| // int) shape used by forge.Client. | ||
| // | ||
| // forge.Client already covers this surface for GitHub and GitLab, but it | ||
| // stays scoped to git-hosting operations — Jira is explicitly not a forge | ||
| // (it has no branches, pull requests, or CI). Keying by a single project | ||
| // string lets a future Jira implementation use its natural project key | ||
| // (e.g. "PROJECT") instead of forcing an owner/repo split that Jira | ||
| // doesn't have; the issue number is passed separately, as with GitHub and | ||
| // GitLab. | ||
| // | ||
| // This package only defines the interface and thin adapters over | ||
| // forge.Client (see ForgeClient). Nothing calls tracker.Client yet. | ||
| package tracker | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| ) | ||
|
|
||
| // ErrNotFound indicates a requested issue or comment was not found. | ||
| // Implementations of Client must return an error satisfying errors.Is(err, | ||
| // ErrNotFound) — checkable via IsNotFound — for missing resources, rather | ||
| // than requiring callers to reach into a specific tracker backend (e.g. | ||
| // forge.ErrNotFound) to detect this case. | ||
| var ErrNotFound = errors.New("not found") | ||
|
|
||
| // IsNotFound reports whether err indicates a requested issue or comment | ||
| // was not found. | ||
| func IsNotFound(err error) bool { | ||
| return errors.Is(err, ErrNotFound) | ||
| } | ||
|
|
||
| // Issue represents an issue's content, independent of the tracker backend. | ||
| type Issue struct { | ||
| Number int | ||
| Title string | ||
| Body string | ||
| URL string | ||
| Labels []string | ||
| } | ||
|
|
||
| // Comment represents a comment on an issue. | ||
| // | ||
| // ID is a string for JSON round-tripping safety and to allow for | ||
| // non-numeric IDs from a possible future tracker, even though GitHub, | ||
| // GitLab, and Jira comment IDs are all numeric under the hood. Callers | ||
| // that need to update a comment pass the ID back verbatim via | ||
| // UpdateComment. | ||
| type Comment struct { | ||
| ID string | ||
| HTMLURL string | ||
| Body string | ||
|
ralphbean marked this conversation as resolved.
Resolved
|
||
| Author string | ||
| CreatedAt string | ||
| } | ||
|
|
||
| // Client abstracts issue-content read/write operations across trackers | ||
|
ralphbean marked this conversation as resolved.
Resolved
|
||
| // (GitHub, GitLab, and eventually Jira). Project identifies the issue's | ||
| // container: "owner/repo" for GitHub/GitLab, a Jira project key for Jira. | ||
| // | ||
| // Implementations must return an error satisfying IsNotFound when the | ||
| // requested issue or comment doesn't exist. | ||
| type Client interface { | ||
| // GetIssue returns the issue identified by project and number. | ||
| GetIssue(ctx context.Context, project string, number int) (*Issue, error) | ||
| // ListComments returns all comments on the issue identified by project and number. | ||
| ListComments(ctx context.Context, project string, number int) ([]Comment, error) | ||
| // CreateComment adds a new comment with the given body to the issue. | ||
| CreateComment(ctx context.Context, project string, number int, body string) (*Comment, error) | ||
| // UpdateComment updates the body of commentID on the issue (project, number). | ||
| // number is included because Jira requires the issue key to update a comment. | ||
| UpdateComment(ctx context.Context, project string, number int, commentID string, body string) error | ||
|
ralphbean marked this conversation as resolved.
Resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package tracker | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/fullsend-ai/fullsend/internal/forge" | ||
| ) | ||
|
|
||
| func TestSplitProject(t *testing.T) { | ||
| tests := []struct { | ||
| input string | ||
| wantOwner string | ||
| wantRepo string | ||
| wantErr bool | ||
| }{ | ||
| {input: "org/project", wantOwner: "org", wantRepo: "project"}, | ||
| {input: "group/subgroup/project", wantOwner: "group/subgroup", wantRepo: "project"}, | ||
| {input: "project", wantErr: true}, | ||
| {input: "/repo", wantErr: true}, | ||
| {input: "owner/", wantErr: true}, | ||
| {input: "", wantErr: true}, | ||
| } | ||
| for _, tc := range tests { | ||
| owner, repo, err := splitProject(tc.input) | ||
| if tc.wantErr { | ||
| if err == nil { | ||
| t.Errorf("splitProject(%q) = (%q, %q, <nil>), want error", tc.input, owner, repo) | ||
| } | ||
| continue | ||
| } | ||
| if err != nil { | ||
| t.Errorf("splitProject(%q) returned unexpected error: %v", tc.input, err) | ||
| continue | ||
| } | ||
| if owner != tc.wantOwner || repo != tc.wantRepo { | ||
| t.Errorf("splitProject(%q) = (%q, %q), want (%q, %q)", | ||
| tc.input, owner, repo, tc.wantOwner, tc.wantRepo) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestForgeClient_GetIssue(t *testing.T) { | ||
| fc := forge.NewFakeClient() | ||
| fc.OpenIssues = map[string][]forge.Issue{ | ||
| "acme/widgets": { | ||
| {Number: 42, Title: "Widget is broken", Body: "details", URL: "https://example.com/42", Labels: []string{"bug"}}, | ||
| }, | ||
| } | ||
|
|
||
| c := NewForgeClient(fc) | ||
| issue, err := c.GetIssue(context.Background(), "acme/widgets", 42) | ||
| if err != nil { | ||
| t.Fatalf("GetIssue returned error: %v", err) | ||
| } | ||
| if issue.Number != 42 || issue.Title != "Widget is broken" || issue.Body != "details" || issue.URL != "https://example.com/42" { | ||
| t.Errorf("GetIssue returned unexpected issue: %+v", issue) | ||
| } | ||
| if len(issue.Labels) != 1 || issue.Labels[0] != "bug" { | ||
| t.Errorf("GetIssue returned unexpected labels: %+v", issue.Labels) | ||
| } | ||
| } | ||
|
|
||
| func TestForgeClient_GetIssue_NestedNamespace(t *testing.T) { | ||
| fc := forge.NewFakeClient() | ||
| fc.OpenIssues = map[string][]forge.Issue{ | ||
| "group/subgroup/project": { | ||
| {Number: 1, Title: "Nested"}, | ||
| }, | ||
| } | ||
|
|
||
| c := NewForgeClient(fc) | ||
| issue, err := c.GetIssue(context.Background(), "group/subgroup/project", 1) | ||
| if err != nil { | ||
| t.Fatalf("GetIssue returned error: %v", err) | ||
| } | ||
| if issue.Title != "Nested" { | ||
| t.Errorf("GetIssue returned unexpected issue: %+v", issue) | ||
| } | ||
| } | ||
|
|
||
| func TestForgeClient_GetIssue_NotFound(t *testing.T) { | ||
|
ralphbean marked this conversation as resolved.
Resolved
|
||
| fc := forge.NewFakeClient() | ||
| c := NewForgeClient(fc) | ||
| _, err := c.GetIssue(context.Background(), "acme/widgets", 99) | ||
| if !IsNotFound(err) { | ||
| t.Errorf("GetIssue error = %v, want tracker.ErrNotFound", err) | ||
| } | ||
| if !forge.IsNotFound(err) { | ||
| t.Errorf("GetIssue error = %v, want it to still satisfy forge.ErrNotFound", err) | ||
| } | ||
| } | ||
|
|
||
| func TestForgeClient_CreateAndListComments(t *testing.T) { | ||
| fc := forge.NewFakeClient() | ||
| fc.AuthenticatedUser = "fullsend-bot" | ||
|
|
||
| c := NewForgeClient(fc) | ||
| ctx := context.Background() | ||
|
|
||
| created, err := c.CreateComment(ctx, "acme/widgets", 42, "hello there") | ||
| if err != nil { | ||
| t.Fatalf("CreateComment returned error: %v", err) | ||
| } | ||
| if created.Body != "hello there" || created.Author != "fullsend-bot" || created.ID == "" { | ||
| t.Errorf("CreateComment returned unexpected comment: %+v", created) | ||
| } | ||
|
|
||
| comments, err := c.ListComments(ctx, "acme/widgets", 42) | ||
| if err != nil { | ||
| t.Fatalf("ListComments returned error: %v", err) | ||
| } | ||
| if len(comments) != 1 || comments[0].ID != created.ID || comments[0].Body != "hello there" { | ||
| t.Errorf("ListComments returned unexpected comments: %+v", comments) | ||
| } | ||
| } | ||
|
|
||
| func TestForgeClient_UpdateComment(t *testing.T) { | ||
| fc := forge.NewFakeClient() | ||
| c := NewForgeClient(fc) | ||
| ctx := context.Background() | ||
|
|
||
| created, err := c.CreateComment(ctx, "acme/widgets", 42, "original") | ||
| if err != nil { | ||
| t.Fatalf("CreateComment returned error: %v", err) | ||
| } | ||
|
|
||
| if err := c.UpdateComment(ctx, "acme/widgets", 42, created.ID, "updated"); err != nil { | ||
| t.Fatalf("UpdateComment returned error: %v", err) | ||
| } | ||
|
|
||
| comments, err := c.ListComments(ctx, "acme/widgets", 42) | ||
| if err != nil { | ||
| t.Fatalf("ListComments returned error: %v", err) | ||
| } | ||
| if len(comments) != 1 || comments[0].Body != "updated" { | ||
| t.Errorf("ListComments after update returned unexpected comments: %+v", comments) | ||
| } | ||
| } | ||
|
|
||
| func TestForgeClient_UpdateComment_InvalidID(t *testing.T) { | ||
| fc := forge.NewFakeClient() | ||
| c := NewForgeClient(fc) | ||
|
|
||
| err := c.UpdateComment(context.Background(), "acme/widgets", 42, "not-a-number", "updated") | ||
| if err == nil { | ||
| t.Fatal("UpdateComment with non-numeric ID should return an error") | ||
| } | ||
| } | ||
|
|
||
| // staticClient is a minimal tracker.Client implementation used to verify | ||
| // the interface shape independent of the forge adapter. | ||
| type staticClient struct{} | ||
|
|
||
| func (staticClient) GetIssue(_ context.Context, _ string, _ int) (*Issue, error) { return nil, nil } | ||
| func (staticClient) ListComments(_ context.Context, _ string, _ int) ([]Comment, error) { | ||
| return nil, nil | ||
| } | ||
| func (staticClient) CreateComment(_ context.Context, _ string, _ int, _ string) (*Comment, error) { | ||
| return nil, nil | ||
| } | ||
| func (staticClient) UpdateComment(_ context.Context, _ string, _ int, _ string, _ string) error { | ||
| return nil | ||
| } | ||
|
|
||
| var _ Client = staticClient{} | ||
| var _ Client = (*ForgeClient)(nil) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.