Skip to content

fix: Escape refs in commit, tag and archive paths - #4733

Merged
gmlewis merged 2 commits into
google:masterfrom
sohailshk:fix/ref-escape
Nov 26, 2026
Merged

gmlewis merged 2 commits into
google:masterfrom
sohailshk:fix/ref-escape

Conversation

@sohailshk

Copy link
Copy Markdown
Contributor

GetCommit, GetCommitRaw, GetReleaseByTag and GetArchiveLink (with opts.Ref) put the branch or tag name into the request path unescaped. A ref containing # or %, such as feature/#123 or 100%, is cut off as a fragment or makes NewRequest fail with invalid URL escape:

client.Repositories.GetCommit(ctx, "o", "r", "feature/#123", nil)  // requests /repos/o/r/commits/feature/
client.Repositories.GetReleaseByTag(ctx, "o", "r", "v1#rc")        // requests /repos/o/r/releases/tags/v1
client.Repositories.GetCommit(ctx, "o", "r", "100%", nil)          // error: invalid URL escape "%"

The fix runs the ref through refURLEscape, which GetCommitSHA1, GetCombinedStatus, ListStatuses and the check-run methods already use. It escapes each /-separated segment with url.PathEscape and keeps the / separators. The four doc comments now say so.

Note: a caller that pre-escaped the ref as a workaround will now have it escaped twice (50%25 is sent as 50%2525). GitService.GetTree also takes a ref and is left for a follow-up.

TestRepositoriesService_refIsEscaped covers the four methods with feature/x, fix#12, what?, 100%, a b and a pre-escaped 50%25. On master 16 of its 24 subtests fail; with the change all 24 pass. go test ./github passes.

The implementation and tests were written with AI assistance.

GetCommit, GetCommitRaw, GetReleaseByTag and GetArchiveLink put a branch or tag name into the request path as is, so a ref containing '#' or '%' was cut off as a fragment or rejected as an invalid URL escape. Use refURLEscape, as GetCommitSHA1 and GetCombinedStatus already do.
@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.64%. Comparing base (5c5bbe8) to head (caba1f8).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4733   +/-   ##
=======================================
  Coverage   98.64%   98.64%           
=======================================
  Files         204      204           
  Lines       19162    19164    +2     
=======================================
+ Hits        18902    18904    +2     
  Misses        260      260           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gmlewis gmlewis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @sohailshk!
LGTM.
Awaiting second LGTM+Approval from any other contributor to this repo before merging.

cc: @stevehipwell - @Not-Dhananjay-Mishra

Comment thread github/repos_commits.go Outdated

// GetCommit fetches the specified commit, including all details about it.
//
// Note: the ref is URL path escaped for you. Slashes are left as separators.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a comment to the method mentioning that sha can be a branch or tag ref as well?

@Not-Dhananjay-Mishra Not-Dhananjay-Mishra left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gmlewis gmlewis removed the NeedsReview PR is awaiting a review before merging. label Nov 26, 2026
@gmlewis

gmlewis commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Thank you, @Not-Dhananjay-Mishra!
Merging.

@gmlewis
gmlewis merged commit 4579bc4 into google:master Nov 26, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants