Skip to content
Permalink
Browse files

Merge pull request #42 from wpraet/master

  • Loading branch information
ndeloof committed Oct 25, 2011
2 parents 79c9c8d + 4b6865a commit 1b3aac3d68f5f952691e41cf2d58f03fe1b01ca0
@@ -0,0 +1,131 @@
package hudson.plugins.git.browser;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.scm.EditType;
import hudson.scm.RepositoryBrowser;
import hudson.util.FormValidation;
import hudson.util.FormValidation.URLCheck;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Pattern;

import javax.servlet.ServletException;

import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

public class FisheyeGitRepositoryBrowser extends GitRepositoryBrowser {

private static final long serialVersionUID = 2881872624557203410L;

/**
* The URL of the FishEye repository.
*
* This is normally like <tt>http://fisheye5.cenqua.com/browse/glassfish/</tt>
* Normalized to have '/' at the tail.
*/
public final URL url;

@DataBoundConstructor
public FisheyeGitRepositoryBrowser(URL url) throws MalformedURLException {
this.url = normalizeToEndWithSlash(url);
}

public URL getUrl() {
return url;
}

@Override
public URL getDiffLink(Path path) throws IOException {
if (path.getEditType() != EditType.EDIT)
return null; // no diff if this is not an edit change
String r1 = path.getChangeSet().getParentCommit();
String r2 = path.getChangeSet().getId();
return new URL(url, getPath(path) + String.format("?r1=%s&r2=%s", r1, r2));
}

@Override
public URL getFileLink(Path path) throws IOException {
return new URL(url, getPath(path));
}

private String getPath(Path path) {
return trimHeadSlash(path.getPath());
}

/**
* Pick up "FOOBAR" from "http://site/browse/FOOBAR/"
*/
private String getProjectName() {
String p = url.getPath();
if (p.endsWith("/"))
p = p.substring(0, p.length() - 1);

int idx = p.lastIndexOf('/');
return p.substring(idx + 1);
}

@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
return new URL(url, "../../changelog/" + getProjectName() + "?cs=" + changeSet.getId());
}

@Extension
public static class FisheyeGitRepositoryBrowserDescriptor extends Descriptor<RepositoryBrowser<?>> {

public String getDisplayName() {
return "FishEye";
}

@Override
public FisheyeGitRepositoryBrowser newInstance(StaplerRequest req, JSONObject jsonObject) throws FormException {
return req.bindParameters(FisheyeGitRepositoryBrowser.class, "fisheye.");
}

/**
* Performs on-the-fly validation of the URL.
*/
public FormValidation doCheckUrl(@QueryParameter(fixEmpty = true) String value) throws IOException,
ServletException {
if (value == null) // nothing entered yet
return FormValidation.ok();

if (!value.endsWith("/"))
value += '/';
if (!URL_PATTERN.matcher(value).matches())
return FormValidation.errorWithMarkup("The URL should end like <tt>.../browse/foobar/</tt>");

// Connect to URL and check content only if we have admin permission
if (!Hudson.getInstance().hasPermission(Hudson.ADMINISTER))
return FormValidation.ok();

final String finalValue = value;
return new URLCheck() {
@Override
protected FormValidation check() throws IOException, ServletException {
try {
if (findText(open(new URL(finalValue)), "FishEye")) {
return FormValidation.ok();
} else {
return FormValidation.error("This is a valid URL but it doesn't look like FishEye");
}
} catch (IOException e) {
return handleIOException(finalValue, e);
}
}
}.check();
}

private static final Pattern URL_PATTERN = Pattern.compile(".+/browse/[^/]+/");

}
}
@@ -0,0 +1,5 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry field="url" title="URL">
<f:textbox/>
</f:entry>
</j:jelly>
@@ -0,0 +1,3 @@
<div>
Specify the URL of this module in FishEye. (such as http://fisheye6.cenqua.com/browse/ant/)
</div>

0 comments on commit 1b3aac3

Please sign in to comment.
You can’t perform that action at this time.