Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #270 from olafmandel/JENKINS-25568
[FIXED JENKINS-25568] GitLab: fix file link for version 4.2
  • Loading branch information
MarkEWaite committed Nov 12, 2014
2 parents 699fdc9 + 809fec0 commit d265da5
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/main/java/hudson/plugins/git/browser/GitLab.java
Expand Up @@ -36,7 +36,8 @@ public double getVersion() {
/**
* Creates a link to the changeset
*
* https://[GitLab URL]/commits/a9182a07750c9a0dfd89a8461adf72ef5ef0885b
* v < 3.0: [GitLab URL]/commits/[Hash]
* else: [GitLab URL]/commit/[Hash]
*
* @return diff link
* @throws IOException
Expand All @@ -50,9 +51,10 @@ public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {

/**
* Creates a link to the commit diff.
*
* https://[GitLab URL]/commits/a9182a07750c9a0dfd89a8461adf72ef5ef0885b#[path to file]
*
*
* v < 3.0: [GitLab URL]/commits/[Hash]#[File path]
* else: [GitLab URL]/commit/[Hash]#[File path]
*
* @param path
* @return diff link
* @throws IOException
Expand All @@ -65,8 +67,10 @@ public URL getDiffLink(Path path) throws IOException {

/**
* Creates a link to the file.
* https://[GitLab URL]/a9182a07750c9a0dfd89a8461adf72ef5ef0885b/tree/pom.xml
*
* v ≤ 4.2: [GitLab URL]tree/[Hash]/[File path]
* v < 5.1: [GitLab URL][Hash]/tree/[File path]
* else: [GitLab URL]blob/[Hash]/[File path]
*
* @param path
* @return file link
* @throws IOException
Expand All @@ -76,14 +80,13 @@ public URL getFileLink(Path path) throws IOException {
if (path.getEditType().equals(EditType.DELETE)) {
return getDiffLink(path);
} else {
String spec;
if(getVersion() >= 5.1) {
spec = "blob/" + path.getChangeSet().getId() + "/" + path.getPath();
if(getVersion() <= 4.2) {
return new URL(getUrl(), "tree/" + path.getChangeSet().getId() + "/" + path.getPath());
} else if(getVersion() < 5.1) {
return new URL(getUrl(), path.getChangeSet().getId() + "/tree/" + path.getPath());
} else {
spec = path.getChangeSet().getId() + "/tree/" + path.getPath();
return new URL(getUrl(), "blob/" + path.getChangeSet().getId() + "/" + path.getPath());
}
URL url = getUrl();
return new URL(url, url.getPath() + spec);
}
}

Expand All @@ -100,11 +103,11 @@ public GitLab newInstance(StaplerRequest req, JSONObject jsonObject) throws Form
}

private String calculatePrefix() {
if(getVersion() >= 3){
if(getVersion() < 3) {
return "commits/";
} else {
return "commit/";
}

return "commits/";
}

}
119 changes: 119 additions & 0 deletions src/test/java/hudson/plugins/git/browser/GitLabTest.java
@@ -0,0 +1,119 @@
package hudson.plugins.git.browser;

import hudson.model.Run;
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.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import junit.framework.TestCase;

import org.xml.sax.SAXException;

public class GitLabTest extends TestCase {

private static final String GITLAB_URL = "https://SERVER/USER/REPO/";
private final GitLab gitlab29 = new GitLab(GITLAB_URL, "2.9");
private final GitLab gitlab42 = new GitLab(GITLAB_URL, "4.2");
private final GitLab gitlab50 = new GitLab(GITLAB_URL, "5.0");
private final GitLab gitlab51 = new GitLab(GITLAB_URL, "5.1");

/**
* Test method for {@link hudson.plugins.git.browser.GitLab#getVersion()}.
*/
public void testGetVersion() {
assertEquals(gitlab29.getVersion(), 2.9);
assertEquals(gitlab42.getVersion(), 4.2);
assertEquals(gitlab50.getVersion(), 5.0);
assertEquals(gitlab51.getVersion(), 5.1);
}

/**
* Test method for {@link hudson.plugins.git.browser.GitLab#getChangeSetLink(hudson.plugins.git.GitChangeSet)}.
* @throws IOException
*/
public void testGetChangeSetLinkGitChangeSet() throws IOException, SAXException {
assertEquals(GITLAB_URL + "commits/396fc230a3db05c427737aa5c2eb7856ba72b05d",
gitlab29.getChangeSetLink(createChangeSet("rawchangelog")).toString());
assertEquals(GITLAB_URL + "commit/396fc230a3db05c427737aa5c2eb7856ba72b05d",
gitlab42.getChangeSetLink(createChangeSet("rawchangelog")).toString());
assertEquals(GITLAB_URL + "commit/396fc230a3db05c427737aa5c2eb7856ba72b05d",
gitlab50.getChangeSetLink(createChangeSet("rawchangelog")).toString());
assertEquals(GITLAB_URL + "commit/396fc230a3db05c427737aa5c2eb7856ba72b05d",
gitlab51.getChangeSetLink(createChangeSet("rawchangelog")).toString());
}

/**
* Test method for {@link hudson.plugins.git.browser.GitLab#getDiffLink(hudson.plugins.git.GitChangeSet.Path)}.
* @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(GITLAB_URL + "commits/396fc230a3db05c427737aa5c2eb7856ba72b05d#src/main/java/hudson/plugins/git/browser/GithubWeb.java",
gitlab29.getDiffLink(modified1).toString());
assertEquals(GITLAB_URL + "commit/396fc230a3db05c427737aa5c2eb7856ba72b05d#src/main/java/hudson/plugins/git/browser/GithubWeb.java",
gitlab42.getDiffLink(modified1).toString());
assertEquals(GITLAB_URL + "commit/396fc230a3db05c427737aa5c2eb7856ba72b05d#src/main/java/hudson/plugins/git/browser/GithubWeb.java",
gitlab50.getDiffLink(modified1).toString());
assertEquals(GITLAB_URL + "commit/396fc230a3db05c427737aa5c2eb7856ba72b05d#src/main/java/hudson/plugins/git/browser/GithubWeb.java",
gitlab51.getDiffLink(modified1).toString());
}

/**
* Test method for {@link hudson.plugins.git.browser.GitLab#getFileLink(hudson.plugins.git.GitChangeSet.Path)}.
* @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");
assertEquals(GITLAB_URL + "tree/396fc230a3db05c427737aa5c2eb7856ba72b05d/src/main/java/hudson/plugins/git/browser/GithubWeb.java",
gitlab29.getFileLink(path).toString());
assertEquals(GITLAB_URL + "tree/396fc230a3db05c427737aa5c2eb7856ba72b05d/src/main/java/hudson/plugins/git/browser/GithubWeb.java",
gitlab42.getFileLink(path).toString());
assertEquals(GITLAB_URL + "396fc230a3db05c427737aa5c2eb7856ba72b05d/tree/src/main/java/hudson/plugins/git/browser/GithubWeb.java",
gitlab50.getFileLink(path).toString());
assertEquals(GITLAB_URL + "blob/396fc230a3db05c427737aa5c2eb7856ba72b05d/src/main/java/hudson/plugins/git/browser/GithubWeb.java",
gitlab51.getFileLink(path).toString());
}

/**
* Test method for {@link hudson.plugins.git.browser.GitLab#getFileLink(hudson.plugins.git.GitChangeSet.Path)}.
* @throws IOException
*/
public void testGetFileLinkPathForDeletedFile() throws IOException, SAXException {
final HashMap<String,Path> pathMap = createPathMap("rawchangelog-with-deleted-file");
final Path path = pathMap.get("bar");
assertEquals(GITLAB_URL + "commits/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f#bar",
gitlab29.getFileLink(path).toString());
assertEquals(GITLAB_URL + "commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f#bar",
gitlab42.getFileLink(path).toString());
assertEquals(GITLAB_URL + "commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f#bar",
gitlab50.getFileLink(path).toString());
assertEquals(GITLAB_URL + "commit/fc029da233f161c65eb06d0f1ed4f36ae81d1f4f#bar",
gitlab51.getFileLink(path).toString());
}

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

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;
}

}

0 comments on commit d265da5

Please sign in to comment.