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

Rewrite rebuild actions using javascript java injection #580

Merged
merged 2 commits into from
Feb 4, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import hudson.model.Action;
import hudson.model.BallColor;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Item;
import hudson.model.Queue;
import hudson.security.Permission;
import hudson.util.HttpResponses;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONObject;
import org.jenkins.ui.icon.IconSpec;
import org.jenkinsci.plugins.workflow.cps.replay.ReplayAction;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.interceptor.RequirePOST;

public abstract class AbstractPipelineViewAction implements Action, IconSpec {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
Expand Down Expand Up @@ -47,14 +51,24 @@
return run.getDisplayName();
}

public boolean isParameterized() {
ParametersAction paramAction = run.getAction(ParametersAction.class);
if (paramAction != null && !paramAction.getAllParameters().isEmpty()) {
/**
* Handles the rebuild request using ReplayAction feature
*/
@RequirePOST
@JavaScriptMethod
public boolean doRebuild() throws IOException, ExecutionException {
if (run != null) {
run.checkAnyPermission(Item.BUILD);
ReplayAction replayAction = run.getAction(ReplayAction.class);
Queue.Item item =
replayAction.run2(replayAction.getOriginalScript(), replayAction.getOriginalLoadedScripts());

if (item == null) {
return false;
}
return true;
}

ParametersDefinitionProperty property = run.getParent().getProperty(ParametersDefinitionProperty.class);
return property != null && !property.getParameterDefinitions().isEmpty();
return false;

Check warning on line 71 in src/main/java/io/jenkins/plugins/pipelinegraphview/utils/AbstractPipelineViewAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 60-71 are not covered by tests
}

public String getFullBuildDisplayName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:p="/lib/pipeline-graph-view">
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout" xmlns:p="/lib/pipeline-graph-view">
<l:layout title="${%Graph} [${it.fullBuildDisplayName}]" type="one-column">
<l:main-panel>
<div class="jenkins-app-bar">
Expand All @@ -26,8 +26,11 @@
<div class="jenkins-app-bar__controls">
<j:if test="${it.buildable}">
<l:hasPermission permission="${it.permission}">
<button id="pgv-rebuild" data-success-message="${%Build scheduled}" data-build-path="../../build"
data-parameterized="${it.parameterized}" class="jenkins-button jenkins-!-build-color">
<j:set var="proxyId" value="${h.generateId()}" />
<st:bind value="${it}" var="rebuildAction${proxyId}"/>
<button id="pgv-rebuild" data-success-message="${%Build scheduled}"
data-proxy-name="rebuildAction${proxyId}"
class="jenkins-button jenkins-!-build-color">
<l:icon src="symbol-play-outline plugin-ionicons-api"/>
${%Rebuild}
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:dd="/lib/layout/dropdowns">
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout" xmlns:dd="/lib/layout/dropdowns">
<l:layout title="${%Build log} [${it.buildDisplayName}]" type="one-column">
<l:main-panel>
<div class="jenkins-app-bar">
Expand All @@ -24,8 +24,10 @@
<div class="jenkins-app-bar__controls">
<j:if test="${it.buildable}">
<l:hasPermission permission="${it.permission}">
<j:set var="proxyId" value="${h.generateId()}" />
<st:bind value="${it}" var="rebuildAction${proxyId}"/>
<button id="pgv-rebuild" data-success-message="${%Build scheduled}"
data-build-path="../../build" data-parameterized="${it.parameterized}"
data-proxy-name="rebuildAction${proxyId}"
class="jenkins-button jenkins-!-build-color">
<l:icon src="symbol-play-outline plugin-ionicons-api"/>
${%Rebuild}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
<l:app-bar title="${%Build} ${it.jobDisplayName}">
<j:if test="${it.buildable}">
<l:hasPermission permission="${it.permission}">
<button id="pgv-rebuild" data-success-message="${%Build scheduled}" data-build-path="../build"
data-parameterized="${it.parameterized}" class="jenkins-button jenkins-!-build-color">
<j:set var="proxyId" value="${h.generateId()}" />
<st:bind value="${it}" var="rebuildAction${proxyId}"/>
<button id="pgv-rebuild" data-success-message="${%Build scheduled}"
data-proxy-name="rebuildAction${proxyId}"
class="jenkins-button jenkins-!-build-color">
<l:icon src="symbol-play-outline plugin-ionicons-api"/>
${%Build}
</button>
Expand Down
23 changes: 7 additions & 16 deletions src/main/webapp/js/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,12 @@ const rebuildButton = document.getElementById('pgv-rebuild');
if (rebuildButton) {
rebuildButton.addEventListener('click', event => {
event.preventDefault();
const buildUrl = `${rebuildButton.dataset.buildPath}?delay=0sec`
if (rebuildButton.dataset.parameterized === 'true') {
window.location.href = buildUrl
} else {
fetch(buildUrl, {
method: 'post',
headers: crumb.wrap({})
})
.then(res => {
if (!res.ok) {
console.error('Build failed', res);
} else {
window.hoverNotification(rebuildButton.dataset.successMessage, rebuildButton);
}
})
}
const rebuildAction = window[`${rebuildButton.dataset.proxyName}`];
rebuildAction.doRebuild(function (success) {
const result = success.responseJSON;
if (result) {
window.hoverNotification(rebuildButton.dataset.successMessage, rebuildButton);
}
});
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public void rebuildButtonRedirectsForParameterizedJob() throws Exception {

HtmlPage page = webClient.getPage(run, new PipelineGraphViewAction(run).getUrlName());
HtmlPage newPage = page.getElementById("pgv-rebuild").click();
assertEquals(
j.getURL() + run.getParent().getUrl() + "build?delay=0sec",
newPage.getBaseURL().toExternalForm());
assertEquals(page.getBaseURL(), newPage.getBaseURL());
}
}
}