From 933dc982f766522240297f80c2eb3ee43e4c16f1 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 07:29:00 +0000 Subject: [PATCH] feat(forge): expose user agent metadata Co-Authored-By: Virgil --- client.go | 9 +++++++++ client_test.go | 3 +++ docs/api-contract.md | 3 +++ forge.go | 7 +++++++ forge_test.go | 7 +++++++ 5 files changed, 29 insertions(+) diff --git a/client.go b/client.go index eeb1174..e3c2230 100644 --- a/client.go +++ b/client.go @@ -141,6 +141,15 @@ func (c *Client) RateLimit() RateLimit { return c.rateLimit } +// UserAgent returns the configured User-Agent header value. +// +// Usage: +// +// ua := client.UserAgent() +func (c *Client) UserAgent() string { + return c.userAgent +} + // NewClient creates a new Forgejo API client. // // Usage: diff --git a/client_test.go b/client_test.go index 25cf217..1f3b6e3 100644 --- a/client_test.go +++ b/client_test.go @@ -195,6 +195,9 @@ func TestClient_Options_Good(t *testing.T) { if c.userAgent != "go-forge/1.0" { t.Errorf("got user agent=%q", c.userAgent) } + if got := c.UserAgent(); got != "go-forge/1.0" { + t.Errorf("got UserAgent()=%q", got) + } } func TestClient_WithHTTPClient_Good(t *testing.T) { diff --git a/docs/api-contract.md b/docs/api-contract.md index 434d392..c135ef0 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -98,6 +98,7 @@ Coverage notes: rows list direct tests when a symbol is named in test names or r | method | Client.PostRaw | `func (c *Client) PostRaw(ctx context.Context, path string, body any) ([]byte, error)` | PostRaw performs a POST request with a JSON body and returns the raw response body as bytes instead of JSON-decoding. Useful for endpoints such as /markdown that return raw HTML text. | No direct tests. | | method | Client.Put | `func (c *Client) Put(ctx context.Context, path string, body, out any) error` | Put performs a PUT request. | No direct tests. | | method | Client.RateLimit | `func (c *Client) RateLimit() RateLimit` | RateLimit returns the last known rate limit information. | `TestClient_Good_RateLimit` | +| method | Client.UserAgent | `func (c *Client) UserAgent() string` | UserAgent returns the configured User-Agent header value. | `TestClient_Good_Options` | | method | CommitService.CreateStatus | `func (s *CommitService) CreateStatus(ctx context.Context, owner, repo, sha string, opts *types.CreateStatusOption) (*types.CommitStatus, error)` | CreateStatus creates a new commit status for the given SHA. | `TestCommitService_Good_CreateStatus` | | method | CommitService.Get | `func (s *CommitService) Get(ctx context.Context, params Params) (*types.Commit, error)` | Get returns a single commit by SHA or ref. | `TestCommitService_Good_Get`, `TestCommitService_Good_List` | | method | CommitService.GetCombinedStatus | `func (s *CommitService) GetCombinedStatus(ctx context.Context, owner, repo, ref string) (*types.CombinedStatus, error)` | GetCombinedStatus returns the combined status for a given ref (branch, tag, or SHA). | `TestCommitService_Good_GetCombinedStatus` | @@ -112,6 +113,8 @@ Coverage notes: rows list direct tests when a symbol is named in test names or r | method | ContentService.GetRawFile | `func (s *ContentService) GetRawFile(ctx context.Context, owner, repo, filepath string) ([]byte, error)` | GetRawFile returns the raw file content as bytes. | `TestContentService_Bad_GetRawNotFound`, `TestContentService_Good_GetRawFile` | | method | ContentService.UpdateFile | `func (s *ContentService) UpdateFile(ctx context.Context, owner, repo, filepath string, opts *types.UpdateFileOptions) (*types.FileResponse, error)` | UpdateFile updates an existing file in a repository. | `TestContentService_Good_UpdateFile` | | method | Forge.Client | `func (f *Forge) Client() *Client` | Client returns the underlying HTTP client. | `TestForge_Good_Client` | +| method | Forge.RateLimit | `func (f *Forge) RateLimit() RateLimit` | RateLimit returns the last known rate limit information. | `TestForge_Good_RateLimit` | +| method | Forge.UserAgent | `func (f *Forge) UserAgent() string` | UserAgent returns the configured User-Agent header value. | `TestForge_Good_UserAgent` | | method | IssueService.AddLabels | `func (s *IssueService) AddLabels(ctx context.Context, owner, repo string, index int64, labelIDs []int64) error` | AddLabels adds labels to an issue. | No direct tests. | | method | IssueService.AddReaction | `func (s *IssueService) AddReaction(ctx context.Context, owner, repo string, index int64, reaction string) error` | AddReaction adds a reaction to an issue. | No direct tests. | | method | IssueService.CreateComment | `func (s *IssueService) CreateComment(ctx context.Context, owner, repo string, index int64, body string) (*types.Comment, error)` | CreateComment creates a comment on an issue. | `TestIssueService_Good_CreateComment` | diff --git a/forge.go b/forge.go index 3c70143..f8ca6be 100644 --- a/forge.go +++ b/forge.go @@ -85,3 +85,10 @@ func (f *Forge) BaseURL() string { return f.client.BaseURL() } // // rl := f.RateLimit() func (f *Forge) RateLimit() RateLimit { return f.client.RateLimit() } + +// UserAgent returns the configured User-Agent header value. +// +// Usage: +// +// ua := f.UserAgent() +func (f *Forge) UserAgent() string { return f.client.UserAgent() } diff --git a/forge_test.go b/forge_test.go index ad5fc58..f47bac9 100644 --- a/forge_test.go +++ b/forge_test.go @@ -52,6 +52,13 @@ func TestForge_RateLimit_Good(t *testing.T) { } } +func TestForge_UserAgent_Good(t *testing.T) { + f := NewForge("https://forge.lthn.ai", "tok", WithUserAgent("go-forge/1.0")) + if got := f.UserAgent(); got != "go-forge/1.0" { + t.Fatalf("got user agent %q", got) + } +} + func TestRepoService_ListOrgRepos_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {