Search in sources :

Example 1 with ManualCondition

use of hudson.plugins.promoted_builds.conditions.ManualCondition in project promoted-builds-plugin by jenkinsci.

the class ManualConditionInheritanceTest method testManualPromotionProcess.

@Test
public void testManualPromotionProcess() throws Exception {
    InheritanceProjectsPair inheritanceProjectsPair = j.createInheritanceProjectDerivedWithBase();
    ExtensionList<Descriptor> list = Jenkins.get().getExtensionList(Descriptor.class);
    list.add(new JobPropertyImpl.DescriptorImpl(JobPropertyImpl.class));
    JobPropertyImpl base = new JobPropertyImpl(inheritanceProjectsPair.getBase());
    inheritanceProjectsPair.getBase().addProperty(base);
    PromotionProcess foo = base.addProcess("foo");
    ManualCondition condition = new ManualCondition();
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_1", "bogus_value_1", "Bog parameter"));
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_2", "bogus_value_2", "Bog parameter"));
    foo.conditions.add(condition);
    InheritanceBuild b1 = j.assertBuildStatusSuccess(inheritanceProjectsPair.getDerived().scheduleBuild2(0));
    // promote a build
    List<ParameterValue> paramValues = condition.createDefaultValues();
    // try to add duplicate values
    paramValues.addAll(condition.createDefaultValues());
    // We cannot assume that the process will contain builds because the process added to base project is different to the one in derived.
    JobPropertyImpl jobProperty = inheritanceProjectsPair.getDerived().getProperty(JobPropertyImpl.class, /*Forcing inheritance as temporary hack for inheritance plugin 1.53 
                because that version of the plugin uses inheritance only for certain predefined cases: 
                -specific methods on the call stack
                -url paths.
                This has been changed as pull request https://github.com/i-m-c/jenkins-inheritance-plugin/pull/40
               */
    IMode.INHERIT_FORCED);
    assertNotNull("derived jobProperty is null", jobProperty);
    PromotionProcess fooDerived = jobProperty.getItem("foo");
    j.assertBuildStatusSuccess(condition.approve(b1, fooDerived, paramValues));
    ManualApproval manualApproval = b1.getAction(ManualApproval.class);
    assertNotNull(manualApproval);
    PromotedBuildAction statuses = b1.getAction(PromotedBuildAction.class);
    assertNotNull(statuses);
    assertNotNull(statuses.getPromotions());
    assertFalse(statuses.getPromotions().isEmpty());
}
Also used : InheritanceProjectsPair(hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectsPair) ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) InheritanceBuild(hudson.plugins.project_inheritance.projects.InheritanceBuild) StringParameterDefinition(hudson.model.StringParameterDefinition) ParameterValue(hudson.model.ParameterValue) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) Descriptor(hudson.model.Descriptor) Test(org.junit.Test)

Example 2 with ManualCondition

use of hudson.plugins.promoted_builds.conditions.ManualCondition in project promoted-builds-plugin by jenkinsci.

the class PromotedEnvVarTokenMacroTest method testEnvironmentVariableExpansion.

@Test
public void testEnvironmentVariableExpansion() throws Exception {
    // Assemble
    r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
    User u = User.get("foo");
    u.setFullName("Foobar");
    SecurityContextHolder.getContext().setAuthentication(u.impersonate());
    MockFolder parent = r.createFolder("Folder");
    FreeStyleProject project = parent.createProject(FreeStyleProject.class, "Project");
    JobPropertyImpl promotionProperty = new JobPropertyImpl(project);
    PromotionProcess promotionProcess = promotionProperty.addProcess("promo");
    promotionProcess.conditions.clear();
    ManualCondition manualCondition = new ManualCondition();
    manualCondition.getParameterDefinitions().add(new StringParameterDefinition("PROMOTION_PARAM", "defaultValue"));
    promotionProcess.conditions.add(manualCondition);
    Action approvalAction = new ManualCondition.ManualApproval(promotionProcess.getName(), new LinkedList<ParameterValue>());
    TokenMacroExpressionRecorder recorder = new TokenMacroExpressionRecorder("${PROMOTION_ENV,var=\"PROMOTION_PARAM\"}");
    promotionProcess.getBuildSteps().add(recorder);
    // Act & promote
    FreeStyleBuild build = project.scheduleBuild2(0).get();
    build.addAction(approvalAction);
    build.save();
    Promotion promotion = promotionProcess.considerPromotion2(build, Arrays.asList((ParameterValue) new StringParameterValue("PROMOTION_PARAM", "FOO"))).get();
    // Check results
    EnvVars env = promotion.getEnvironment(TaskListener.NULL);
    assertEquals("The PROMOTION_PARAM variable has not been injected", "FOO", env.get("PROMOTION_PARAM"));
    assertEquals("The promotion variable value has not been resolved by the PROMOTION_PARAM macro", "FOO", recorder.getCaptured());
}
Also used : Action(hudson.model.Action) User(hudson.model.User) StringParameterDefinition(hudson.model.StringParameterDefinition) StringParameterValue(hudson.model.StringParameterValue) ParameterValue(hudson.model.ParameterValue) StringParameterValue(hudson.model.StringParameterValue) FreeStyleBuild(hudson.model.FreeStyleBuild) MockFolder(org.jvnet.hudson.test.MockFolder) FreeStyleProject(hudson.model.FreeStyleProject) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Promotion(hudson.plugins.promoted_builds.Promotion) ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) EnvVars(hudson.EnvVars) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) Test(org.junit.Test)

