Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from wpraet/master
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
src/main/java/hudson/plugins/git/browser/FisheyeGitRepositoryBrowser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/[^/]+/"); | ||
|
||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/resources/hudson/plugins/git/browser/FisheyeGitRepositoryBrowser/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
3 changes: 3 additions & 0 deletions
3
src/main/resources/hudson/plugins/git/browser/FisheyeGitRepositoryBrowser/help-url.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div> | ||
Specify the URL of this module in FishEye. (such as http://fisheye6.cenqua.com/browse/ant/) | ||
</div> |