Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try 2 - Promoted Build Cleanup, Audit trailing #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.398</version>
<version>1.430</version>
</parent>

<artifactId>promoted-builds-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* The MIT License
*
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Alan Harder
*
* 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.promoted_builds_simple;

import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;

/**
*
* @author gcampb2
*/
public class CleanupPolicy {

private int count = -1;
private PromotionLevel targetLevel = null;
private PromotionLevel triggerLevel = null;

@DataBoundConstructor
public CleanupPolicy(PromotionLevel targetLevel, int count, PromotionLevel triggerLevel) {
this.count = count;
this.targetLevel = targetLevel;
this.triggerLevel = triggerLevel;
}

public CleanupPolicy(JSONObject json) {
this.count = json.getInt("count");
this.targetLevel = PromoteAction.getAllPromotionLevels().get(json.getInt("targetLevel") - 1);
this.triggerLevel = PromoteAction.getAllPromotionLevels().get(json.getInt("triggerLevel") - 1);
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public PromotionLevel getTargetLevel() {
return targetLevel;
}

public void setTargetLevel(PromotionLevel targetLevel) {
this.targetLevel = targetLevel;
}

public PromotionLevel getTriggerLevel() {
return triggerLevel;
}

public void setTriggerLevel(PromotionLevel triggerLevel) {
this.triggerLevel = triggerLevel;
}
}
127 changes: 127 additions & 0 deletions src/main/java/hudson/plugins/promoted_builds_simple/Housekeeper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* The MIT License
*
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Alan Harder
*
* 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.promoted_builds_simple;

import hudson.Extension;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import hudson.model.Run;
import hudson.util.RunList;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;

/**
*
* @author gcampb2
*/
public class Housekeeper extends JobProperty<Job<?, ?>> {

private List<CleanupPolicy> policies;

private Housekeeper(StaplerRequest req, JSONObject json)
throws Descriptor.FormException, IOException {
Object raw = json.get("policies");

JSONArray array = null;
if (raw instanceof JSONArray) {
array = (JSONArray)raw;
} else {
array = new JSONArray();
array.add((JSONObject) raw);
}

for (Object o : array) {
JSONObject c = (JSONObject) o;
addPolicy(new CleanupPolicy(c));
}
}

public List<CleanupPolicy> getPolicies() {
return policies;
}

public void setPolicies(List<CleanupPolicy> policies) {
this.policies = policies;
}

public final void addPolicy(CleanupPolicy policy) {
if (this.policies == null) {
this.policies = new ArrayList<CleanupPolicy>();
}

this.policies.add(policy);
}

public void clean(StaplerRequest req, PromotionLevel newPromotion)
throws IOException {
Job job = req.findAncestorObject(Job.class);
Run run = req.findAncestorObject(Run.class);

for (CleanupPolicy policy : policies) {
if (policy.getTriggerLevel().getName().equals(newPromotion.getName())) {
int skip = policy.getCount();
RunList rl = job.getBuilds();
for (ListIterator itr = rl.listIterator(rl.indexOf(run)); itr.hasNext();) {
Run old = (Run) itr.next();
PromoteAction oldPromote = (PromoteAction) old.getAction(PromoteAction.class);
if (oldPromote != null && oldPromote.getLevel() != null) {
if (skip > 0 && oldPromote.getLevel().equals(newPromotion.getName())) {
skip--;
} else if (oldPromote.getLevel().equals(policy.getTargetLevel().getName())) {
old.keepLog(false);
}
}
}
}
}
}

@Extension
public static class DescriptorImpl extends JobPropertyDescriptor {

@Override
public String getDisplayName() {
return "Promoted Build cleanup";
}

@Override
public Housekeeper newInstance(StaplerRequest req, JSONObject json) throws Descriptor.FormException {
try {
if (json.has("cleanupPromotions")) {
return new Housekeeper(req, json.getJSONObject("cleanupPromotions"));
}
return null;
} catch (IOException e) {
throw new FormException("Failed to create", e, null); // TODO:hmm
}
}
}
}
110 changes: 91 additions & 19 deletions src/main/java/hudson/plugins/promoted_builds_simple/PromoteAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
package hudson.plugins.promoted_builds_simple;

import hudson.PluginWrapper;
import hudson.model.AbstractProject;
import hudson.model.BuildBadgeAction;
import hudson.model.Hudson;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.User;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -41,30 +44,50 @@
* Store promotion level for a build.
* @author Alan Harder
*/
@ExportedBean(defaultVisibility=2)
@ExportedBean(defaultVisibility = 2)
public class PromoteAction implements BuildBadgeAction {

private String level, icon;
private int levelValue;
public List<PromoteCause> causes = new ArrayList<PromoteCause>();

public PromoteAction() { }
public PromoteAction() {
}

/* Action methods */
public String getUrlName() { return "promote"; }
public String getDisplayName() { return ""; }
public String getIconFileName() { return null; }
public String getUrlName() {
return "promote";
}

public String getDisplayName() {
return "";
}

public String getIconFileName() {
return null;
}

/* Promotion details */
@Exported public String getLevel() { return level; }
@Exported public int getLevelValue() { return levelValue; }
@Exported
public String getLevel() {
return level;
}

@Exported
public int getLevelValue() {
return levelValue;
}

public String getIconPath() {
if (icon == null || icon.startsWith("/")) return icon;
if (icon == null || icon.startsWith("/")) {
return icon;
}
// Try plugin images dir, fallback to main images dir
PluginWrapper wrapper =
Hudson.getInstance().getPluginManager().getPlugin(PromotedBuildsSimplePlugin.class);
Hudson.getInstance().getPluginManager().getPlugin(PromotedBuildsSimplePlugin.class);
return new File(wrapper.baseResourceURL.getPath() + "/images/" + icon).exists()
? "/plugin/" + wrapper.getShortName() + "/images/" + icon
: Hudson.RESOURCE_PATH + "/images/16x16/" + icon;
? "/plugin/" + wrapper.getShortName() + "/images/" + icon
: Hudson.RESOURCE_PATH + "/images/16x16/" + icon;
}

public static List<PromotionLevel> getAllPromotionLevels() {
Expand All @@ -74,21 +97,70 @@ public static List<PromotionLevel> getAllPromotionLevels() {
/* Save change to promotion level for this build and redirect back to build page */
public void doIndex(StaplerRequest req, StaplerResponse rsp)
throws IOException, ServletException {
req.findAncestorObject(Job.class).checkPermission(Run.UPDATE);

Job j = req.findAncestorObject(Job.class);
j.checkPermission(Run.UPDATE);

if (this.causes == null) {
this.causes = new ArrayList<PromoteCause>();
}

Run run = req.findAncestorObject(Run.class);
levelValue = Integer.parseInt(req.getParameter("level"));
PromotionLevel src = null;
if (levelValue == 0) {
level = icon = null;
req.findAncestorObject(Run.class).save();
run.save();
} else {
PromotionLevel src = getAllPromotionLevels().get(levelValue - 1);
src = getAllPromotionLevels().get(levelValue - 1);
level = src.getName();
icon = src.getIcon();
// Mark as keep-forever when promoting; this also does save()
if (src.isAutoKeep())
req.findAncestorObject(Run.class).keepLog(true);
else
req.findAncestorObject(Run.class).save();

Housekeeper hk = (Housekeeper) j.getProperty(Housekeeper.class);
if (hk != null) {
hk.clean(req, src);
}
}

boolean skip = false;
String user = Hudson.getAuthentication().getName();

PromotedPermalinkProjectAction permalinkAction = (PromotedPermalinkProjectAction) j.getAction(PromotedPermalinkProjectAction.class);
if (permalinkAction == null) {
permalinkAction = new PromotedPermalinkProjectAction((AbstractProject) j);
}
permalinkAction.registerPromotion(level, run.number);


if (!this.causes.isEmpty()) {
PromoteCause last = this.causes.get(this.causes.size() - 1);
if ((last.levelName == null && level == null)
|| (last.levelName != null && last.levelName.equals(level) && last.getUserName().equals(user))) {
// double-tap? submit during Jenkins startup?
skip = true;
}
}

if (!skip) {
PromoteCause pc = new PromoteCause(user, run, levelValue, level);
this.causes.add(pc);
User u = Hudson.getInstance().getUser(user);
if (u != null) {
UserPromotion up = u.getProperty(UserPromotion.class);
if (up == null) {
up = new UserPromotion(u);
}
up.addPromotion(pc);
}
}

// Mark as keep-forever when promoting; this also does save()
if (src != null && src.isAutoKeep()) {
run.keepLog(true);
} else {
run.save();
}

rsp.forwardToPreviousPage(req);
}
}
Loading