Skip to content

Commit

Permalink
Merge pull request #47 from christianruhstaller/fix-bitbucketlink
Browse files Browse the repository at this point in the history
Fix Bitbucket link handling for branches with slashes
  • Loading branch information
bradrydzewski authored Dec 19, 2019
2 parents 410933e + 676e792 commit 6f3e892
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions scm/driver/bitbucket/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package bitbucket
import (
"context"
"fmt"
"strings"

"github.com/drone/go-scm/scm"
)
Expand All @@ -26,6 +27,16 @@ func (l *linker) Resource(ctx context.Context, repo string, ref scm.Reference) (
return fmt.Sprintf("%s%s/pull-requests/%d", l.base, repo, d), nil
case 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.IsBranch(ref.Path) && strings.Contains(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
Expand Down
9 changes: 9 additions & 0 deletions scm/driver/bitbucket/linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions scm/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6f3e892

Please sign in to comment.