Skip to content

Commit 6f3e892

Browse files
Merge pull request #47 from christianruhstaller/fix-bitbucketlink
Fix Bitbucket link handling for branches with slashes
2 parents 410933e + 676e792 commit 6f3e892

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

scm/driver/bitbucket/linker.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package bitbucket
77
import (
88
"context"
99
"fmt"
10+
"strings"
1011

1112
"github.com/drone/go-scm/scm"
1213
)
@@ -26,6 +27,16 @@ func (l *linker) Resource(ctx context.Context, repo string, ref scm.Reference) (
2627
return fmt.Sprintf("%s%s/pull-requests/%d", l.base, repo, d), nil
2728
case ref.Sha == "":
2829
t := scm.TrimRef(ref.Path)
30+
31+
// Bitbucket has a bug where the "source view" link for
32+
// a branch which contains a slash results in a 404.
33+
// The link to the "branch view" works with names containing
34+
// a slash so we do this for branches with slashes in its names.
35+
// See https://jira.atlassian.com/browse/BCLOUD-14422 for more information
36+
if scm.IsBranch(ref.Path) && strings.Contains(t, "/") {
37+
return fmt.Sprintf("%s%s/branch/%s", l.base, repo, t), nil
38+
}
39+
2940
return fmt.Sprintf("%s%s/src/%s", l.base, repo, t), nil
3041
default:
3142
return fmt.Sprintf("%s%s/commits/%s", l.base, repo, ref.Sha), nil

scm/driver/bitbucket/linker_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ func TestLink(t *testing.T) {
3535
path: "refs/heads/master",
3636
want: "https://bitbucket.org/octocat/hello-world/src/master",
3737
},
38+
{
39+
path: "refs/heads/release/production",
40+
want: "https://bitbucket.org/octocat/hello-world/branch/release/production",
41+
},
42+
{
43+
path: "refs/heads/release/production",
44+
sha: "a7389057b0eb027e73b32a81e3c5923a71d01dde",
45+
want: "https://bitbucket.org/octocat/hello-world/commits/a7389057b0eb027e73b32a81e3c5923a71d01dde",
46+
},
3847
}
3948

4049
for _, test := range tests {

scm/util.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ func ExtractPullRequest(ref string) int {
5858
return d
5959
}
6060

61+
// IsBranch returns true if the reference path points to
62+
// a branch.
63+
func IsBranch(ref string) bool {
64+
return strings.HasPrefix(ref, "refs/heads/")
65+
}
66+
6167
// IsTag returns true if the reference path points to
6268
// a tag object.
6369
func IsTag(ref string) bool {

0 commit comments

Comments
 (0)