Example 3 with ManualCondition

use of hudson.plugins.promoted_builds.conditions.ManualCondition in project promoted-builds-plugin by jenkinsci.

the class PromotionEnvironmentVariablesTest method shouldSetJobAndJobFullNames.

@Test
public void shouldSetJobAndJobFullNames() throws Descriptor.FormException, IOException, InterruptedException, ExecutionException, Exception {
    // Assemble
    r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
    User u = User.get("foo");
    u.setFullName("Foobar");
    SecurityContextHolder.getContext().setAuthentication(u.impersonate());
    MockFolder parent = r.createFolder("Folder");
    FreeStyleProject project = parent.createProject(FreeStyleProject.class, "Project");
    JobPropertyImpl promotionProperty = new JobPropertyImpl(project);
    PromotionProcess promotionProcess = promotionProperty.addProcess("promo");
    promotionProcess.conditions.clear();
    promotionProcess.conditions.add(new ManualCondition());
    Action approvalAction = new ManualCondition.ManualApproval(promotionProcess.getName(), new ArrayList<ParameterValue>());
    // Act
    FreeStyleBuild build = project.scheduleBuild2(0).get();
    build.setDisplayName("1234");
    build.addAction(approvalAction);
    build.save();
    Promotion promotion = promotionProcess.considerPromotion2(build).get();
    EnvVars env = promotion.getEnvironment(TaskListener.NULL);
    // Assert
    assertEquals("Folder/Project", env.get("PROMOTED_JOB_FULL_NAME"));
    assertEquals("Project", env.get("PROMOTED_JOB_NAME"));
    assertEquals("Foobar", env.get("PROMOTED_USER_NAME"));
    assertEquals("foo", env.get("PROMOTED_USER_ID"));
    assertEquals("1234", env.get("PROMOTED_DISPLAY_NAME"));
    project.delete();
    parent.delete();
}
Also used : Action(hudson.model.Action) User(hudson.model.User) ParameterValue(hudson.model.ParameterValue) FreeStyleBuild(hudson.model.FreeStyleBuild) MockFolder(org.jvnet.hudson.test.MockFolder) FreeStyleProject(hudson.model.FreeStyleProject) ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) EnvVars(hudson.EnvVars) Test(org.junit.Test)

Example 4 with ManualCondition

use of hudson.plugins.promoted_builds.conditions.ManualCondition in project promoted-builds-plugin by jenkinsci.

the class PromotedBuildAction method doForcePromotion.

/**
 * Force a promotion.
 */
@RequirePOST
public HttpResponse doForcePromotion(@QueryParameter("name") String name) throws IOException {
    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);
    ManualCondition manualCondition = (ManualCondition) p.getPromotionCondition(ManualCondition.class.getName());
    PromotionPermissionHelper.checkPermission(getProject(), manualCondition);
    p.promote(owner, new UserCause(), new ManualPromotionBadge());
    return HttpResponses.redirectToDot();
}
Also used : ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) UserCause(hudson.model.Cause.UserCause) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 5 with ManualCondition

use of hudson.plugins.promoted_builds.conditions.ManualCondition in project promoted-builds-plugin by jenkinsci.

the class PromotedBuildAction method canPromote.

@Restricted(NoExternalUse.class)
public boolean canPromote(String processName) {
    PromotionProcess process = getPromotionProcess(processName);
    ManualCondition manualCondition = null;
    if (process != null) {
        manualCondition = (ManualCondition) process.getPromotionCondition(ManualCondition.class.getName());
    }
    return PromotionPermissionHelper.hasPermission(getProject(), manualCondition);
}
Also used : ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) Restricted(org.kohsuke.accmod.Restricted)

Aggregations

ManualCondition (hudson.plugins.promoted_builds.conditions.ManualCondition)12 ParameterValue (hudson.model.ParameterValue)7 Test (org.junit.Test)5 FreeStyleBuild (hudson.model.FreeStyleBuild)3 FreeStyleProject (hudson.model.FreeStyleProject)3 ParameterDefinition (hudson.model.ParameterDefinition)3 StringParameterDefinition (hudson.model.StringParameterDefinition)3 JobPropertyImpl (hudson.plugins.promoted_builds.JobPropertyImpl)3 PromotionProcess (hudson.plugins.promoted_builds.PromotionProcess)3 ManualApproval (hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval)3 ArrayList (java.util.ArrayList)3 EnvVars (hudson.EnvVars)2 Action (hudson.model.Action)2 UserCause (hudson.model.Cause.UserCause)2 Descriptor (hudson.model.Descriptor)2 User (hudson.model.User)2 InheritanceBuild (hudson.plugins.project_inheritance.projects.InheritanceBuild)2 PromotedBuildAction (hudson.plugins.promoted_builds.PromotedBuildAction)2 Promotion (hudson.plugins.promoted_builds.Promotion)2 InheritanceProjectsPair (hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectsPair)2