Skip to content

Commit

Permalink
[JENKINS-59697] Implement an Apply button
Browse files Browse the repository at this point in the history
Custom version to reload the page and verify the connection status
  • Loading branch information
aheritier committed Oct 11, 2019
1 parent 50fb6f3 commit 20eb065
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Expand Up @@ -24,6 +24,7 @@
import jenkins.model.Jenkins;
import jenkins.util.io.OnMaster;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -154,17 +155,18 @@ public void setLastBundleResult(String lastBundleResult) {
@RequirePOST
@Nonnull
@Restricted(NoExternalUse.class)

public HttpResponse doConfigure(@Nonnull StaplerRequest req) {
Jenkins jenkins = Jenkins.get();
jenkins.checkPermission(Jenkins.ADMINISTER);
try {
isValid = configureDescriptor(req, req.getSubmittedForm(), getDescriptor());
save();

return HttpResponses.redirectTo(isValid
? req.getContextPath() + "/manage"
: req.getContextPath() + "/" + getUrlName());
// We want to refresh the page to reload the status even when we click on "Apply"
if (!isValid || StringUtils.isNotBlank(req.getParameter("advisor:apply"))) {
return HttpResponses.redirectToDot();
} else {
return HttpResponses.redirectTo(req.getContextPath() + "/manage");
}
} catch (Exception e) {
isValid = false;
LOG.severe("Unable to save Jenkins Health Advisor by CloudBees configuration: " + Functions.printThrowable(e));
Expand Down Expand Up @@ -378,7 +380,7 @@ public FormValidation doTestConnection(@QueryParameter("email") final String ema
return FormValidation.error("Client error : " + e.getMessage());
}
}

// Used from validateOnLoad.jelly
public String connectionTest(String email) {
AdvisorGlobalConfiguration config = AdvisorGlobalConfiguration.getInstance();
Expand Down
Expand Up @@ -103,6 +103,7 @@

<f:bottomButtonBar>
<f:submit name="save" value="${%Save}"/>
<input name="advisor:apply" type="submit" value="Apply" class="submit-button"/>
</f:bottomButtonBar>

</f:form>
Expand Down
Expand Up @@ -152,14 +152,14 @@ public void testConfigure() throws Exception {
doConfigure.setUp("", "");
HttpRedirect hr1 = (HttpRedirect) j.executeOnServer(doConfigure);
String url1 = Whitebox.getInternalState(hr1, "url");
assertEquals("Rerouted back to configuration", "/jenkins/cloudbees-jenkins-advisor", url1);
assertEquals("Rerouted back to configuration", ".", url1);

// Didn't accept Terms of Service - send back to main page
doConfigure.setUp(email, email);
doConfigure.setTerms(false);
HttpRedirect hr2 = (HttpRedirect) j.executeOnServer(doConfigure);
String url2 = Whitebox.getInternalState(hr2, "url");
assertEquals("Rerouted back to configuration", "/jenkins/cloudbees-jenkins-advisor", url2);
assertEquals("Rerouted back to configuration", ".", url2);

// Redirect to main page
doConfigure.setTerms(true);
Expand Down

0 comments on commit 20eb065

Please sign in to comment.