use of hudson.model.Cause.UserCause in project promoted-builds-plugin by jenkinsci.
the class PromotionProcess method considerPromotion2.
@CheckForNull
public Future<Promotion> considerPromotion2(AbstractBuild<?, ?> build, List<ParameterValue> params) throws IOException {
if (!isActive())
// not active
return null;
PromotedBuildAction a = build.getAction(PromotedBuildAction.class);
// if it's already promoted, no need to do anything.
if (a != null && a.contains(this))
return null;
LOGGER.fine("Considering the promotion of " + build + " via " + getName() + " with parameters");
Status qualification = isMet(build);
if (qualification == null)
// not this time
return null;
LOGGER.fine("Promotion condition of " + build + " is met: " + qualification);
// TODO: define promotion cause
Future<Promotion> f = promote2(build, new UserCause(), qualification, params);
if (f == null)
LOGGER.warning(build + " qualifies for a promotion but the queueing failed.");
return f;
}
use of hudson.model.Cause.UserCause in project promoted-builds-plugin by jenkinsci.
the class Status method doBuild.
/**
* Schedules a new build.
* @param req Request
* @param rsp Response
* @throws IOException Functional error
* @throws ServletException Request handling error
*/
public void doBuild(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
final PromotionProcess process = getProcess();
if (process == null) {
throw new AbortException("Cannot retrieve the promotion process");
}
AbstractBuild<?, ?> target = getTarget();
if (target == null) {
throw new AbortException("Cannot get the target build to be promoted");
}
ManualCondition manualCondition = (ManualCondition) process.getPromotionCondition(ManualCondition.class.getName());
if (!target.hasPermission(Promotion.PROMOTE)) {
if (manualCondition == null || (!manualCondition.getUsersAsSet().isEmpty() && !manualCondition.isInUsersList() && !manualCondition.isInGroupList()))
return;
}
JSONObject formData = req.getSubmittedForm();
List<ParameterValue> paramValues = null;
if (formData != null) {
paramValues = new ArrayList<ParameterValue>();
if (manualCondition != null) {
List<ParameterDefinition> parameterDefinitions = manualCondition.getParameterDefinitions();
if (parameterDefinitions != null && !parameterDefinitions.isEmpty()) {
JSONArray a = JSONArray.fromObject(formData.get("parameter"));
for (Object o : a) {
JSONObject jo = (JSONObject) o;
String name = jo.getString("name");
ParameterDefinition d = manualCondition.getParameterDefinition(name);
if (d == null)
throw new IllegalArgumentException("No such parameter definition: " + name);
paramValues.add(d.createValue(req, jo));
}
}
}
}
if (paramValues == null) {
paramValues = new ArrayList<ParameterValue>();
}
Future<Promotion> f = process.scheduleBuild2(target, new UserCause(), paramValues);
if (f == null)
LOGGER.warning("Failing to schedule the promotion of " + target);
// TODO: we need better visual feed back so that the user knows that the build happened.
rsp.forwardToPreviousPage(req);
}
use of hudson.model.Cause.UserCause in project promoted-builds-plugin by jenkinsci.
the class PromotedBuildActionTest method testDeletedPromotionProcess.
public void testDeletedPromotionProcess() throws Exception {
FreeStyleProject p = createFreeStyleProject();
JobPropertyImpl base = new JobPropertyImpl(p);
p.addProperty(base);
PromotionProcess foo = base.addProcess("foo");
// promote a build
FreeStyleBuild b1 = assertBuildStatusSuccess(p.scheduleBuild2(0));
foo.promote(b1, new UserCause(), new ManualPromotionBadge());
// now delete the promotion process
p.removeProperty(base);
p.addProperty(base = new JobPropertyImpl(p));
assertTrue(base.getActiveItems().isEmpty());
// make sure that the page renders OK without any error
HtmlPage page = createWebClient().getPage(p);
List<?> candidates = page.getByXPath("//IMG");
for (Object candidate : candidates) {
if (!(candidate instanceof HtmlImage)) {
continue;
}
HtmlImage img = (HtmlImage) candidate;
try {
img.getHeight();
} catch (IOException e) {
throw new IOException2("Failed to load " + img.getSrcAttribute(), e);
}
}
}
use of hudson.model.Cause.UserCause in project promoted-builds-plugin by jenkinsci.
the class PromotedBuildAction method doForcePromotion.
/**
* Force a promotion.
*/
public HttpResponse doForcePromotion(@QueryParameter("name") String name) throws IOException {
// if(!req.getMethod().equals("POST")) {// require post,
// rsp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
// return;
// }
this.getProject().checkPermission(Promotion.PROMOTE);
JobPropertyImpl pp = getProject().getProperty(JobPropertyImpl.class);
if (pp == null)
throw new IllegalStateException("This project doesn't have any promotion criteria set");
PromotionProcess p = pp.getItem(name);
if (p == null)
throw new IllegalStateException("This project doesn't have the promotion criterion called " + name);
p.promote(owner, new UserCause(), new ManualPromotionBadge());
return HttpResponses.redirectToDot();
}
use of hudson.model.Cause.UserCause in project promoted-builds-plugin by jenkinsci.
the class Promotion method getUserId.
/**
* Gets ID of the {@link User}, who triggered the promotion.
* The method tries various sources like {@link UserIdCause} or {@link ManualCondition.Badge}.
* @return ID of the user who triggered the promotion.
* If the search fails, returns ID of {@link User#getUnknown()}.
* @since 2.22
*/
@Nonnull
public String getUserId() {
// Deprecated, but we still want to support it in order to maintain the compatiibility
// We try to convert the cause to the user ID by using a search by the full name, not reliable
final UserCause userCause = getCause(UserCause.class);
final String nameFromUserCause = userCause != null ? userCause.getUserName() : null;
final User user = nameFromUserCause != null ? User.get(nameFromUserCause, false, null) : null;
if (user != null) {
return user.getId();
}
// Modern UserIdCause
final UserIdCause userIdCause = getCause(UserIdCause.class);
final String idFromUserIdCause = userIdCause != null ? userIdCause.getUserId() : null;
if (idFromUserIdCause != null) {
return idFromUserIdCause;
}
//fallback to badge lookup for compatibility
for (PromotionBadge badget : getStatus().getBadges()) {
if (badget instanceof ManualCondition.Badge) {
final String idFromBadge = ((ManualCondition.Badge) badget).getUserId();
if (!idFromBadge.equals(ManualCondition.MISSING_USER_ID_DISPLAY_STRING)) {
return idFromBadge;
}
}
}
return User.getUnknown().getId();
}
Aggregations