Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-34395] Support for tag discovery #158

Merged
merged 12 commits into from Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-api</artifactId>
<version>1.86</version>
<version>1.87-20170901.115941-1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Expand Up @@ -119,6 +119,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 GitHubTagSCMHead) {
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 @@ -38,8 +38,12 @@
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 +53,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 +75,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 +147,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 +196,15 @@ 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());
rev = new AbstractGitSCMSource.SCMRevisionImpl(head, 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