Skip to content
Permalink
Browse files

Merge pull request #386 from jglick/GitSCM.guessBrowser-JENKINS-33409

[JENKINS-33409] Incorrect repository browser detection for github-branch-source
  • Loading branch information
MarkEWaite committed Mar 18, 2016
2 parents fb68e50 + 7efc2c1 commit 0955025ba84f1c20adb965a4ba446537f5ee826b
@@ -321,22 +321,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() {
@@ -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;

@@ -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));

0 comments on commit 0955025

Please sign in to comment.
You can’t perform that action at this time.