Skip to content

Commit

Permalink
[FEATURE JENKINS-8296] Allow use of random persona instead of a fix one
Browse files Browse the repository at this point in the history
It would be great to allow per project use of a random persona at each launch of a build.

How :

In the configuration of a project, the "Random Persona" will be choose in the list of available personas.

At each launch of a build the plugin will verify if the choosen persona is the "Random Persona", if so this "Random Persona" will randomize the choose of one of the personas configured.

The remaining process will be the same as today.
The project quote and icon will of course reflect the last build.

Signed-off-by: Whren <whren@free.fr>
  • Loading branch information
whren authored and ssogabe committed Feb 25, 2012
1 parent bf35780 commit 2ac2d10
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/main/java/hudson/plugins/persona/QuotePublisher.java
Expand Up @@ -64,8 +64,13 @@ public Action getProjectAction(AbstractProject<?, ?> project) {

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
if (persona !=null)
build.getActions().add(persona.generateQuote(build));
if (persona !=null) {
if (persona instanceof RandomPersona) {
((RandomPersona) persona).resetCurrentPersona();
}

build.getActions().add(persona.generateQuote(build));
}
return true;
}

Expand Down
168 changes: 168 additions & 0 deletions src/main/java/hudson/plugins/persona/RandomPersona.java
@@ -0,0 +1,168 @@
/*
* The MIT License
*
* Copyright (c) 2010-2011, InfraDNA, 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 hudson.plugins.persona;

import hudson.ExtensionList;
import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.plugins.persona.simple.Image;
import hudson.plugins.persona.simple.SimplePersona;
import hudson.plugins.persona.xml.XmlBasedPersona;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* Random persona implementation on top of SimplePersona
*
* @author whren
*
*/
public class RandomPersona extends SimplePersona {
/**
* Random persona display name
*/
public static final String RANDOM_PERSONA_DISPLAYNAME = "Random Persona";

/**
* Random persona id
*/
public static final String RANDOM_PERSONA_ID = "RandomPersonaId";

/**
* Persona Randomizer
*/
private static final Random random = new Random();

private Map<AbstractBuild<?, ?>, XmlBasedPersona> mapPersonas =
new HashMap<AbstractBuild<?, ?>, XmlBasedPersona>();

private XmlBasedPersona currentPersona;

/**
* Creates a RandomPersona
*
* @return
* A newly created RandomPersona
*/
public static RandomPersona create() {
return new RandomPersona();
}

/**
* Default constructor
*/
private RandomPersona() {
super(RANDOM_PERSONA_ID, null, null, null, null);
}

/**
* {@inheritDoc}
*/
@Override
public Image getDefaultImage() {
return getCurrentPersona().getDefaultImage();
}

/**
* {@inheritDoc}
*/
@Override
public Image getImage(AbstractBuild<?, ?> build) {
return getCurrentPersona().getImage(build);
}

/**
* {@inheritDoc}
*/
public String getDisplayName() {
return RANDOM_PERSONA_DISPLAYNAME;
}

/**
* {@inheritDoc}
*/
@Override
public synchronized String getRandomQuoteText(AbstractBuild<?, ?> build) {
return getCurrentPersona().getDisplayName()
+ " - "
+ getCurrentPersona().getRandomQuoteText(build);
}

/**
* Returns the build associated persona
*
* @param build
* the build from wich to retrieve the associated persona
* @return
* persona associated to the build
*/
public XmlBasedPersona getPersona(AbstractBuild<?,?> build) {
return mapPersonas.get(build);
}

/**
* Return the current persona, generate one if non existent
*
* @return
* The current persona
*/
public XmlBasedPersona getCurrentPersona() {
if (null == currentPersona) {
currentPersona = resetCurrentPersona();
}

return currentPersona;
}

/**
* Reset the current persona and return a new one
*
* @return
* The new current persona
*/
public XmlBasedPersona resetCurrentPersona() {
currentPersona = randomPersona();
return currentPersona;
}

/**
* Returns a random persona form all personas
*
* @return
* A random persona
*/
public static XmlBasedPersona randomPersona() {
ExtensionList<XmlBasedPersona> personas = allXmlBased();
return personas.get(random.nextInt(personas.size()));
}

/**
* Returns all the registered {@link XmlBasedPersona}s.
*/
public static ExtensionList<XmlBasedPersona> allXmlBased() {
return Hudson.getInstance().getExtensionList(XmlBasedPersona.class);
}
}
58 changes: 58 additions & 0 deletions src/main/java/hudson/plugins/persona/RandomPersonaFinder.java
@@ -0,0 +1,58 @@
/*
* The MIT License
*
* Copyright (c) 2010-2011, InfraDNA, 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 hudson.plugins.persona;

import hudson.Extension;
import hudson.ExtensionComponent;
import hudson.ExtensionFinder;
import hudson.model.Hudson;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* @author whren
*
*/
@Extension
public class RandomPersonaFinder extends ExtensionFinder {
@Override
public <T> Collection<ExtensionComponent<T>> find(Class<T> type, Hudson hudson) {
if (type != Persona.class) {
return Collections.emptyList();
}

List<ExtensionComponent<RandomPersona>> r = new ArrayList<ExtensionComponent<RandomPersona>>();

parsePersonaInto(r);

return (List)r;
}

private void parsePersonaInto(Collection<ExtensionComponent<RandomPersona>> result) {
result.add(new ExtensionComponent<RandomPersona>(RandomPersona.create()));
}
}
Expand Up @@ -25,6 +25,7 @@

import hudson.model.InvisibleAction;
import hudson.plugins.persona.Quote;
import hudson.plugins.persona.RandomPersona;

/**
* Default implementation of quote that renders a simple non-localized text.
Expand All @@ -35,7 +36,11 @@ public abstract class AbstractQuoteImpl extends InvisibleAction implements Quote
public final SimplePersona persona;

public AbstractQuoteImpl(SimplePersona persona) {
this.persona = persona;
if (persona instanceof RandomPersona) {
this.persona = ((RandomPersona) persona).getCurrentPersona();
} else {
this.persona = persona;
}
}

public abstract String getQuote();
Expand Down
Expand Up @@ -51,7 +51,9 @@
public class XmlPersonaFinder extends ExtensionFinder {
@Override
public <T> Collection<ExtensionComponent<T>> find(Class<T> type, Hudson hudson) {
if (type!=Persona.class) return Collections.emptyList();
if ((type!=Persona.class) && (type != XmlBasedPersona.class)) {
return Collections.emptyList();
}

List<ExtensionComponent<XmlBasedPersona>> r = new ArrayList<ExtensionComponent<XmlBasedPersona>>();

Expand Down

0 comments on commit 2ac2d10

Please sign in to comment.