Skip to content

Commit

Permalink
[FIXED JENKINS-19865] Wrong GitBucket link
Browse files Browse the repository at this point in the history
  • Loading branch information
ssogabe committed Oct 3, 2013
1 parent d8619d1 commit 3fe7bcf
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 23 deletions.
Expand Up @@ -52,16 +52,17 @@ public class GitBucketBrowser extends GitRepositoryBrowser {

@DataBoundConstructor
public GitBucketBrowser(String url) throws MalformedURLException {
this.url = normalizeToEndWithSlash(new URL(url));
String normalizedUrl = GitBucketUtil.trimEndSlash(url);
this.url = new URL(normalizedUrl);
}

public URL getUrl() {
return url;
}

@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
return new URL(url, url.getPath() + "commit/" + changeSet.getId().toString());
return new URL(url, url.getPath() + "/commit/" + changeSet.getId().toString());
}

@Override
Expand All @@ -88,7 +89,7 @@ public URL getFileLink(Path path) throws IOException {
if (path.getEditType().equals(EditType.DELETE)) {
return getDiffLinkRegardlessOfEditType(path);
} else {
String spec = "blob/" + path.getChangeSet().getId() + "/" + path.getPath();
String spec = "/blob/" + path.getChangeSet().getId() + "/" + path.getPath();
return new URL(url, url.getPath() + spec);
}
}
Expand Down
Expand Up @@ -50,7 +50,7 @@ public void annotate(AbstractBuild<?, ?> build, ChangeLogSet.Entry change, Marku
}

String url = gpp.getUrl();
annotate(text, url);
annotate(text, url + '/');
}

void annotate(MarkupText text, String url) {
Expand Down
Expand Up @@ -57,21 +57,10 @@ public boolean isLinkEnabled() {

@DataBoundConstructor
public GitBucketProjectProperty(String url, boolean linkEnabled) {
this.url = normalizeUrl(url);
this.url = GitBucketUtil.trimEndSlash(url);
this.linkEnabled = linkEnabled;
}

private String normalizeUrl(String url) {
String u = Util.fixEmptyAndTrim(url);
if (u == null) {
return null;
}
if (u.endsWith("/")) {
return u;
}
return u + "/";
}

@Override
public Collection<? extends Action> getJobActions(AbstractProject<?, ?> job) {
if (url == null) {
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitbucket/GitBucketUtil.java
@@ -0,0 +1,48 @@
/*
* The MIT License
*
* Copyright (c) 2013, Seiji Sogabe
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.gitbucket;

import hudson.Util;

/**
* Utility
*
* @author sogabe
*/
public final class GitBucketUtil {

private GitBucketUtil() {
}

public static String trimEndSlash(String url) {
String u = Util.fixEmptyAndTrim(url);
if (u == null) {
return null;
}
if (!u.endsWith("/")) {
return u;
}
return url.substring(0, u.length() - 1);
}
}
Expand Up @@ -47,30 +47,40 @@
*/
public class GitBucketProjectPropertyTest {

private static final String GITBUCKET_URL = "http://localhost/gitbucket/sogabe/gitbucket-plugin/";
private static final String GITBUCKET_URL = "http://localhost/gitbucket/sogabe/gitbucket-plugin";

private GitBucketProjectProperty target;

@Test
public void testNormalizeUrl_NotEndWithSlash() {
// not end with slash
String url = "http://localhost/gitbucket/sogabe/gitbucket-plugin";
target = new GitBucketProjectProperty(url, true);
target = new GitBucketProjectProperty(GITBUCKET_URL, true);

String actual = target.getUrl();

assertThat(actual, is(url + '/'));
assertThat(actual, is(GITBUCKET_URL));
}

@Test
public void testNormalizeUrl_EndWithSlash() {
// end with slash
String url = GITBUCKET_URL + '/';
target = new GitBucketProjectProperty(url, true);

String actual = target.getUrl();

assertThat(actual, is(GITBUCKET_URL));
}

@Test
public void testNormalizeUrl_EndWithSpace() {
// end with " "
String url = "http://localhost/gitbucket/sogabe/gitbucket-plugin ";
String url = GITBUCKET_URL + ' ';
target = new GitBucketProjectProperty(url, true);

String actual = target.getUrl();

assertThat(actual, is(url.trim() + '/'));
assertThat(actual, is(url.trim()));
}

@Test
Expand Down

0 comments on commit 3fe7bcf

Please sign in to comment.