From 58a95f4a158fc9fb33686091fb492e2bafdcf55b Mon Sep 17 00:00:00 2001 From: Christian Ruhstaller Date: Thu, 21 Nov 2019 20:29:20 +0100 Subject: [PATCH 1/3] Fix Bitbucket link handling for branches with slashes Bitbucket has a bug where the "source view" link for a branch which contains a slash results in a 404. The link to the "branch view" works with names containing a slash so we do this for branches with slashes in its names. See https://jira.atlassian.com/browse/BCLOUD-14422 for more information. --- scm/driver/bitbucket/linker.go | 13 ++++++++++++- scm/driver/bitbucket/linker_test.go | 9 +++++++++ scm/util.go | 12 ++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/scm/driver/bitbucket/linker.go b/scm/driver/bitbucket/linker.go index d9895c4fa..3817063dc 100644 --- a/scm/driver/bitbucket/linker.go +++ b/scm/driver/bitbucket/linker.go @@ -24,8 +24,19 @@ func (l *linker) Resource(ctx context.Context, repo string, ref scm.Reference) ( case scm.IsPullRequest(ref.Path): d := scm.ExtractPullRequest(ref.Path) return fmt.Sprintf("%s%s/pull-requests/%d", l.base, repo, d), nil - case ref.Sha == "": + case scm.IsBranch(ref.Path) && ref.Sha == "": t := scm.TrimRef(ref.Path) + + // Bitbucket has a bug where the "source view" link for + // a branch which contains a slash results in a 404. + // The link to the "branch view" works with names containing + // a slash so we do this for branches with slashes in its names. + // See https://jira.atlassian.com/browse/BCLOUD-14422 for more information + // + if scm.BranchContainsSlash(t) { + return fmt.Sprintf("%s%s/branch/%s", l.base, repo, t), nil + } + return fmt.Sprintf("%s%s/src/%s", l.base, repo, t), nil default: return fmt.Sprintf("%s%s/commits/%s", l.base, repo, ref.Sha), nil diff --git a/scm/driver/bitbucket/linker_test.go b/scm/driver/bitbucket/linker_test.go index 26d00d29c..2a993552b 100644 --- a/scm/driver/bitbucket/linker_test.go +++ b/scm/driver/bitbucket/linker_test.go @@ -35,6 +35,15 @@ func TestLink(t *testing.T) { path: "refs/heads/master", want: "https://bitbucket.org/octocat/hello-world/src/master", }, + { + path: "refs/heads/release/production", + want: "https://bitbucket.org/octocat/hello-world/branch/release/production", + }, + { + path: "refs/heads/release/production", + sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde", + want: "https://bitbucket.org/octocat/hello-world/commits/a7389057b0eb027e73b32a81e3c5923a71d01dde", + }, } for _, test := range tests { diff --git a/scm/util.go b/scm/util.go index 58344493d..cbb02c3ad 100644 --- a/scm/util.go +++ b/scm/util.go @@ -58,6 +58,12 @@ func ExtractPullRequest(ref string) int { return d } +// IsBranch returns true if the reference path points to +// a branch. +func IsBranch(ref string) bool { + return strings.HasPrefix(ref, "refs/heads/") +} + // IsTag returns true if the reference path points to // a tag object. func IsTag(ref string) bool { @@ -71,3 +77,9 @@ func IsPullRequest(ref string) bool { strings.HasPrefix(ref, "refs/pull-request/") || strings.HasPrefix(ref, "refs/merge-requests/") } + +// BranchContainsSlash returns true if the branch name +// contains a slash. +func BranchContainsSlash(branch string) bool { + return strings.Contains(branch, "/") +} From 62c5aad84194edb0147e8a2ca08cd822fde535dd Mon Sep 17 00:00:00 2001 From: Christian Ruhstaller Date: Thu, 21 Nov 2019 20:42:22 +0100 Subject: [PATCH 2/3] Inline check for slash in branch name --- scm/driver/bitbucket/linker.go | 3 ++- scm/util.go | 6 ------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/scm/driver/bitbucket/linker.go b/scm/driver/bitbucket/linker.go index 3817063dc..a1e414633 100644 --- a/scm/driver/bitbucket/linker.go +++ b/scm/driver/bitbucket/linker.go @@ -7,6 +7,7 @@ package bitbucket import ( "context" "fmt" + "strings" "github.com/drone/go-scm/scm" ) @@ -33,7 +34,7 @@ func (l *linker) Resource(ctx context.Context, repo string, ref scm.Reference) ( // a slash so we do this for branches with slashes in its names. // See https://jira.atlassian.com/browse/BCLOUD-14422 for more information // - if scm.BranchContainsSlash(t) { + if strings.Contains(t, "/") { return fmt.Sprintf("%s%s/branch/%s", l.base, repo, t), nil } diff --git a/scm/util.go b/scm/util.go index cbb02c3ad..6108feee8 100644 --- a/scm/util.go +++ b/scm/util.go @@ -77,9 +77,3 @@ func IsPullRequest(ref string) bool { strings.HasPrefix(ref, "refs/pull-request/") || strings.HasPrefix(ref, "refs/merge-requests/") } - -// BranchContainsSlash returns true if the branch name -// contains a slash. -func BranchContainsSlash(branch string) bool { - return strings.Contains(branch, "/") -} From 676e792608afe4be44e7c98129962e4ecdc426f9 Mon Sep 17 00:00:00 2001 From: Christian Ruhstaller Date: Thu, 21 Nov 2019 20:54:23 +0100 Subject: [PATCH 3/3] Refactor check for branch to reduce possible regression --- scm/driver/bitbucket/linker.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scm/driver/bitbucket/linker.go b/scm/driver/bitbucket/linker.go index a1e414633..d3cc6bd33 100644 --- a/scm/driver/bitbucket/linker.go +++ b/scm/driver/bitbucket/linker.go @@ -25,7 +25,7 @@ func (l *linker) Resource(ctx context.Context, repo string, ref scm.Reference) ( case scm.IsPullRequest(ref.Path): d := scm.ExtractPullRequest(ref.Path) return fmt.Sprintf("%s%s/pull-requests/%d", l.base, repo, d), nil - case scm.IsBranch(ref.Path) && ref.Sha == "": + case ref.Sha == "": t := scm.TrimRef(ref.Path) // Bitbucket has a bug where the "source view" link for @@ -33,8 +33,7 @@ func (l *linker) Resource(ctx context.Context, repo string, ref scm.Reference) ( // The link to the "branch view" works with names containing // a slash so we do this for branches with slashes in its names. // See https://jira.atlassian.com/browse/BCLOUD-14422 for more information - // - if strings.Contains(t, "/") { + if scm.IsBranch(ref.Path) && strings.Contains(t, "/") { return fmt.Sprintf("%s%s/branch/%s", l.base, repo, t), nil }