Skip to content
Permalink
Browse files
Merge pull request #158 from stephenc/jenkins-34395
[JENKINS-34395] Support for tag discovery
  • Loading branch information
stephenc committed Nov 7, 2017
2 parents e6e740d + 67e8211 commit 2fc52660e0f095d7fe8b4f8e28eca95fc4bf50c2
@@ -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>
@@ -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.89</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -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;

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

@@ -49,7 +54,16 @@
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;
@@ -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;
}
}

@@ -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) {
@@ -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;
@@ -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
*/
@@ -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;
@@ -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();
}
}

@@ -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()) {

0 comments on commit 2fc5266

Please sign in to comment.