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

Support for Gitorious repositories #38

Closed
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
86 changes: 86 additions & 0 deletions src/main/java/hudson/plugins/git/browser/GitoriousWeb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package hudson.plugins.git.browser;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.scm.EditType;
import hudson.scm.RepositoryBrowser;

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

import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

/**
* Git Browser for Gitorious
*/
public class GitoriousWeb extends GitRepositoryBrowser {

private static final long serialVersionUID = 1L;
private final URL url;

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

public URL getUrl() {
return url;
}

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

/**
* Creates a link to the commit diff.
*
* https://[Gitorious URL]/commit/a9182a07750c9a0dfd89a8461adf72ef5ef0885b
*
* @param path
* @return diff link
* @throws IOException
*/
@Override
public URL getDiffLink(Path path) throws IOException {
final GitChangeSet changeSet = path.getChangeSet();
// gitorious does not show diffs on single files so we create a link to the commit
return new URL(url, "commit/" + changeSet.getId().toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Kraig,

I see there is no method to jump directly to a specific file in the inline view, however something like:

https://gitorious.org/jenkinsci/jenkins/commit/bf2f214983517bc1656fa560e50e5e4df84eba95/diffs?diffmode=sidebyside&fragment=1#test/src/test/java/hudson/tasks/junit/TestResultTest.java
does work, so maybe changing this would be good.

`return new URL(url, "commit/" + changeSet.getId().toString() + "/diffs?diffmode=sidebyside&fragment=1#" + path);``

Regards
Mirko

}

/**
* Creates a link to the file.
* https://[Gitorious URL]/blobs/a9182a07750c9a0dfd89a8461adf72ef5ef0885b/pom.xml
*
* @param path
* @return file link
* @throws IOException
*/
@Override
public URL getFileLink(Path path) throws IOException {
if (path.getEditType().equals(EditType.DELETE)) {
return getDiffLink(path);
} else {
final String spec = "blobs/" + path.getChangeSet().getId() + "/" + path.getPath();
return new URL(url, url.getPath() + spec);
}
}

@Extension
public static class GitoriousWebDescriptor extends Descriptor<RepositoryBrowser<?>> {
public String getDisplayName() {
return "gitoriousweb";
}

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

}
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Specify the root URL serving this repository (such as <a href="http://gitorious.org/gitorious/mainline">http://gitorious.org/gitorious/mainline</a>).
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public void testRedmineWeb() throws IOException {
testExistingBrowser(RedmineWeb.class);
}

public void testGitoriousWeb() throws IOException {
testExistingBrowser(GitoriousWeb.class);
}

public void testGithubWeb() throws IOException {
testExistingBrowser(GithubWeb.class);
}
Expand Down
123 changes: 123 additions & 0 deletions src/test/java/hudson/plugins/git/browser/GitoriousWebTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package hudson.plugins.git.browser;

import hudson.plugins.git.GitChangeLogParser;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import junit.framework.TestCase;

import org.xml.sax.SAXException;


public class GitoriousWebTest extends TestCase {

/**
*
*/
private static final String GITORIOUS_URL = "https://SERVER/PROJECT";
private final GitoriousWeb gitoriousWeb;

{
try {
gitoriousWeb = new GitoriousWeb(GITORIOUS_URL);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}

/**
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}.
* @throws MalformedURLException
*/
public void testGetUrl() throws MalformedURLException {
assertEquals(String.valueOf(gitoriousWeb.getUrl()), GITORIOUS_URL + "/");
}

/**
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getUrl()}.
* @throws MalformedURLException
*/
public void testGetUrlForRepoWithTrailingSlash() throws MalformedURLException {
assertEquals(String.valueOf(new GitoriousWeb(GITORIOUS_URL + "/").getUrl()), GITORIOUS_URL + "/");
}

/**
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getChangeSetLink(hudson.plugins.git.GitChangeSet)}.
* @throws SAXException
* @throws IOException
*/
public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException {
final URL changeSetLink = gitoriousWeb.getChangeSetLink(createChangeSet("rawchangelog"));
assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", changeSetLink.toString());
}

/**
* Test method for {@link hudson.plugins.git.browser.GitoriousWeb#getDiffLink(hudson.plugins.git.GitChangeSet.Path)}.
* @throws SAXException
* @throws IOException
*/
public void testGetDiffLinkPath() throws IOException, SAXException {
final HashMap<String, Path> pathMap = createPathMap("rawchangelog");
final Path modified1 = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", gitoriousWeb.getDiffLink(modified1).toString());
// For added files returns a link to the commit.
final Path added = pathMap.get("src/test/resources/hudson/plugins/git/browser/rawchangelog-with-deleted-file");
assertEquals(GITORIOUS_URL + "/commit/396fc230a3db05c427737aa5c2eb7856ba72b05d", gitoriousWeb.getDiffLink(added).toString());
}

/**
* Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}.
* @throws SAXException
* @throws IOException
*/
public void testGetFileLinkPath() throws IOException, SAXException {
final HashMap<String,Path> pathMap = createPathMap("rawchangelog");
final Path path = pathMap.get("src/main/java/hudson/plugins/git/browser/GithubWeb.java");
final URL fileLink = gitoriousWeb.getFileLink(path);
assertEquals(GITORIOUS_URL + "/blobs/396fc230a3db05c427737aa5c2eb7856ba72b05d/src/main/java/hudson/plugins/git/browser/GithubWeb.java", String.valueOf(fileLink));
}

/**
* Test method for {@link hudson.plugins.git.browser.GithubWeb#getFileLink(hudson.plugins.git.GitChangeSet.Path)}.
* @throws SAXException
* @throws IOException
*/
public void testGetFileLinkPathForDeletedFile() throws IOException, SAXException {
final HashMap<String,Path> pathMap = createPathMap("rawchangelog-with-deleted-file");
final Path path = pathMap.get("bar");
final URL fileLink = gitoriousWeb.getFileLink(path);
assertEquals(GITORIOUS_URL + "/commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f", String.valueOf(fileLink));
}

private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException {
final File rawchangelog = new File(GitoriousWebTest.class.getResource(rawchangelogpath).getFile());
final GitChangeLogParser logParser = new GitChangeLogParser(false);
final List<GitChangeSet> changeSetList = logParser.parse(null, rawchangelog).getLogs();
return changeSetList.get(0);
}

/**
* @param changelog
* @return
* @throws IOException
* @throws SAXException
*/
private HashMap<String, Path> createPathMap(final String changelog) throws IOException, SAXException {
final HashMap<String, Path> pathMap = new HashMap<String, Path>();
final Collection<Path> changeSet = createChangeSet(changelog).getPaths();
for (final Path path : changeSet) {
pathMap.put(path.getPath(), path);
}
return pathMap;
}


}