Skip to content

Commit

Permalink
[JENKINS-34395] Support for tag discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Sep 7, 2017
1 parent dd2db08 commit e28cd4a
Show file tree
Hide file tree
Showing 9 changed files with 522 additions and 9 deletions.
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 @@ -38,6 +38,7 @@
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import org.eclipse.jgit.lib.Constants;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;
Expand Down Expand Up @@ -136,7 +137,9 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
}
String ref;
if (head instanceof BranchSCMHead) {
ref = head.getName();
ref = Constants.R_HEADS + head.getName();
} else if (head instanceof GitHubTagSCMHead) {
ref = Constants.R_TAGS + head.getName();
} else if (head instanceof PullRequestSCMHead) {
PullRequestSCMHead pr = (PullRequestSCMHead) head;
if (!pr.isMerge() && pr.getSourceRepo() != null) {
Expand Down Expand Up @@ -181,7 +184,7 @@ 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());
rev = new AbstractGitSCMSource.SCMRevisionImpl(head, repo.getRef(ref).getObject().getSha());
}
return new GitHubSCMFileSystem(github, repo, ref, rev);
} catch (IOException | RuntimeException e) {
Expand Down

Large diffs are not rendered by default.

Expand Up @@ -23,7 +23,6 @@
*/
package org.jenkinsci.plugins.github_branch_source;

import com.google.common.base.Function;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Util;
Expand All @@ -46,6 +45,7 @@
import org.kohsuke.github.GHBranch;
import org.kohsuke.github.GHPermissionType;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRef;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

Expand Down Expand Up @@ -106,7 +106,11 @@ public class GitHubSCMSourceRequest extends SCMSourceRequest {
*/
@CheckForNull
private Iterable<GHBranch> branches;
// TODO private Iterable<GHTag> tags;
/**
* The tag details or {@code null} if not {@link #isFetchTags()}.
*/
@CheckForNull
private Iterable<GHRef> tags;
/**
* The repository collaborator names or {@code null} if not provided.
*/
Expand Down Expand Up @@ -345,6 +349,26 @@ public final Iterable<GHBranch> getBranches() {
return Util.fixNull(branches);
}

/**
* Provides the requests with the tag details.
*
* @param tags the tag details.
*/
public final void setTags(@CheckForNull Iterable<GHRef> tags) {
this.tags = tags;
}

/**
* Returns the branch details or an empty list if either the request did not specify to {@link #isFetchBranches()}
* or if the branch details have not been provided by {@link #setBranches(Iterable)} yet.
*
* @return the branch details (may be empty)
*/
@NonNull
public final Iterable<GHRef> getTags() {
return Util.fixNull(tags);
}

// TODO Iterable<GHTag> getTags() and setTags(...)

/**
Expand Down
@@ -0,0 +1,29 @@
package org.jenkinsci.plugins.github_branch_source;

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

public class GitHubTagSCMHead extends SCMHead implements TagSCMHead {

private final long timestamp;

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

/**
* {@inheritDoc}
*/
@Override
public long getTimestamp() {
return timestamp;
}
}
@@ -0,0 +1,149 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.github_branch_source;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.util.ListBoxModel;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadCategory;
import jenkins.scm.api.SCMHeadOrigin;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.trait.SCMHeadAuthority;
import jenkins.scm.api.trait.SCMHeadAuthorityDescriptor;
import jenkins.scm.api.trait.SCMHeadFilter;
import jenkins.scm.api.trait.SCMSourceContext;
import jenkins.scm.api.trait.SCMSourceRequest;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import jenkins.scm.impl.ChangeRequestSCMHeadCategory;
import jenkins.scm.impl.TagSCMHeadCategory;
import jenkins.scm.impl.trait.Discovery;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* A {@link Discovery} trait for GitHub that will discover tags on the repository.
*
* @since TODO
*/
public class TagDiscoveryTrait extends SCMSourceTrait {
/**
* Constructor for stapler.
*/
@DataBoundConstructor
public TagDiscoveryTrait() {

}

/**
* {@inheritDoc}
*/
@Override
protected void decorateContext(SCMSourceContext<?, ?> context) {
GitHubSCMSourceContext ctx = (GitHubSCMSourceContext) context;
ctx.wantTags(true);
ctx.withAuthority(new TagSCMHeadAuthority());
}

/**
* {@inheritDoc}
*/
@Override
public boolean includeCategory(@NonNull SCMHeadCategory category) {
return category instanceof TagSCMHeadCategory;
}

/**
* Our descriptor.
*/
@Extension
@Discovery
public static class DescriptorImpl extends SCMSourceTraitDescriptor {

/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return Messages.TagDiscoveryTrait_displayName();
}

/**
* {@inheritDoc}
*/
@Override
public Class<? extends SCMSourceContext> getContextClass() {
return GitHubSCMSourceContext.class;
}

/**
* {@inheritDoc}
*/
@Override
public Class<? extends SCMSource> getSourceClass() {
return GitHubSCMSource.class;
}

}

/**
* Trusts branches from the origin repository.
*/
public static class TagSCMHeadAuthority extends SCMHeadAuthority<SCMSourceRequest, GitHubTagSCMHead, SCMRevision> {
/**
* {@inheritDoc}
*/
@Override
protected boolean checkTrusted(@NonNull SCMSourceRequest request, @NonNull GitHubTagSCMHead head) {
return true;
}

/**
* Out descriptor.
*/
@Extension
public static class DescriptorImpl extends SCMHeadAuthorityDescriptor {
/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return Messages.TagDiscoveryTrait_authorityDisplayName();
}

/**
* {@inheritDoc}
*/
@Override
public boolean isApplicableToOrigin(@NonNull Class<? extends SCMHeadOrigin> originClass) {
return SCMHeadOrigin.Default.class.isAssignableFrom(originClass);
}
}
}
}
Expand Up @@ -51,3 +51,5 @@ SSHCheckoutTrait.displayName=Checkout over SSH
SSHCheckoutTrait.incompatibleCredentials=The currently configured credentials are incompatible with this behaviour
SSHCheckoutTrait.missingCredentials=The currently configured credentials cannot be found
SSHCheckoutTrait.useAgentKey=- use build agent''s key -
TagDiscoveryTrait.authorityDisplayName=Trust origin tags
TagDiscoveryTrait.displayName=Discover tags
@@ -0,0 +1,5 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:c="/lib/credentials"
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"
xmlns:f2="/org/jenkinsci/plugins/github_branch_source/form">
</j:jelly>
@@ -0,0 +1,3 @@
<div>
Discovers tags on the repository.
</div>

0 comments on commit e28cd4a

Please sign in to comment.