Skip to content

Commit fda28d2

Browse files
committed
Addressing review comments
1 parent 6d3051d commit fda28d2

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

pkg/github/repositories.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ func GetFileContents(t translations.TranslationHelperFunc) inventory.ServerTool
683683
return utils.NewToolResultError("failed to get GitHub client"), nil, nil
684684
}
685685

686-
rawOpts, err := resolveGitReference(ctx, client, owner, repo, ref, sha)
686+
rawOpts, fallbackUsed, err := resolveGitReference(ctx, client, owner, repo, ref, sha)
687687
if err != nil {
688688
return utils.NewToolResultError(fmt.Sprintf("failed to resolve git reference: %s", err)), nil, nil
689689
}
@@ -751,7 +751,7 @@ func GetFileContents(t translations.TranslationHelperFunc) inventory.ServerTool
751751

752752
// main branch ref passed in ref parameter but it doesn't exist - default branch was used
753753
var successNote string
754-
if !strings.HasSuffix(rawOpts.Ref, originalRef) {
754+
if fallbackUsed {
755755
successNote = fmt.Sprintf(" Note: the provided ref '%s' does not exist, default branch '%s' was used instead.", originalRef, rawOpts.Ref)
756756
}
757757

@@ -1884,15 +1884,15 @@ func looksLikeSHA(s string) bool {
18841884
//
18851885
// Any unexpected (non-404) errors during the resolution process are returned
18861886
// immediately. All API errors are logged with rich context to aid diagnostics.
1887-
func resolveGitReference(ctx context.Context, githubClient *github.Client, owner, repo, ref, sha string) (*raw.ContentOpts, error) {
1887+
func resolveGitReference(ctx context.Context, githubClient *github.Client, owner, repo, ref, sha string) (*raw.ContentOpts, bool, error) {
18881888
// 1) If SHA explicitly provided, it's the highest priority.
18891889
if sha != "" {
1890-
return &raw.ContentOpts{Ref: "", SHA: sha}, nil
1890+
return &raw.ContentOpts{Ref: "", SHA: sha}, false, nil
18911891
}
18921892

18931893
// 1a) If sha is empty but ref looks like a SHA, return it without changes
18941894
if looksLikeSHA(ref) {
1895-
return &raw.ContentOpts{Ref: "", SHA: ref}, nil
1895+
return &raw.ContentOpts{Ref: "", SHA: ref}, false, nil
18961896
}
18971897

18981898
originalRef := ref // Keep original ref for clearer error messages down the line.
@@ -1901,13 +1901,14 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
19011901
var reference *github.Reference
19021902
var resp *github.Response
19031903
var err error
1904+
var fallbackUsed bool
19041905

19051906
switch {
19061907
case originalRef == "":
19071908
// 2a) If ref is empty, determine the default branch.
19081909
reference, err = resolveDefaultBranch(ctx, githubClient, owner, repo)
19091910
if err != nil {
1910-
return nil, err // Error is already wrapped in resolveDefaultBranch.
1911+
return nil, false, err // Error is already wrapped in resolveDefaultBranch.
19111912
}
19121913
ref = reference.GetRef()
19131914
case strings.HasPrefix(originalRef, "refs/"):
@@ -1937,23 +1938,24 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
19371938
if originalRef == "main" {
19381939
reference, err = resolveDefaultBranch(ctx, githubClient, owner, repo)
19391940
if err != nil {
1940-
return nil, err // Error is already wrapped in resolveDefaultBranch.
1941+
return nil, false, err // Error is already wrapped in resolveDefaultBranch.
19411942
}
19421943
// Update ref to the actual default branch ref so the note can be generated
19431944
ref = reference.GetRef()
1945+
fallbackUsed = true
19441946
break
19451947
}
1946-
return nil, fmt.Errorf("could not resolve ref %q as a branch or a tag", originalRef)
1948+
return nil, false, fmt.Errorf("could not resolve ref %q as a branch or a tag", originalRef)
19471949
}
19481950

19491951
// The tag lookup failed for a different reason.
19501952
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (tag)", resp, err)
1951-
return nil, fmt.Errorf("failed to get reference for tag '%s': %w", originalRef, err)
1953+
return nil, false, fmt.Errorf("failed to get reference for tag '%s': %w", originalRef, err)
19521954
}
19531955
} else {
19541956
// The branch lookup failed for a different reason.
19551957
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get reference (branch)", resp, err)
1956-
return nil, fmt.Errorf("failed to get reference for branch '%s': %w", originalRef, err)
1958+
return nil, false, fmt.Errorf("failed to get reference for branch '%s': %w", originalRef, err)
19571959
}
19581960
}
19591961
}
@@ -1964,19 +1966,20 @@ func resolveGitReference(ctx context.Context, githubClient *github.Client, owner
19641966
if ref == "refs/heads/main" {
19651967
reference, err = resolveDefaultBranch(ctx, githubClient, owner, repo)
19661968
if err != nil {
1967-
return nil, err // Error is already wrapped in resolveDefaultBranch.
1969+
return nil, false, err // Error is already wrapped in resolveDefaultBranch.
19681970
}
19691971
// Update ref to the actual default branch ref so the note can be generated
19701972
ref = reference.GetRef()
1973+
fallbackUsed = true
19711974
} else {
19721975
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get final reference", resp, err)
1973-
return nil, fmt.Errorf("failed to get final reference for %q: %w", ref, err)
1976+
return nil, false, fmt.Errorf("failed to get final reference for %q: %w", ref, err)
19741977
}
19751978
}
19761979
}
19771980

19781981
sha = reference.GetObject().GetSHA()
1979-
return &raw.ContentOpts{Ref: ref, SHA: sha}, nil
1982+
return &raw.ContentOpts{Ref: ref, SHA: sha}, fallbackUsed, nil
19801983
}
19811984

19821985
func resolveDefaultBranch(ctx context.Context, githubClient *github.Client, owner, repo string) (*github.Reference, error) {
@@ -1985,15 +1988,23 @@ func resolveDefaultBranch(ctx context.Context, githubClient *github.Client, owne
19851988
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get repository info", resp, err)
19861989
return nil, fmt.Errorf("failed to get repository info: %w", err)
19871990
}
1991+
1992+
if resp != nil && resp.Body != nil {
1993+
_ = resp.Body.Close()
1994+
}
1995+
19881996
defaultBranch := repoInfo.GetDefaultBranch()
19891997

19901998
defaultRef, resp, err := githubClient.Git.GetRef(ctx, owner, repo, "heads/"+defaultBranch)
1991-
defer func() { _ = resp.Body.Close() }()
19921999
if err != nil {
19932000
_, _ = ghErrors.NewGitHubAPIErrorToCtx(ctx, "failed to get default branch reference", resp, err)
19942001
return nil, fmt.Errorf("failed to get default branch reference: %w", err)
19952002
}
19962003

2004+
if resp != nil && resp.Body != nil {
2005+
defer func() { _ = resp.Body.Close() }()
2006+
}
2007+
19972008
return defaultRef, nil
19982009
}
19992010

pkg/github/repositories_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3361,7 +3361,7 @@ func Test_resolveGitReference(t *testing.T) {
33613361
t.Run(tc.name, func(t *testing.T) {
33623362
// Setup client with mock
33633363
client := github.NewClient(tc.mockSetup())
3364-
opts, err := resolveGitReference(ctx, client, owner, repo, tc.ref, tc.sha)
3364+
opts, _, err := resolveGitReference(ctx, client, owner, repo, tc.ref, tc.sha)
33653365

33663366
if tc.expectError {
33673367
require.Error(t, err)

0 commit comments

Comments
 (0)