Add support for parsing GitHub API PR URLs

This commit is contained in:
Bernd Ahlers
2025-03-03 11:28:54 +01:00
parent 74102d8ccc
commit 0a410a3255
2 changed files with 9 additions and 1 deletions

View File

@@ -35,7 +35,14 @@ func ParseGitHubPRString(prString string) (string, int, error) {
var parts []string
if strings.HasPrefix(strings.ToLower(prString), "https://github.com/") && strings.Contains(prString, "/pull/") {
if strings.HasPrefix(strings.ToLower(prString), "https://api.github.com/") && strings.Contains(prString, "/pulls/") {
// Input is a PR API URL like this: https://api.github.com/repos/Graylog2/graylog2-server/pulls/9309
u, err := neturl.Parse(prString)
if err != nil {
return "", 0, errors.Wrapf(err, "couldn't parse GitHub pull request API URL <%s>", prString)
}
parts = strings.SplitN(strings.TrimPrefix(u.Path, "/repos/"), "/pulls/", 2)
} else if strings.HasPrefix(strings.ToLower(prString), "https://github.com/") && strings.Contains(prString, "/pull/") {
// Input is a PR URL like this: https://github.com/Graylog2/graylog2-server/pull/9692
u, err := neturl.Parse(prString)
if err != nil {

View File

@@ -162,6 +162,7 @@ func TestParseGitHubPRString(t *testing.T) {
}{
{"Graylog2/graylog2-server#123", "Graylog2/graylog2-server", 123, false},
{"https://github.com/Graylog2/graylog-plugin-collector/pull/9692", "Graylog2/graylog-plugin-collector", 9692, false},
{"https://api.github.com/repos/Graylog2/graylog2-server/pulls/9309", "Graylog2/graylog2-server", 9309, false},
{"https://github.com/Graylog2/graylog-plugin-collector/pull/", "", 0, true},
{"https://github.com/9692", "", 0, true},
{"https://example.com/Graylog2/graylog-plugin-collector/pull/9692", "", 0, true},