Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #7 from jglick/SCMHead-Actionable-JENKINS-33309
[JENKINS-33309] Allow SCMHead to have actions, and define ChangeRequestAction
  • Loading branch information
stephenc committed Mar 9, 2016
2 parents b822371 + 5c01ff7 commit 891ef06
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/main/java/jenkins/scm/api/SCMHead.java
Expand Up @@ -25,19 +25,35 @@

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Action;
import hudson.model.Actionable;
import hudson.model.Item;
import hudson.model.ItemGroup;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import jenkins.model.TransientActionFactory;
import jenkins.scm.api.actions.ChangeRequestAction;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

/**
* Represents a named SCM branch, tag or mainline.
*
* @author Stephen Connolly
*/
@ExportedBean
public class SCMHead implements Comparable<SCMHead>, Serializable {

private static final Logger LOGGER = Logger.getLogger(SCMHead.class.getName());

/**
* Ensure consistent serialization.
*/
Expand All @@ -64,6 +80,7 @@ public SCMHead(@NonNull String name) {
*
* @return the name.
*/
@Exported
@NonNull
public String getName() {
return name;
Expand Down Expand Up @@ -115,6 +132,54 @@ public String toString() {
return sb.toString();
}

/**
* Gets all actions used to decorate the behavior of this branch.
* May be overridden to create a new list, perhaps with additions.
* @return a list of all actions associated with this branch (by default, an unmodifiable list searching {@link TransientActionFactory}s)
* @see Actionable#getAllActions
* @since FIXME
*/
@NonNull
@Exported(name="actions")
public List<? extends Action> getAllActions() {
List<Action> actions = new ArrayList<Action>();
Jenkins j = Jenkins.getInstance(); // TODO 1.572+ ExtensionList.lookup
if (j != null) {
for (TransientActionFactory<?> taf : j.getExtensionList(TransientActionFactory.class)) {
if (taf.type().isInstance(this)) {
try {
actions.addAll(createFor(taf));
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Could not load actions from " + taf + " for " + this, e);
}
}
}
}
return Collections.unmodifiableList(actions);
}
private <T> Collection<? extends Action> createFor(TransientActionFactory<T> taf) {
return taf.createFor(taf.type().cast(this));
}

/**
* Gets a specific action used to decorate the behavior of this branch.
* May be overridden but suffices to override {@link #getAllActions}.
* @param <T> a desired action type to query, such as {@link ChangeRequestAction}
* @param type type token
* @return an instance of that action interface (by default, filters {@link #getAllActions})
* @see Actionable#getAction(Class)
* @since FIXME
*/
@CheckForNull
public <T extends Action> T getAction(@NonNull Class<T> type) {
for (Action action : getAllActions()) {
if (type.isInstance(action)) {
return type.cast(action);
}
}
return null;
}

/**
* Means of locating a head given an item.
* @since FIXME
Expand Down
126 changes: 126 additions & 0 deletions src/main/java/jenkins/scm/api/actions/ChangeRequestAction.java
@@ -0,0 +1,126 @@
/*
* The MIT License
*
* Copyright 2016 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 jenkins.scm.api.actions;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.model.InvisibleAction;
import hudson.model.TaskListener;
import java.io.Serializable;
import java.net.URL;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

/**
* Indicates that an {@link SCMHead} represents a registered proposed change, such as a pull request.
* <p>Intended to capture concepts common to popular code review systems and which might warrant generic UI representation.
* Fields may be null in case the corresponding concept does not exist in the system being represented.
* <p>Should be restricted to those aspects of a change which are either immutable
* or otherwise not affected by changes to the head {@link SCMRevision}
* (as opposed to, say, mergeability status).
* Should also be restricted to short metadata which can be quickly retrieved during {@link SCMSource#retrieve(SCMHeadObserver, TaskListener)}.
* @see SCMHead#getAllActions
* @since FIXME
*/
@ExportedBean
public abstract class ChangeRequestAction extends InvisibleAction implements Serializable {

private static final long serialVersionUID = 1L;

/**
* Identifier of this change request.
* Expected to be unique among requests coming from a given {@link SCMSource}.
* @return an ID of some kind, such as a pull request number (decimal) or a Gerrit change ID
*/
@Exported
@CheckForNull
public String getId() {
return null;
}

/**
* Link to web representation of change.
* @return an HTTP(S) permalink
*/
@Exported
@CheckForNull
public URL getURL() {
return null;
}

/**
* Display title.
* @return a summary message
*/
@Exported
@CheckForNull
public String getTitle() {
return null;
}

/**
* Username of author of the proposed change.
* @return a user login name or other unique user identifier
*/
@Exported
@CheckForNull
public String getAuthor() {
return null;
}

/**
* Human name of author of proposed change.
* @return First M. Last, etc.
*/
@Exported
@CheckForNull
public String getAuthorDisplayName() {
return null;
}

/**
* Email address of author of proposed change.
* @return a valid email address
*/
@Exported
@CheckForNull
public String getAuthorEmail() {
return null;
}

/**
* Branch to which this change would be merged or applied if it were accepted.
* @return a “target” or “base” branch
*/
@Exported
@CheckForNull
public SCMHead getTarget() {
return null;
}

}

0 comments on commit 891ef06

Please sign in to comment.