Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 deletions pkg/github/repositories_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,30 @@ 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.
// The chain is bounded: a repository can nest tags arbitrarily deep, and each
// level costs the caller an API request.
object := reference.GetObject()
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)
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
}

// 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 {
Expand Down
66 changes: 66 additions & 0 deletions pkg/github/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5555,6 +5555,72 @@ 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: "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",
Expand Down