Skip to content

Commit

Permalink
[JENKINS-34395] Ensure that lightweight checkout works for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Sep 7, 2017
1 parent 909ce64 commit 6f9ee58
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
Expand Up @@ -89,28 +89,29 @@ private Object metadata() throws IOException {
try {
switch (info) {
case DIRECTORY_ASSUMED:
metadata = repo.getDirectoryContent(getPath(), Constants.R_REFS + 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(), Constants.R_REFS + ref);
metadata = repo.getDirectoryContent(getPath(), ref.indexOf('/') == -1 ? ref : Constants.R_REFS + ref);
resolved = true;
break;
case NON_DIRECTORY_CONFIRMED:
metadata = repo.getFileContent(getPath(), Constants.R_REFS + ref);
metadata = repo.getFileContent(getPath(), ref.indexOf('/') == -1 ? ref : Constants.R_REFS + ref);
resolved = true;
break;
case UNRESOLVED:
checkOpen();
try {
metadata = repo.getFileContent(getPath(), Constants.R_REFS + 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(), Constants.R_REFS + ref);
metadata = repo.getDirectoryContent(getPath(),
ref.indexOf('/') == -1 ? ref : Constants.R_REFS + ref);
info = TypeInfo.DIRECTORY_CONFIRMED;
resolved = true;
} else {
Expand Down
Expand Up @@ -40,8 +40,10 @@
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 Down Expand Up @@ -194,7 +196,13 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
return null;
}
if (rev == null) {
rev = new AbstractGitSCMSource.SCMRevisionImpl(head, repo.getRef(refName).getObject().getSha());
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, refName, rev);
} catch (IOException | RuntimeException e) {
Expand Down
Expand Up @@ -990,27 +990,29 @@ public SCMRevision create(@NonNull PullRequestSCMHead head,
listener.getLogger().format("%n Checking tag %s%n", HyperlinkNote
.encodeTo(repositoryUrl + "/tree/" + tagName, tagName));
long tagDate = 0L;
String tagSha = tag.getObject().getSha();
String sha = tag.getObject().getSha();
if ("tag".equalsIgnoreCase(tag.getObject().getType())) {
// annotated tag object
try {
GHTagObject tagObject = request.getRepository().getTagObject(tagSha);
GHTagObject tagObject = request.getRepository().getTagObject(sha);
tagDate = tagObject.getTagger().getDate().getTime();
// we want the sha of the tagged commit not the tag object
sha = tagObject.getObject().getSha();
} catch (IOException e) {
// ignore, if the tag doesn't exist, the probe will handle that correctly
// we just need enough of a date value to allow for probing
}
} else {
try {
GHCommit commit = request.getRepository().getCommit(tagSha);
GHCommit commit = request.getRepository().getCommit(sha);
tagDate = commit.getCommitDate().getTime();
} catch (IOException e) {
// ignore, if the tag doesn't exist, the probe will handle that correctly
// we just need enough of a date value to allow for probing
}
}
GitHubTagSCMHead head = new GitHubTagSCMHead(tagName, tagDate);
if (request.process(head, new SCMRevisionImpl(head, tagSha),
if (request.process(head, new SCMRevisionImpl(head, sha),
new SCMSourceRequest.ProbeLambda<GitHubTagSCMHead, SCMRevisionImpl>() {
@NonNull
@Override
Expand Down Expand Up @@ -1359,8 +1361,15 @@ protected SCMRevision retrieve(SCMHead head, TaskListener listener) throws IOExc
}
return new PullRequestSCMRevision(prhead, baseHash, pr.getHead().getSha());
} else if (head instanceof GitHubTagSCMHead) {
// TODO verify
return new SCMRevisionImpl(head, ghRepository.getRef("tags/" + head.getName()).getObject().getSha());
GHRef tag = ghRepository.getRef("tags/" + head.getName());
String sha = tag.getObject().getSha();
if ("tag".equalsIgnoreCase(tag.getObject().getType())) {
// annotated tag object
GHTagObject tagObject = ghRepository.getTagObject(sha);
// we want the sha of the tagged commit not the tag object
sha = tagObject.getObject().getSha();
}
return new SCMRevisionImpl(head, sha);
} else {
return new SCMRevisionImpl(head, ghRepository.getRef("heads/" + head.getName()).getObject().getSha());
}
Expand Down

0 comments on commit 6f9ee58

Please sign in to comment.