Skip to content

Commit

Permalink
[FIXED JENKINS-33409] Incorrect repository browser detection when mul…
Browse files Browse the repository at this point in the history
…tiple refspecs were configured, even with the same URL.
  • Loading branch information
jglick committed Mar 16, 2016
1 parent 61ae4e8 commit 7efc2c1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
32 changes: 19 additions & 13 deletions src/main/java/hudson/plugins/git/GitSCM.java
Expand Up @@ -319,22 +319,28 @@ public GitRepositoryBrowser getBrowser() {
}

@Override public RepositoryBrowser<?> guessBrowser() {
if (remoteRepositories != null && remoteRepositories.size() == 1) {
List<URIish> uris = remoteRepositories.get(0).getURIs();
if (uris.size() == 1) {
String uri = uris.get(0).toString();
// TODO make extensible by introducing an abstract GitRepositoryBrowserDescriptor
Matcher m = Pattern.compile("(https://github[.]com/[^/]+/[^/]+)[.]git").matcher(uri);
if (m.matches()) {
return new GithubWeb(m.group(1) + "/");
}
m = Pattern.compile("git@github[.]com:([^/]+/[^/]+)[.]git").matcher(uri);
if (m.matches()) {
return new GithubWeb("https://github.com/" + m.group(1) + "/");
Set<String> webUrls = new HashSet<String>();
if (remoteRepositories != null) {
for (RemoteConfig config : remoteRepositories) {
for (URIish uriIsh : config.getURIs()) {
String uri = uriIsh.toString();
// TODO make extensible by introducing an abstract GitRepositoryBrowserDescriptor
Matcher m = Pattern.compile("(https://github[.]com/[^/]+/[^/]+)[.]git").matcher(uri);
if (m.matches()) {
webUrls.add(m.group(1) + "/");
}
m = Pattern.compile("git@github[.]com:([^/]+/[^/]+)[.]git").matcher(uri);
if (m.matches()) {
webUrls.add("https://github.com/" + m.group(1) + "/");
}
}
}
}
return null;
if (webUrls.size() == 1) {
return new GithubWeb(webUrls.iterator().next());
} else {
return null;
}
}

public boolean isCreateAccountBasedOnEmail() {
Expand Down
54 changes: 52 additions & 2 deletions src/test/java/hudson/plugins/git/browser/GithubWebTest.java
Expand Up @@ -4,23 +4,26 @@

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 hudson.plugins.git.GitSCM;
import hudson.scm.RepositoryBrowser;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.scm.api.SCMHead;
import org.eclipse.jgit.transport.RefSpec;

import static org.junit.Assert.*;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

import org.xml.sax.SAXException;

Expand Down Expand Up @@ -117,6 +120,53 @@ private void assertGuessURL(String repo, String web) {
assertEquals(web, actual);
}

@Issue("JENKINS-33409")
@Test
public void guessBrowserSCMSource() throws Exception {
// like GitSCMSource:
assertGuessURL("https://github.com/kohsuke/msv.git", "https://github.com/kohsuke/msv/", "+refs/heads/*:refs/remotes/origin/*");
// like GitHubSCMSource:
assertGuessURL("https://github.com/kohsuke/msv.git", "https://github.com/kohsuke/msv/", "+refs/heads/*:refs/remotes/origin/*", "+refs/pull/*/merge:refs/remotes/origin/pr/*");
}
private void assertGuessURL(String remote, String web, String... refSpecs) {
RepositoryBrowser<?> guess = new MockSCMSource(remote, refSpecs).build(new SCMHead("master")).guessBrowser();
String actual = guess instanceof GithubWeb ? ((GithubWeb) guess).getRepoUrl() : null;
assertEquals(web, actual);
}
private static class MockSCMSource extends AbstractGitSCMSource {
private final String remote;
private final String[] refSpecs;
MockSCMSource(String remote, String[] refSpecs) {
super(null);
this.remote = remote;
this.refSpecs = refSpecs;
}
@Override
public String getCredentialsId() {
return null;
}
@Override
public String getRemote() {
return remote;
}
@Override
public String getIncludes() {
return "*";
}
@Override
public String getExcludes() {
return "";
}
@Override
protected List<RefSpec> getRefSpecs() {
List<RefSpec> result = new ArrayList<RefSpec>();
for (String refSpec : refSpecs) {
result.add(new RefSpec(refSpec));
}
return result;
}
}

private GitChangeSet createChangeSet(String rawchangelogpath) throws IOException, SAXException {
final GitChangeLogParser logParser = new GitChangeLogParser(false);
final List<GitChangeSet> changeSetList = logParser.parse(GithubWebTest.class.getResourceAsStream(rawchangelogpath));
Expand Down

0 comments on commit 7efc2c1

Please sign in to comment.