Skip to content

Commit

Permalink
[FIXED JENKINS-16690] Fix OpenGrok browser support
Browse files Browse the repository at this point in the history
  • Loading branch information
mc1arke committed Mar 23, 2013
1 parent 3aa040b commit 2732db7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
49 changes: 44 additions & 5 deletions src/main/java/hudson/scm/browsers/OpenGrok.java
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2012, Michael Clarke
* Copyright (c) 2012-2013, Michael Clarke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,18 +24,25 @@
package hudson.scm.browsers;

import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.scm.CVSChangeLogSet;
import hudson.scm.CVSRepositoryBrowser;
import hudson.scm.RepositoryBrowser;
import hudson.util.FormValidation;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.export.Exported;

import javax.servlet.ServletException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class OpenGrok extends CVSRepositoryBrowser {
public final URL url;
private final URL url;

@DataBoundConstructor
public OpenGrok(URL url) throws MalformedURLException {
Expand All @@ -44,15 +51,25 @@ public OpenGrok(URL url) throws MalformedURLException {


public URL getFileLink(CVSChangeLogSet.File file) throws IOException {
return new URL(url,"xref/ "+ file.getName() + param().add("r = " + file.getRevision()));
return new URL(url, file.getName() + param().add("r=" + file.getRevision()));
}

public URL getDiffLink(CVSChangeLogSet.File file) throws IOException {
CVSChangeLogSet.Revision r = new CVSChangeLogSet.Revision(file.getRevision());
CVSChangeLogSet.Revision p = r.getPrevious();
if(p==null) return null;

return new URL(getFileLink(file), file.getSimpleName()+".diff"+param().add("r1="+p).add("r2="+r));
if(p == null) {
return null;
}
String path = url.getPath();
Matcher matches = Pattern.compile("^(.*)/xref/([^/]+/)$").matcher(path);
if (!matches.matches()) {
return null;
}
String moduleName = matches.group(2);
path = matches.replaceFirst("$1/diff/" + moduleName);
path = path + file.getName() + param().add("r2=/"+ moduleName +file.getName() + "@" + r).add("r1=/" + moduleName + file.getName() + "@" + p);
return new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
}

public URL getChangeSetLink(CVSChangeLogSet.CVSChangeLog changeSet) throws IOException {
Expand All @@ -65,8 +82,30 @@ private QueryBuilder param() {

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

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

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

public FormValidation doCheckUrl(@QueryParameter String value) {
String url = Util.fixEmpty(value);

if (url == null) {
return FormValidation.ok();
}

if (!url.endsWith("/")) {
url += '/';
}

if (!URL_PATTERN.matcher(url).matches()) {
return FormValidation.errorWithMarkup("The URL should end like <tt>.../xref/foobar/</tt>");
}

return FormValidation.ok();

}
}
}
44 changes: 44 additions & 0 deletions src/test/java/hudson/scm/browsers/OpenGrokTest.java
@@ -0,0 +1,44 @@
package hudson.scm.browsers;


import hudson.scm.CVSChangeLogSet;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class OpenGrokTest {

private OpenGrok testCase;
private CVSChangeLogSet.File file;

@Before
public void setUp() throws MalformedURLException {
testCase = new OpenGrok(new URL("http://1.2.3.4/source/xref/branchv3/"));

file = new CVSChangeLogSet.File();
file.setName("src/example2.java");
file.setRevision("1.19.2.1");
file.setPrevrevision("1.7");
}

@Test
public void testGetDiffLink() throws IOException {
assertEquals(new URL("http://1.2.3.4/source/diff/branchv3/src/example2.java?r2=/branchv3/src/example2.java@1.19.2.1&r1=/branchv3/src/example2.java@1.19"), testCase.getDiffLink(file));
}

@Test
public void testGetFile() throws IOException {
assertEquals(new URL("http://1.2.3.4/source/xref/branchv3/src/example2.java?r=1.19.2.1"), testCase.getFileLink(file));
}

@Test
public void testGetChangeSetLink() throws IOException {
assertNull(testCase.getChangeSetLink(new CVSChangeLogSet.CVSChangeLog()));
}
}

0 comments on commit 2732db7

Please sign in to comment.