-
Notifications
You must be signed in to change notification settings - Fork 8.4k
browse: disambiguate decimal-only short SHAs from issue numbers #13347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
When a selector argument passed to gh browse consists entirely of decimal digits and is at least 7 characters long, it satisfies both isNumber and isCommit. Previously isNumber was always checked first, so gh browse 3096289 would open an issues URL even when that string referred to an actual commit SHA. Only in the ambiguous case (decimal-only, no # prefix, passes both checks), call the commits API to determine whether the ref resolves to a real commit. If it does, open the commit URL; otherwise fall back to the issues URL. No API call is made for unambiguous inputs. Fixes #12357
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,6 +250,22 @@ func parseSection(baseRepo ghrepo.Interface, opts *BrowseOptions) (string, error | |
| return "", nil | ||
| } | ||
| if isNumber(opts.SelectorArg) { | ||
| // A selector without a "#" prefix that satisfies both isNumber and | ||
| // isCommit is ambiguous (e.g. "309628980"). Disambiguate via API. | ||
| if !strings.HasPrefix(opts.SelectorArg, "#") && isCommit(opts.SelectorArg) { | ||
| httpClient, err := opts.HttpClient() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| apiClient := api.NewClientFromHTTP(httpClient) | ||
| exists, err := api.CommitExists(apiClient, baseRepo, opts.SelectorArg) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if exists { | ||
| return fmt.Sprintf("commit/%s", opts.SelectorArg), nil | ||
| } | ||
|
Comment on lines
+255
to
+267
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is intentional. The maintainer explicitly chose correctness over graceful degradation in the issue discussion:
The issue author agreed. Silently guessing the wrong URL during an outage would be a worse user experience than surfacing the error — especially since |
||
| } | ||
| return fmt.Sprintf("issues/%s", strings.TrimPrefix(opts.SelectorArg, "#")), nil | ||
| } | ||
| if isCommit(opts.SelectorArg) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed — added direct unit tests for
CommitExistsinapi/queries_repo_test.go(second commit). Covers all four status code paths: 200 (exists), 404 (not found), 422 (unprocessable ref), and 500 (server error).