Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions internal/tracker/forge_client.go
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
}

Comment thread
ralphbean marked this conversation as resolved.
Resolved
// 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)
Comment thread
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) {
Comment thread
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
}
76 changes: 76 additions & 0 deletions internal/tracker/tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Package tracker defines a narrow, forge-agnostic interface for reading
Comment thread
ralphbean marked this conversation as resolved.
Resolved
Comment thread
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
Comment thread
ralphbean marked this conversation as resolved.
Resolved
Author string
CreatedAt string
}

// Client abstracts issue-content read/write operations across trackers
Comment thread
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
Comment thread
ralphbean marked this conversation as resolved.
Resolved
}
167 changes: 167 additions & 0 deletions internal/tracker/tracker_test.go
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) {
Comment thread
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)
Loading