Skip to content
Permalink
Browse files
[JENKINS-34395] Add some tests and update git dependency
  • Loading branch information
stephenc committed Oct 23, 2017
1 parent 014230c commit e98acfa47bcd74c30423e5d24afff7556874022a
@@ -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,7 +46,7 @@
<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>
@@ -33,6 +33,7 @@
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;
@@ -199,7 +200,16 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
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());
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());
}
@@ -77,6 +77,7 @@
import javax.servlet.http.HttpServletResponse;
import jenkins.model.Jenkins;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.plugins.git.GitTagSCMRevision;
import jenkins.plugins.git.MergeWithGitSCMExtension;
import jenkins.plugins.git.traits.GitBrowserSCMSourceTrait;
import jenkins.scm.api.SCMHead;
@@ -1014,12 +1015,12 @@ public SCMRevision create(@NonNull PullRequestSCMHead head,
}
}
GitHubTagSCMHead head = new GitHubTagSCMHead(tagName, tagDate);
if (request.process(head, new SCMRevisionImpl(head, sha),
new SCMSourceRequest.ProbeLambda<GitHubTagSCMHead, SCMRevisionImpl>() {
if (request.process(head, new GitTagSCMRevision(head, sha),
new SCMSourceRequest.ProbeLambda<GitHubTagSCMHead, GitTagSCMRevision>() {
@NonNull
@Override
public SCMSourceCriteria.Probe create(@NonNull GitHubTagSCMHead head,
@Nullable SCMRevisionImpl revisionInfo)
@Nullable GitTagSCMRevision revisionInfo)
throws IOException, InterruptedException {
return new GitHubSCMProbe(github, ghRepository, head, revisionInfo);
}
@@ -1246,7 +1247,7 @@ protected SCMRevision retrieve(@NonNull String headName, @NonNull TaskListener l
// we just need enough of a date value to allow for probing
}
}
return new SCMRevisionImpl(new GitHubTagSCMHead(headName, tagDate), tagSha);
return new GitTagSCMRevision(new GitHubTagSCMHead(headName, tagDate), tagSha);
}
} catch (FileNotFoundException e) {
// ok it doesn't exist
@@ -1363,15 +1364,16 @@ protected SCMRevision retrieve(SCMHead head, TaskListener listener) throws IOExc
}
return new PullRequestSCMRevision(prhead, baseHash, pr.getHead().getSha());
} else if (head instanceof GitHubTagSCMHead) {
GHRef tag = ghRepository.getRef("tags/" + head.getName());
GitHubTagSCMHead tagHead = (GitHubTagSCMHead) head;
GHRef tag = ghRepository.getRef("tags/" + tagHead.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);
return new GitTagSCMRevision(tagHead, sha);
} else {
return new SCMRevisionImpl(head, ghRepository.getRef("heads/" + head.getName()).getObject().getSha());
}
@@ -1,12 +1,10 @@
package org.jenkinsci.plugins.github_branch_source;

import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.scm.api.SCMHead;
import jenkins.plugins.git.GitTagSCMHead;
import jenkins.scm.api.mixin.TagSCMHead;

public class GitHubTagSCMHead extends SCMHead implements TagSCMHead {

private final long timestamp;
public class GitHubTagSCMHead extends GitTagSCMHead implements TagSCMHead {

/**
* Constructor.
@@ -15,16 +13,7 @@
* @param timestamp the tag timestamp;
*/
public GitHubTagSCMHead(@NonNull String name, long timestamp) {
super(name);
this.timestamp = timestamp;
}

/**
* {@inheritDoc}
*/
@Override
public long getTimestamp() {
return timestamp;
super(name, timestamp);
}

/**
@@ -41,6 +41,7 @@
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.plugins.git.GitTagSCMRevision;
import jenkins.scm.api.SCMEvent;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadEvent;
@@ -352,7 +353,7 @@ public String getSourceName() {
}
if (!excluded) {
return Collections.<SCMHead, SCMRevision>singletonMap(head,
new AbstractGitSCMSource.SCMRevisionImpl(head, push.getHead()));
new GitTagSCMRevision(head, push.getHead()));
}
}
return Collections.emptyMap();
@@ -26,6 +26,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.util.ListBoxModel;
import jenkins.plugins.git.GitTagSCMRevision;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadCategory;
import jenkins.scm.api.SCMHeadOrigin;
@@ -115,7 +116,7 @@ public String getDisplayName() {
/**
* Trusts branches from the origin repository.
*/
public static class TagSCMHeadAuthority extends SCMHeadAuthority<SCMSourceRequest, GitHubTagSCMHead, SCMRevision> {
public static class TagSCMHeadAuthority extends SCMHeadAuthority<SCMSourceRequest, GitHubTagSCMHead, GitTagSCMRevision> {
/**
* {@inheritDoc}
*/
@@ -0,0 +1,58 @@
package org.jenkinsci.plugins.github_branch_source;

import java.util.Collections;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadCategory;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMHeadOrigin;
import jenkins.scm.api.mixin.ChangeRequestCheckoutStrategy;
import jenkins.scm.api.trait.SCMHeadAuthority;
import jenkins.scm.impl.ChangeRequestSCMHeadCategory;
import jenkins.scm.impl.TagSCMHeadCategory;
import jenkins.scm.impl.UncategorizedSCMHeadCategory;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;

public class TagDiscoveryTraitTest {
@ClassRule
public static JenkinsRule j = new JenkinsRule();

@Test
public void decorateContext() throws Exception {
GitHubSCMSourceContext probe = new GitHubSCMSourceContext(null, SCMHeadObserver.collect());
assertThat(probe.wantBranches(), is(false));
assertThat(probe.wantPRs(), is(false));
assertThat(probe.wantTags(), is(false));
assertThat(probe.authorities(), is(Collections.<SCMHeadAuthority<?, ?, ?>>emptyList()));
new TagDiscoveryTrait().applyToContext(probe);
assertThat(probe.wantBranches(), is(false));
assertThat(probe.wantPRs(), is(false));
assertThat(probe.wantTags(), is(true));
assertThat(probe.authorities(), contains(instanceOf(TagDiscoveryTrait.TagSCMHeadAuthority.class)));
}

@Test
public void includeCategory() throws Exception {
assertThat(new TagDiscoveryTrait().includeCategory(ChangeRequestSCMHeadCategory.DEFAULT), is(false));
assertThat(new TagDiscoveryTrait().includeCategory(UncategorizedSCMHeadCategory.DEFAULT), is(false));
assertThat(new TagDiscoveryTrait().includeCategory(TagSCMHeadCategory.DEFAULT), is(true));
}

@Test
public void authority() throws Exception {
try (GitHubSCMSourceRequest probe = new GitHubSCMSourceContext(null, SCMHeadObserver.collect()).newRequest(new GitHubSCMSource("does-not-exist","http://does-not-exist.test"), null)) {
TagDiscoveryTrait.TagSCMHeadAuthority instance = new TagDiscoveryTrait.TagSCMHeadAuthority();
assertThat(instance.isTrusted(probe, new SCMHead("v1.0.0")), is(false));
assertThat(instance.isTrusted(probe, new PullRequestSCMHead("PR-1", "does-not-exists",
"http://does-not-exist.test", "feature/1", 1, new BranchSCMHead("master"), SCMHeadOrigin.DEFAULT, ChangeRequestCheckoutStrategy.MERGE)), is(false));
assertThat(instance.isTrusted(probe, new GitHubTagSCMHead("v1.0.0", 0L)), is(true));
}
}

}

0 comments on commit e98acfa

Please sign in to comment.