Skip to content
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

Fix Bitbucket link handling for branches with slashes #47

Merged
merged 3 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion scm/driver/bitbucket/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
bradrydzewski marked this conversation as resolved.
Show resolved Hide resolved
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
12 changes: 12 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 All @@ -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, "/")
}