feat(commits): add commit pull request lookup
Some checks failed
Security Scan / security (push) Has been cancelled
Test / test (push) Has been cancelled

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 01:55:27 +00:00
parent 07241a502b
commit 720e2f2e0a
2 changed files with 50 additions and 0 deletions

View file

@ -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))

View file

@ -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 {