From 720e2f2e0a14d76ab4be2ebb5114ea7d2f3e26b2 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 01:55:27 +0000 Subject: [PATCH] feat(commits): add commit pull request lookup Co-Authored-By: Virgil --- commits.go | 15 +++++++++++++++ commits_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/commits.go b/commits.go index efb1a18..d4efcec 100644 --- a/commits.go +++ b/commits.go @@ -53,6 +53,21 @@ func (s *CommitService) Get(ctx context.Context, params Params) (*types.Commit, return &out, nil } +// GetPullRequest returns the pull request associated with a commit SHA. +// +// Usage: +// +// f := forge.NewForge("https://forge.lthn.ai", "token") +// _, err := f.Commits.GetPullRequest(ctx, "core", "go-forge", "abc123") +func (s *CommitService) GetPullRequest(ctx context.Context, owner, repo, sha string) (*types.PullRequest, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/commits/{sha}/pull", pathParams("owner", owner, "repo", repo, "sha", sha)) + var out types.PullRequest + if err := s.client.Get(ctx, path, &out); err != nil { + return nil, err + } + return &out, nil +} + // GetCombinedStatus returns the combined status for a given ref (branch, tag, or SHA). func (s *CommitService) GetCombinedStatus(ctx context.Context, owner, repo, ref string) (*types.CombinedStatus, error) { path := ResolvePath("/api/v1/repos/{owner}/{repo}/statuses/{ref}", pathParams("owner", owner, "repo", repo, "ref", ref)) diff --git a/commits_test.go b/commits_test.go index 5d57f6d..f9fbf2b 100644 --- a/commits_test.go +++ b/commits_test.go @@ -95,6 +95,41 @@ func TestCommitService_Get_Good(t *testing.T) { } } +func TestCommitService_GetPullRequest_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/repos/core/go-forge/commits/abc123/pull" { + t.Errorf("wrong path: %s", r.URL.Path) + } + json.NewEncoder(w).Encode(types.PullRequest{ + ID: 17, + Index: 9, + Title: "Add commit-linked pull request", + Head: &types.PRBranchInfo{ + Ref: "feature/commit-link", + }, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + pr, err := f.Commits.GetPullRequest(context.Background(), "core", "go-forge", "abc123") + if err != nil { + t.Fatal(err) + } + if pr.ID != 17 { + t.Errorf("got id=%d, want 17", pr.ID) + } + if pr.Index != 9 { + t.Errorf("got index=%d, want 9", pr.Index) + } + if pr.Head == nil || pr.Head.Ref != "feature/commit-link" { + t.Fatalf("unexpected head branch info: %+v", pr.Head) + } +} + func TestCommitService_ListStatuses_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {