-
Notifications
You must be signed in to change notification settings - Fork 2
feat(git): add support for GitLab merge requests #704
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f112201
fix(git): resolve GitLab merge requests, not just GitHub PRs
skevetter 89ef50d
fix(git): satisfy goconst/golines linters in host tests
skevetter 8071c1a
fix(git): scope host detection to hostname and fallback to missing re…
skevetter 301ffa8
chore: update comments
skevetter a0184fe
fix: linting errors
skevetter 95f5c7a
fix: spacing
skevetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package git | ||
|
|
||
| import ( | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| var prNumberRegEx = regexp.MustCompile(`(?:pull|merge-requests)/([0-9]+)/head`) | ||
|
|
||
| const ( | ||
| hostNameGitHub = "github" | ||
| hostNameGitLab = "gitlab" | ||
| ) | ||
|
|
||
| // Host is a git provider's convention for referencing pull/merge requests: | ||
| // GitHub exposes them at pull/N/head, GitLab at merge-requests/N/head. | ||
| type Host struct { | ||
| Name string | ||
| refPrefix string | ||
| branchAbbr string | ||
| hostHint string // substring identifying the provider in a repository URL | ||
| } | ||
|
|
||
| var ( | ||
| HostGitHub = Host{ | ||
| Name: hostNameGitHub, | ||
| refPrefix: "pull", | ||
| branchAbbr: "PR", | ||
| hostHint: hostNameGitHub, | ||
| } | ||
| HostGitLab = Host{ | ||
| Name: hostNameGitLab, | ||
| refPrefix: "merge-requests", | ||
| branchAbbr: "MR", | ||
| hostHint: hostNameGitLab, | ||
| } | ||
|
|
||
| knownHosts = []Host{HostGitHub, HostGitLab} | ||
| ) | ||
|
|
||
| func (h Host) Refspec(number string) string { | ||
| return h.refPrefix + "/" + number + "/head" | ||
| } | ||
|
|
||
| func (h Host) BranchName(number string) string { | ||
| return h.branchAbbr + number | ||
| } | ||
|
|
||
| // DetectHost picks the provider from a repository URL, defaulting to GitHub. | ||
| func DetectHost(repoURL string) Host { | ||
| host := strings.ToLower(hostFromURL(repoURL)) | ||
| for _, h := range knownHosts { | ||
| if strings.Contains(host, h.hostHint) { | ||
| return h | ||
| } | ||
| } | ||
| return HostGitHub | ||
| } | ||
|
|
||
| // hostFromURL extracts the hostname from SSH (git@host:path) and URL | ||
| // (scheme://[user@]host/path) forms, returning "" when none is present. | ||
| func hostFromURL(repoURL string) string { | ||
| s := repoURL | ||
| if i := strings.Index(s, "://"); i != -1 { | ||
| s = s[i+3:] | ||
| } | ||
| if i := strings.Index(s, "@"); i != -1 { | ||
| s = s[i+1:] | ||
| } | ||
| if i := strings.IndexAny(s, "/:"); i != -1 { | ||
| s = s[:i] | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // hostForRef infers the provider from the ref itself. | ||
| func hostForRef(ref string) Host { | ||
| if strings.Contains(ref, HostGitLab.refPrefix+"/") { | ||
| return HostGitLab | ||
| } | ||
| return HostGitHub | ||
| } | ||
|
|
||
| func prNumber(ref string) string { | ||
| if m := prNumberRegEx.FindStringSubmatch(ref); m != nil { | ||
| return m[1] | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // prCandidates orders the hosts to try for a checkout. | ||
| func prCandidates(repoURL string) []Host { | ||
| primary := DetectHost(repoURL) | ||
| candidates := []Host{primary} | ||
| for _, h := range knownHosts { | ||
| if h.Name != primary.Name { | ||
| candidates = append(candidates, h) | ||
| } | ||
| } | ||
| return candidates | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package git | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "gotest.tools/assert" | ||
| "gotest.tools/assert/cmp" | ||
| ) | ||
|
|
||
| func TestDetectHost(t *testing.T) { | ||
| cases := []struct { | ||
| url string | ||
| want string | ||
| }{ | ||
| {"git@github.com:org/repo.git", hostNameGitHub}, | ||
| {"https://github.com/org/repo.git", hostNameGitHub}, | ||
| {"git@gitlab.com:org/repo.git", hostNameGitLab}, | ||
| {"https://gitlab.example.com/org/repo.git", hostNameGitLab}, | ||
| // Only the hostname decides: a "gitlab" owner/path on github.com is GitHub. | ||
| {"git@github.com:gitlab-org/repo.git", hostNameGitHub}, | ||
| {"https://github.com/org/gitlab-mirror.git", hostNameGitHub}, | ||
| { | ||
| "https://git.internal.example/org/repo.git", | ||
| hostNameGitHub, | ||
| }, // unknown host defaults to GitHub | ||
| } | ||
| for _, c := range cases { | ||
| assert.Check(t, cmp.Equal(c.want, DetectHost(c.url).Name), "url=%s", c.url) | ||
| } | ||
| } | ||
|
|
||
| func TestHostRefspecAndBranch(t *testing.T) { | ||
| assert.Equal(t, "pull/42/head", HostGitHub.Refspec("42")) | ||
| assert.Equal(t, "PR42", HostGitHub.BranchName("42")) | ||
| assert.Equal(t, "merge-requests/42/head", HostGitLab.Refspec("42")) | ||
| assert.Equal(t, "MR42", HostGitLab.BranchName("42")) | ||
| } | ||
|
|
||
| func TestPRNumber(t *testing.T) { | ||
| assert.Equal(t, "996", prNumber("pull/996/head")) | ||
| assert.Equal(t, "7125", prNumber("merge-requests/7125/head")) | ||
| assert.Equal(t, "", prNumber("refs/heads/main")) | ||
| } | ||
|
|
||
| func TestPRCandidatesOrder(t *testing.T) { | ||
| got := prCandidates("git@gitlab.com:org/repo.git") | ||
| assert.Equal(t, 2, len(got)) | ||
| assert.Equal(t, hostNameGitLab, got[0].Name) // detected host first | ||
| assert.Equal(t, hostNameGitHub, got[1].Name) // fallback | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.