From 180d4775c27d62afd716a4544cbd176c8f14ab29 Mon Sep 17 00:00:00 2001 From: burrows99 Date: Tue, 24 Nov 2026 03:33:57 +0000 Subject: [PATCH 1/2] fix: resolve annotated tags to their commit in get_file_contents An annotated tag ref points at a tag object. resolveGitReference returned that object's SHA as if it were a commit, so get_file_contents built repo://.../sha//contents/... resource URIs that the server's own repository resource cannot read (404), and the resolved refs it reports name the tag object instead of the commit. Peel annotated tags with the Git tags API so the SHA names the tagged commit, as it already does for branches and lightweight tags. --- pkg/github/repositories_helper.go | 15 +++++++++++-- pkg/github/repositories_test.go | 35 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/pkg/github/repositories_helper.go b/pkg/github/repositories_helper.go index fe1983252c..c1bd495bf9 100644 --- a/pkg/github/repositories_helper.go +++ b/pkg/github/repositories_helper.go @@ -567,8 +567,19 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner } } - sha = reference.GetObject().GetSHA() - return &raw.ContentOpts{Ref: ref, SHA: sha}, fallbackUsed, nil + // An annotated tag ref points at a tag object, not a commit. Peel it so the + // returned SHA names the tagged commit, like a branch or lightweight tag. + object := reference.GetObject() + for object.GetType() == "tag" { + tag, resp, err := githubClient.Git.GetTag(ctx, owner, repo, object.GetSHA()) + if err != nil { + _, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get annotated tag", resp, err) + return nil, false, fmt.Errorf("failed to get annotated tag %q: %w", originalRef, err) + } + object = tag.GetObject() + } + + return &raw.ContentOpts{Ref: ref, SHA: object.GetSHA()}, fallbackUsed, nil } func resolveDefaultBranch(ctx context.Context, githubClient *github.Client, owner, repo string) (*github.Reference, error) { diff --git a/pkg/github/repositories_test.go b/pkg/github/repositories_test.go index 9b40c932ff..15d52d553b 100644 --- a/pkg/github/repositories_test.go +++ b/pkg/github/repositories_test.go @@ -5555,6 +5555,41 @@ func Test_resolveGitReference(t *testing.T) { }, expectError: false, }, + { + name: "annotated tag resolves to the tagged commit", + ref: "v1.0.0", + sha: "", + mockSetup: func() *http.Client { + return NewMockedHTTPClient( + WithRequestMatchHandler( + GetReposGitRefByOwnerByRepoByRef, + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.Contains(r.URL.Path, "/git/ref/heads/v1.0.0") { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + return + } + assert.Contains(t, r.URL.Path, "/git/ref/tags/v1.0.0") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"ref": "refs/tags/v1.0.0", "object": {"sha": "tag-object-sha", "type": "tag"}}`)) + }), + ), + WithRequestMatchHandler( + GetReposGitTagsByOwnerByRepoByTagSHA, + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Contains(t, r.URL.Path, "/git/tags/tag-object-sha") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"sha": "tag-object-sha", "object": {"sha": "commit-sha", "type": "commit"}}`)) + }), + ), + ) + }, + expectedOutput: &raw.ContentOpts{ + Ref: "refs/tags/v1.0.0", + SHA: "commit-sha", + }, + expectError: false, + }, { name: "heads/ prefix gets refs/ prepended", ref: "heads/feature-branch", From 6928b9841032535db66af9a20a601bf482ead10e Mon Sep 17 00:00:00 2001 From: burrows99 Date: Tue, 24 Nov 2026 15:23:13 +0000 Subject: [PATCH 2/2] fix: stop following annotated tags after 10 levels A repository can nest annotated tags arbitrarily deep, and each level costs the caller an API request. Return an error once the chain is deeper than maxAnnotatedTagDepth (10). --- pkg/github/repositories_helper.go | 11 ++++++++++- pkg/github/repositories_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/pkg/github/repositories_helper.go b/pkg/github/repositories_helper.go index c1bd495bf9..16e1ea5f1c 100644 --- a/pkg/github/repositories_helper.go +++ b/pkg/github/repositories_helper.go @@ -569,8 +569,13 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner // An annotated tag ref points at a tag object, not a commit. Peel it so the // returned SHA names the tagged commit, like a branch or lightweight tag. + // The chain is bounded: a repository can nest tags arbitrarily deep, and each + // level costs the caller an API request. object := reference.GetObject() - for object.GetType() == "tag" { + for depth := 0; object.GetType() == "tag"; depth++ { + if depth == maxAnnotatedTagDepth { + return nil, false, fmt.Errorf("annotated tag %q is nested more than %d levels deep", originalRef, maxAnnotatedTagDepth) + } tag, resp, err := githubClient.Git.GetTag(ctx, owner, repo, object.GetSHA()) if err != nil { _, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get annotated tag", resp, err) @@ -582,6 +587,10 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner return &raw.ContentOpts{Ref: ref, SHA: object.GetSHA()}, fallbackUsed, nil } +// maxAnnotatedTagDepth limits how many annotated tags are followed when a tag +// points at another tag. +const maxAnnotatedTagDepth = 10 + func resolveDefaultBranch(ctx context.Context, githubClient *github.Client, owner, repo string) (*github.Reference, error) { repoInfo, resp, err := githubClient.Repositories.Get(ctx, owner, repo) if err != nil { diff --git a/pkg/github/repositories_test.go b/pkg/github/repositories_test.go index 15d52d553b..e4376f3863 100644 --- a/pkg/github/repositories_test.go +++ b/pkg/github/repositories_test.go @@ -5590,6 +5590,37 @@ func Test_resolveGitReference(t *testing.T) { }, expectError: false, }, + { + name: "annotated tag nested too deep returns an error", + ref: "v1.0.0", + sha: "", + mockSetup: func() *http.Client { + return NewMockedHTTPClient( + WithRequestMatchHandler( + GetReposGitRefByOwnerByRepoByRef, + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.Contains(r.URL.Path, "/git/ref/heads/v1.0.0") { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + return + } + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"ref": "refs/tags/v1.0.0", "object": {"sha": "tag-object-sha", "type": "tag"}}`)) + }), + ), + WithRequestMatchHandler( + GetReposGitTagsByOwnerByRepoByTagSHA, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + // Every tag points at another tag. + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"sha": "tag-object-sha", "object": {"sha": "tag-object-sha", "type": "tag"}}`)) + }), + ), + ) + }, + expectError: true, + errorContains: "nested more than 10 levels deep", + }, { name: "heads/ prefix gets refs/ prepended", ref: "heads/feature-branch",