Skip to content

Commit

Permalink
[JENKINS-34395] Add some tests and update git dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Oct 23, 2017
1 parent 014230c commit 8768d73
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -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,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>
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
Expand Down
@@ -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.
Expand All @@ -15,16 +13,7 @@ public class GitHubTagSCMHead extends SCMHead implements TagSCMHead {
* @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);
}

/**
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -352,7 +353,7 @@ && isApiMatch(((GitHubSCMSource) source).getApiUri())
}
if (!excluded) {
return Collections.<SCMHead, SCMRevision>singletonMap(head,
new AbstractGitSCMSource.SCMRevisionImpl(head, push.getHead()));
new GitTagSCMRevision(head, push.getHead()));
}
}
return Collections.emptyMap();
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -115,7 +116,7 @@ public Class<? extends SCMSource> getSourceClass() {
/**
* 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}
*/
Expand Down
@@ -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 8768d73

Please sign in to comment.