Skip to content

Commit 58a95f4

Browse files
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.
1 parent 410933e commit 58a95f4

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

scm/driver/bitbucket/linker.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,19 @@ func (l *linker) Resource(ctx context.Context, repo string, ref scm.Reference) (
2424
case scm.IsPullRequest(ref.Path):
2525
d := scm.ExtractPullRequest(ref.Path)
2626
return fmt.Sprintf("%s%s/pull-requests/%d", l.base, repo, d), nil
27-
case ref.Sha == "":
27+
case scm.IsBranch(ref.Path) && ref.Sha == "":
2828
t := scm.TrimRef(ref.Path)
29+
30+
// Bitbucket has a bug where the "source view" link for
31+
// a branch which contains a slash results in a 404.
32+
// The link to the "branch view" works with names containing
33+
// a slash so we do this for branches with slashes in its names.
34+
// See https://jira.atlassian.com/browse/BCLOUD-14422 for more information
35+
//
36+
if scm.BranchContainsSlash(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: 12 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 {
@@ -71,3 +77,9 @@ func IsPullRequest(ref string) bool {
7177
strings.HasPrefix(ref, "refs/pull-request/") ||
7278
strings.HasPrefix(ref, "refs/merge-requests/")
7379
}
80+
81+
// BranchContainsSlash returns true if the branch name
82+
// contains a slash.
83+
func BranchContainsSlash(branch string) bool {
84+
return strings.Contains(branch, "/")
85+
}

0 commit comments

Comments
 (0)