fix(watchpr): accept GitHub PR URLs with a trailing path, query or fragment

This commit is contained in:
2026-11-22 02:01:16 +11:00
parent c4471975a3
commit 2332cf4b5f
2 changed files with 11 additions and 4 deletions
+6 -3
View File
@@ -63,12 +63,15 @@ func ParsePRRef(s string) (PRRef, error) {
// ParseWatchRef parses a Gitea ref (see ParsePRRef) or a GitHub one:
// "github:owner/repo#N", "github:owner/repo:N" or
// "https://github.com/owner/repo/pull/N".
// "https://github.com/owner/repo/pull/N", where a trailing path ("/files"),
// query or fragment on the URL is ignored.
func ParseWatchRef(s string) (PRRef, error) {
s = strings.TrimSpace(s)
if rest, ok := strings.CutPrefix(s, githubPRURLPrefix); ok {
parts := strings.Split(strings.TrimSuffix(rest, "/"), "/")
if len(parts) != 4 || parts[2] != "pull" {
rest, _, _ = strings.Cut(rest, "#")
rest, _, _ = strings.Cut(rest, "?")
parts := strings.Split(rest, "/")
if len(parts) < 4 || parts[2] != "pull" {
return PRRef{}, fmt.Errorf("invalid GitHub PR URL %q: expected %sowner/repo/pull/N", s, githubPRURLPrefix)
}
s = "github:" + parts[0] + "/" + parts[1] + "#" + parts[3]
+5 -1
View File
@@ -192,6 +192,10 @@ func TestParseWatchRef(t *testing.T) {
{"github:IUCLabs/app:12", PRRef{Forge: ForgeGitHub, Owner: "IUCLabs", Repo: "app", Number: 12}},
{" https://github.com/IUCLabs/app/pull/12 ", PRRef{Forge: ForgeGitHub, Owner: "IUCLabs", Repo: "app", Number: 12}},
{"https://github.com/IUCLabs/app/pull/12/", PRRef{Forge: ForgeGitHub, Owner: "IUCLabs", Repo: "app", Number: 12}},
{"https://github.com/IUCLabs/app/pull/12/files", PRRef{Forge: ForgeGitHub, Owner: "IUCLabs", Repo: "app", Number: 12}},
{"https://github.com/IUCLabs/app/pull/12#issuecomment-1", PRRef{Forge: ForgeGitHub, Owner: "IUCLabs", Repo: "app", Number: 12}},
{"https://github.com/IUCLabs/app/pull/12?x=y", PRRef{Forge: ForgeGitHub, Owner: "IUCLabs", Repo: "app", Number: 12}},
{"https://github.com/IUCLabs/app/pull/12/files?w=1#diff-a/b", PRRef{Forge: ForgeGitHub, Owner: "IUCLabs", Repo: "app", Number: 12}},
}
for _, tt := range tests {
got, err := ParseWatchRef(tt.in)
@@ -201,7 +205,7 @@ func TestParseWatchRef(t *testing.T) {
}
for _, bad := range []string{
"github:IUCLabs#1", "github:IUCLabs/app#0", "https://github.com/IUCLabs/app/issues/3",
"https://github.com/IUCLabs/app/pull/3/files", "https://github.com/IUCLabs/app/pull/x", "gitlab:o/r#1",
"https://github.com/IUCLabs/app/pull/x", "https://github.com/IUCLabs/app/pull/x/files", "https://github.com/IUCLabs/app/pull", "https://github.com/IUCLabs/app/pull?x=1", "gitlab:o/r#1",
} {
if got, err := ParseWatchRef(bad); err == nil {
t.Errorf("ParseWatchRef(%q) = %+v, want error", bad, got)