Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' into JENKINS-36574
  • Loading branch information
steven-foster committed Dec 13, 2017
2 parents dcae814 + 08b3d32 commit 51e1535
Show file tree
Hide file tree
Showing 18 changed files with 788 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,5 +1,5 @@
target
work
/work*
/github-branch-source.iml
/.idea
.classpath
Expand Down
12 changes: 6 additions & 6 deletions pom.xml
Expand Up @@ -4,11 +4,11 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.32</version>
<version>2.33</version>
<relativePath />
</parent>
<artifactId>github-branch-source</artifactId>
<version>2.2.4-SNAPSHOT</version>
<version>2.3.2-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>GitHub Branch Source Plugin</name>
<url>https://wiki.jenkins-ci.org/display/JENKINS/GitHub+Branch+Source+Plugin</url>
Expand All @@ -21,7 +21,7 @@
</licenses>

<properties>
<jenkins.version>1.625.3</jenkins.version>
<jenkins.version>1.642.3</jenkins.version>
<workflow.version>1.14.2</workflow.version>
<scm-api.version>2.2.0</scm-api.version>
</properties>
Expand All @@ -36,7 +36,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.9</version>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -46,12 +46,12 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>3.5.0</version>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-api</artifactId>
<version>1.86</version>
<version>1.90</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Expand Up @@ -46,6 +46,7 @@
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.scm.api.mixin.TagSCMHead;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.transport.RefSpec;
Expand Down Expand Up @@ -119,6 +120,9 @@ public GitHubSCMBuilder(@NonNull GitHubSCMSource source,
withRefSpec("+refs/pull/" + h.getId() + "/head:refs/remotes/@{remote}/" + head
.getName());
repoUrl = repositoryUrl(h.getSourceOwner(), h.getSourceRepo());
} else if (head instanceof TagSCMHead) {
withRefSpec("+refs/tags/" + head.getName() + ":refs/tags/" + head.getName());
repoUrl = repositoryUrl(repoOwner, repository);
} else {
withRefSpec("+refs/heads/" + head.getName() + ":refs/remotes/@{remote}/" + head.getName());
repoUrl = repositoryUrl(repoOwner, repository);
Expand Down
Expand Up @@ -33,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import jenkins.scm.api.SCMFile;
import org.eclipse.jgit.lib.Constants;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;

Expand Down Expand Up @@ -88,28 +89,29 @@ private Object metadata() throws IOException {
try {
switch (info) {
case DIRECTORY_ASSUMED:
metadata = repo.getDirectoryContent(getPath(), ref);
metadata = repo.getDirectoryContent(getPath(), ref.indexOf('/') == -1 ? ref : Constants.R_REFS + ref);
info = TypeInfo.DIRECTORY_CONFIRMED;
resolved = true;
break;
case DIRECTORY_CONFIRMED:
metadata = repo.getDirectoryContent(getPath(), ref);
metadata = repo.getDirectoryContent(getPath(), ref.indexOf('/') == -1 ? ref : Constants.R_REFS + ref);
resolved = true;
break;
case NON_DIRECTORY_CONFIRMED:
metadata = repo.getFileContent(getPath(), ref);
metadata = repo.getFileContent(getPath(), ref.indexOf('/') == -1 ? ref : Constants.R_REFS + ref);
resolved = true;
break;
case UNRESOLVED:
checkOpen();
try {
metadata = repo.getFileContent(getPath(), ref);
metadata = repo.getFileContent(getPath(), ref.indexOf('/') == -1 ? ref : Constants.R_REFS + ref);
info = TypeInfo.NON_DIRECTORY_CONFIRMED;
resolved = true;
} catch (IOException e) {
if (e.getCause() instanceof IOException
&& e.getCause().getCause() instanceof JsonMappingException) {
metadata = repo.getDirectoryContent(getPath(), ref);
metadata = repo.getDirectoryContent(getPath(),
ref.indexOf('/') == -1 ? ref : Constants.R_REFS + ref);
info = TypeInfo.DIRECTORY_CONFIRMED;
resolved = true;
} else {
Expand All @@ -136,7 +138,7 @@ protected SCMFile newChild(String name, boolean assumeIsDirectory) {
@Override
public Iterable<SCMFile> children() throws IOException {
checkOpen();
List<GHContent> content = repo.getDirectoryContent(getPath(), ref);
List<GHContent> content = repo.getDirectoryContent(getPath(), ref.indexOf('/') == -1 ? ref : Constants.R_REFS + ref);
List<SCMFile> result = new ArrayList<>(content.size());
for (GHContent c : content) {
result.add(new GitHubSCMFile(this, c.getName(), c));
Expand Down
Expand Up @@ -33,13 +33,18 @@
import hudson.scm.SCM;
import java.io.IOException;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.plugins.git.GitTagSCMRevision;
import jenkins.scm.api.SCMFile;
import jenkins.scm.api.SCMFileSystem;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.lib.Constants;
import org.kohsuke.github.GHRef;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GHTagObject;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.HttpException;

Expand All @@ -49,7 +54,16 @@ public class GitHubSCMFileSystem extends SCMFileSystem implements GitHubClosable
private final String ref;
private boolean open;

protected GitHubSCMFileSystem(GitHub gitHub, GHRepository repo, String ref, @CheckForNull SCMRevision rev) throws IOException {
/**
* Constructor.
*
* @param gitHub the {@link GitHub}
* @param repo the {@link GHRepository}
* @param refName the ref name, e.g. {@code heads/branchName}, {@code tags/tagName}, {@code pull/N/head} or the SHA.
* @param rev the optional revision.
* @throws IOException if I/O errors occur.
*/
protected GitHubSCMFileSystem(GitHub gitHub, GHRepository repo, String refName, @CheckForNull SCMRevision rev) throws IOException {
super(rev);
this.gitHub = gitHub;
this.open = true;
Expand All @@ -62,10 +76,10 @@ protected GitHubSCMFileSystem(GitHub gitHub, GHRepository repo, String ref, @Che
} else if (rev instanceof AbstractGitSCMSource.SCMRevisionImpl) {
this.ref = ((AbstractGitSCMSource.SCMRevisionImpl) rev).getHash();
} else {
this.ref = ref;
this.ref = refName;
}
} else {
this.ref = ref;
this.ref = refName;
}
}

Expand Down Expand Up @@ -134,9 +148,11 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
apiUri == null ? GitHubSCMSource.GITHUB_URL : apiUri);
throw new IOException(message);
}
String ref;
String refName;
if (head instanceof BranchSCMHead) {
ref = head.getName();
refName = "heads/" + head.getName();
} else if (head instanceof GitHubTagSCMHead) {
refName = "tags/" + head.getName();
} else if (head instanceof PullRequestSCMHead) {
PullRequestSCMHead pr = (PullRequestSCMHead) head;
if (!pr.isMerge() && pr.getSourceRepo() != null) {
Expand Down Expand Up @@ -181,9 +197,24 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
return null;
}
if (rev == null) {
rev = new AbstractGitSCMSource.SCMRevisionImpl((BranchSCMHead) head, repo.getBranch(ref).getSHA1());
GHRef ref = repo.getRef(refName);
if ("tag".equalsIgnoreCase(ref.getObject().getType())) {
GHTagObject tag = repo.getTagObject(ref.getObject().getSha());
if (head instanceof GitHubTagSCMHead) {
rev = new GitTagSCMRevision((GitHubTagSCMHead) head, tag.getObject().getSha());
} else {
// we should never get here, but just in case, we have the information to construct
// the correct head, so let's do that
rev = new GitTagSCMRevision(
new GitHubTagSCMHead(head.getName(),
tag.getTagger().getDate().getTime()), tag.getObject().getSha()
);
}
} else {
rev = new AbstractGitSCMSource.SCMRevisionImpl(head, ref.getObject().getSha());
}
}
return new GitHubSCMFileSystem(github, repo, ref, rev);
return new GitHubSCMFileSystem(github, repo, refName, rev);
} catch (IOException | RuntimeException e) {
Connector.release(github);
throw e;
Expand Down
Expand Up @@ -1324,6 +1324,7 @@ public FormValidation doCheckCredentialsId(@CheckForNull @AncestorInPath Item co
*
* @param context the context.
* @param apiUri the end-point.
* @param credentialsId the existing selection;
* @return the drop-down list.
* @since 2.2.0
*/
Expand Down
Expand Up @@ -36,6 +36,8 @@
import jenkins.scm.api.SCMProbe;
import jenkins.scm.api.SCMProbeStat;
import jenkins.scm.api.SCMRevision;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.lib.Constants;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRef;
Expand All @@ -59,9 +61,11 @@ public GitHubSCMProbe(GitHub github, GHRepository repo, SCMHead head, SCMRevisio
this.name = head.getName();
if (head instanceof PullRequestSCMHead) {
PullRequestSCMHead pr = (PullRequestSCMHead) head;
this.ref = "refs/pull/" + pr.getNumber() + (pr.isMerge() ? "/merge" : "/head");
this.ref = "pull/" + pr.getNumber() + (pr.isMerge() ? "/merge" : "/head");
} else if (head instanceof GitHubTagSCMHead){
this.ref = "tags/" + head.getName();
} else {
this.ref = "refs/heads/" + head.getName();
this.ref = "heads/" + head.getName();
}
}

Expand Down Expand Up @@ -128,7 +132,7 @@ public SCMProbeStat stat(@NonNull String path) throws IOException {
checkOpen();
try {
int index = path.lastIndexOf('/') + 1;
List<GHContent> directoryContent = repo.getDirectoryContent(path.substring(0, index), ref);
List<GHContent> directoryContent = repo.getDirectoryContent(path.substring(0, index), Constants.R_REFS + ref);
for (GHContent content : directoryContent) {
if (content.getPath().equals(path)) {
if (content.isFile()) {
Expand Down

0 comments on commit 51e1535

Please sign in to comment.