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

Pipeline stars #139

Open
wants to merge 3 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ freeStyleJob('test-job') {

* `Artifact Promotion Plugin For Jenkins Pipeline` - Under GSoC 2019.
Join with the Project through the following Chat Link:
* [promoted-builds-plugin](https://gitter.im/jenkinsci/promoted-builds-plugin)
* [promoted-builds-plugin](https://gitter.im/jenkinsci/promoted-builds-plugin)
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
63 changes: 34 additions & 29 deletions src/main/java/hudson/plugins/promoted_builds/JobPropertyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.security.MasterToSlaveCallable;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.StaplerRequest;

import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
Expand All @@ -35,6 +25,16 @@
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import hudson.model.Run;
import jenkins.security.MasterToSlaveCallable;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.StaplerRequest;

import hudson.Extension;
import hudson.Util;
import hudson.model.listeners.ItemListener;
import hudson.util.IOUtils;
import javax.annotation.CheckForNull;
Expand All @@ -51,7 +51,7 @@
* </p>
* @author Kohsuke Kawaguchi
*/
public final class JobPropertyImpl extends JobProperty<AbstractProject<?,?>> implements ItemGroup<PromotionProcess> {
public final class JobPropertyImpl extends JobProperty<Job<?,?>> implements ItemGroup<PromotionProcess> {
/**
* These are loaded from the disk in a different way.
*/
Expand All @@ -63,6 +63,8 @@ public final class JobPropertyImpl extends JobProperty<AbstractProject<?,?>> imp
*/
private transient /*final*/ List<PromotionProcess> activeProcesses;

private transient /*final*/ String testJobName;

/**
* These {@link PromotionProcess}es are active.
*/
Expand All @@ -71,7 +73,7 @@ public final class JobPropertyImpl extends JobProperty<AbstractProject<?,?>> imp
* Programmatic construction.
* @param owner owner job
*/
public JobPropertyImpl(AbstractProject<?,?> owner) throws Descriptor.FormException, IOException {
public JobPropertyImpl(Job<?,?> owner) throws Descriptor.FormException, IOException {
this.owner = owner;
init();
}
Expand All @@ -80,9 +82,10 @@ public JobPropertyImpl(AbstractProject<?,?> owner) throws Descriptor.FormExcepti
* @param other Property to be copied
* @param owner owner job
*/
public JobPropertyImpl(JobPropertyImpl other, AbstractProject<?,?> owner) throws Descriptor.FormException, IOException {
public JobPropertyImpl(JobPropertyImpl other, Job<?,?> owner) throws Descriptor.FormException, IOException {
this.owner = owner;
this.activeProcessNames.addAll(other.activeProcessNames);
this.testJobName = other.testJobName;
loadAllProcesses(other.getRootDir());
}

Expand All @@ -95,24 +98,23 @@ public JobPropertyImpl(Set<String> activeProcessNames) {
}

private JobPropertyImpl(StaplerRequest req, JSONObject json) throws Descriptor.FormException, IOException {
// a hack to get the owning AbstractProject.
// a hack to get the owning Run.
// this is needed here so that we can load items
List<Ancestor> ancs = req.getAncestors();
final Object ancestor = ancs.get(ancs.size()-1).getObject();
if (ancestor instanceof AbstractProject) {
owner = (AbstractProject)ancestor;
} else if (ancestor == null) {
throw new Descriptor.FormException("Cannot retrieve the ancestor item in the request",
"owner");
final Object ancestor = ancs.get(ancs.size() - 1).getObject();
if (ancestor == null) {
throw new Descriptor.FormException("Cannot retrieve the ancestor item in the request", "owner");
} else if (ancestor instanceof Job) {
owner = (Job) ancestor;
} else {
throw new Descriptor.FormException("Cannot create Promoted Builds Job Property for " + ancestor.getClass()
+ ". Currently the plugin supports instances of AbstractProject only."
+ ". Currently the plugin supports instances of Run and Pipelines."
+ ". Other job types are not supported, submit a bug to the plugin, which provides the job type"
+ ". If you use Multi-Branch Project plugin, see https://issues.jenkins-ci.org/browse/JENKINS-32237",
"owner");
}

// newer version of Hudson put "promotions". This code makes it work with or without them.
//newer version of Hudson put "promotions". This code makes it work with or without them.
if(json.has("promotions"))
json = json.getJSONObject("promotions");

Expand Down Expand Up @@ -214,7 +216,7 @@ private synchronized void safeAddToProcessesList(PromotionProcess p) {
}

@Override
protected void setOwner(AbstractProject<?,?> owner) {
protected void setOwner(Job<?,?> owner) {
super.setOwner(owner);

// readResolve is too early because we don't have our parent set yet,
Expand Down Expand Up @@ -291,6 +293,10 @@ public List<PromotionProcess> getActiveItems() {
return activeProcesses;
}

public String getTestJobName(){
return testJobName;
}

/** @see ItemGroupMixIn#createProjectFromXML */
public PromotionProcess createProcessFromXml(final String name, InputStream xml) throws IOException {
owner.checkPermission(Item.CONFIGURE); // CREATE is ItemGroup-scoped and owner is not an ItemGroup
Expand Down Expand Up @@ -323,10 +329,10 @@ public PromotionProcess createProcessFromXml(final String name, InputStream xml)
}

/**
* Gets {@link AbstractProject} that contains us.
* Gets {@link Run} that contains us.
* @return Owner project
*/
public AbstractProject<?,?> getOwner() {
public Job<?,?> getOwner() {
return owner;
}

Expand Down Expand Up @@ -392,14 +398,13 @@ public String getDisplayName() {
return "promotion";
}

@Override
public boolean prebuild(AbstractBuild<?,?> build, BuildListener listener) {
build.addAction(new PromotedBuildAction(build));
return true;
}

@Deprecated
public Action getJobAction(AbstractProject<?,?> job) {
public Action getJobAction(Job<?,?> job) {
return new PromotedProjectAction(job,this);
}

Expand All @@ -415,12 +420,12 @@ public DescriptorImpl(Class<? extends JobProperty<?>> clazz) {
}

public String getDisplayName() {
return "Promote Builds When...";
return "Promote Builds when...";
}

@Override
public boolean isApplicable(Class<? extends Job> jobType) {
return AbstractProject.class.isAssignableFrom(jobType);
return Job.class.isAssignableFrom(jobType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.Run;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
Expand Down Expand Up @@ -41,7 +42,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
console.println(Messages.KeepBuildForEverAction_console_promotionNotGoodEnough(build.getResult()));
return true;
}
AbstractBuild promoted = ((Promotion) build).getTarget();
Run promoted = ((Promotion) build).getTarget();
console.println(Messages.KeepBuildForEverAction_console_keepingBuild());
promoted.keepLog();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import hudson.RestrictedSince;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildBadgeAction;
import hudson.model.Cause.UserCause;
import hudson.model.Cause;
import hudson.model.Job;
import hudson.model.Run;
import hudson.plugins.promoted_builds.conditions.ManualCondition;
import hudson.util.CopyOnWriteList;
import org.kohsuke.stapler.HttpResponse;
Expand Down Expand Up @@ -33,18 +34,25 @@
public final class PromotedBuildAction implements BuildBadgeAction {

//TODO: bug: serialization of builds into the badge
public final AbstractBuild<?,?> owner;
public final Run<?,?> owner;

/**
* Per-process status.
*/
private final CopyOnWriteList<Status> statuses = new CopyOnWriteList<Status>();

public PromotedBuildAction(Run<?,?> owner) {
assert owner != null;
this.owner = owner;
}

@Deprecated
public PromotedBuildAction(AbstractBuild<?,?> owner) {
assert owner!=null;
this.owner = owner;
}

@Deprecated
public PromotedBuildAction(AbstractBuild<?,?> owner, Status firstStatus) {
this(owner);
statuses.add(firstStatus);
Expand All @@ -53,15 +61,15 @@ public PromotedBuildAction(AbstractBuild<?,?> owner, Status firstStatus) {
/**
* Gets the owning build.
*/
public AbstractBuild<?,?> getOwner() {
public Run<?,?> getOwner() {
return owner;
}

/**
* Gets the owning project.
*/
public AbstractProject<?,?> getProject() {
return owner.getProject();
public Job<?,?> getProject() {
return owner.getParent();
}

/**
Expand Down Expand Up @@ -241,7 +249,7 @@ public HttpResponse doForcePromotion(@QueryParameter("name") String name) throws
ManualCondition manualCondition = (ManualCondition) p.getPromotionCondition(ManualCondition.class.getName());
PromotionPermissionHelper.checkPermission(getProject(), manualCondition);

p.promote(owner,new UserCause(),new ManualPromotionBadge());
p.promote(owner, new Cause.UserIdCause(), new ManualPromotionBadge());

return HttpResponses.redirectToDot();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package hudson.plugins.promoted_builds;

import hudson.model.*;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.CheckForNull;

import hudson.model.AbstractBuild;
import hudson.model.Api;
import hudson.model.Job;
import hudson.model.PermalinkProjectAction;
import hudson.model.ProminentProjectAction;
import hudson.model.Run;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -28,10 +33,10 @@ public class PromotedProjectAction implements ProminentProjectAction, PermalinkP
//TODO externalize to a plugin property?
private static final int SUMMARY_SIZE = 10;

public final AbstractProject<?,?> owner;
public final Job<?,?> owner;
private final JobPropertyImpl property;

public PromotedProjectAction(AbstractProject<?, ?> owner, JobPropertyImpl property) {
public PromotedProjectAction(Job<?, ?> owner, JobPropertyImpl property) {
this.owner = owner;
this.property = property;
}
Expand Down Expand Up @@ -97,8 +102,8 @@ public AbstractBuild<?,?> getLatest(String name) {

public List<Promotion> getPromotions(PromotionProcess promotionProcess){
List<Promotion> list = new ArrayList<Promotion>();
for( AbstractBuild<?,?> build : owner.getBuilds() ) {
PromotedBuildAction a = build.getAction(PromotedBuildAction.class);
for(Run<?, ?> runs : owner.getBuilds() ) {
PromotedBuildAction a = runs.getAction(PromotedBuildAction.class);
if(a!=null && a.contains(promotionProcess))
list.addAll( a.getPromotionBuilds(promotionProcess) );
}
Expand Down
Loading