Skip to content

Commit 931b8d6

Browse files
mvn spotless apply
1 parent 261fcc2 commit 931b8d6

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

src/main/java/com/cloudbees/jenkins/support/SupportAction.java

+24-22
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import edu.umd.cs.findbugs.annotations.NonNull;
3535
import hudson.Extension;
3636
import hudson.Functions;
37-
import hudson.Main;
3837
import hudson.model.Api;
3938
import hudson.model.Failure;
4039
import hudson.model.RootAction;
@@ -43,6 +42,7 @@
4342
import hudson.security.Permission;
4443
import jakarta.servlet.ServletException;
4544
import jakarta.servlet.ServletOutputStream;
45+
import jakarta.servlet.http.HttpServletResponse;
4646
import java.io.File;
4747
import java.io.FileInputStream;
4848
import java.io.FileOutputStream;
@@ -66,8 +66,6 @@
6666
import java.util.stream.Collectors;
6767
import java.util.zip.ZipEntry;
6868
import java.util.zip.ZipOutputStream;
69-
70-
import jakarta.servlet.http.HttpServletResponse;
7169
import jenkins.model.Jenkins;
7270
import jenkins.util.ProgressiveRendering;
7371
import jenkins.util.Timer;
@@ -110,11 +108,12 @@ public class SupportAction implements RootAction, StaplerProxy {
110108
private final Logger logger = Logger.getLogger(SupportAction.class.getName());
111109

112110
private static final String SUPPORT_BUNDLE_FILE_NAME = "support-bundle.zip";
113-
private static final String SUPPORT_BUNDLE_CREATION_FOLDER = Paths.get(System.getProperty("java.io.tmpdir")).resolve("support-bundle").toString();
111+
private static final String SUPPORT_BUNDLE_CREATION_FOLDER = Paths.get(System.getProperty("java.io.tmpdir"))
112+
.resolve("support-bundle")
113+
.toString();
114114

115115
private static final Map<UUID, SupportBundleAsyncGenerator> generatorMap = new ConcurrentHashMap<>();
116116

117-
118117
@Override
119118
@Restricted(NoExternalUse.class)
120119
public Object getTarget() {
@@ -345,7 +344,6 @@ public void doGenerateAllBundles(StaplerRequest2 req, StaplerResponse2 rsp) thro
345344
prepareBundle(rsp, components);
346345
}
347346

348-
349347
/**
350348
* Generates a support bundle with selected components from the UI. in async
351349
* @param req The stapler request
@@ -354,31 +352,32 @@ public void doGenerateAllBundles(StaplerRequest2 req, StaplerResponse2 rsp) thro
354352
* @throws IOException If an input or output exception occurs
355353
*/
356354
@RequirePOST
357-
public HttpRedirect doGenerateAllBundlesAsync(StaplerRequest2 req, StaplerResponse2 rsp) throws ServletException, IOException {
355+
public HttpRedirect doGenerateAllBundlesAsync(StaplerRequest2 req, StaplerResponse2 rsp)
356+
throws ServletException, IOException {
358357
JSONObject json = req.getSubmittedForm();
359358
if (!json.has("components")) {
360359
rsp.sendError(SC_BAD_REQUEST);
361360
return new HttpRedirect("support");
362361
}
363362
final List<Component> components = getComponents(req, json);
364363

365-
for(Component component:components ){
366-
if(component instanceof AboutBrowser){
364+
for (Component component : components) {
365+
if (component instanceof AboutBrowser) {
367366
AboutBrowser aboutBrowser = (AboutBrowser) component;
368367
aboutBrowser.setScreenResolution(Functions.getScreenResolution());
369368
aboutBrowser.setCurrentRequest(Stapler.getCurrentRequest2());
370369
aboutBrowser.setGeneratedAsync(true);
371370
}
372371

373-
if(component instanceof ReverseProxy){
372+
if (component instanceof ReverseProxy) {
374373
ReverseProxy reverseProxy = (ReverseProxy) component;
375374
reverseProxy.setCurrentRequest(Stapler.getCurrentRequest2());
376375
}
377376
}
378377

379378
UUID taskId = UUID.randomUUID();
380379
SupportBundleAsyncGenerator supportBundleAsyncGenerator = new SupportBundleAsyncGenerator();
381-
supportBundleAsyncGenerator.init(taskId,components);
380+
supportBundleAsyncGenerator.init(taskId, components);
382381
generatorMap.put(taskId, supportBundleAsyncGenerator);
383382

384383
return new HttpRedirect("progressPage?taskId=" + taskId);
@@ -511,31 +510,34 @@ public ProgressiveRendering getGenerateSupportBundle(@QueryParameter String task
511510
return generatorMap.get(UUID.fromString(taskId));
512511
}
513512

514-
public static class SupportBundleAsyncGenerator extends ProgressiveRendering{
513+
public static class SupportBundleAsyncGenerator extends ProgressiveRendering {
515514
private final Logger logger = Logger.getLogger(SupportBundleAsyncGenerator.class.getName());
516515
private UUID taskId;
517516
private boolean isCompleted;
518517
private String pathToBundle;
519518
private List<Component> components;
520519
private boolean supportBundleGenerationInProgress = false;
521520

522-
public SupportBundleAsyncGenerator init( UUID taskId,List<Component> components) {
521+
public SupportBundleAsyncGenerator init(UUID taskId, List<Component> components) {
523522
this.taskId = taskId;
524523
this.components = components;
525524
return this;
526525
}
527526

528527
@Override
529528
protected void compute() throws Exception {
530-
if(!supportBundleGenerationInProgress) {
529+
if (!supportBundleGenerationInProgress) {
531530
this.supportBundleGenerationInProgress = true;
532-
logger.info("Generating support bundle... task id "+ taskId);
531+
logger.info("Generating support bundle... task id " + taskId);
533532
File outputDir = new File(SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId);
534533
if (!outputDir.exists()) {
535-
outputDir.mkdirs();
534+
if (!outputDir.mkdirs()) {
535+
throw new IOException("Failed to create directory: " + outputDir.getAbsolutePath());
536+
}
536537
}
537538

538-
try (FileOutputStream fileOutputStream = new FileOutputStream(new File(outputDir, SUPPORT_BUNDLE_FILE_NAME))) {
539+
try (FileOutputStream fileOutputStream =
540+
new FileOutputStream(new File(outputDir, SUPPORT_BUNDLE_FILE_NAME))) {
539541
SupportPlugin.setRequesterAuthentication(Jenkins.getAuthentication2());
540542
try {
541543
try (ACLContext ignored = ACL.as2(ACL.SYSTEM2)) {
@@ -568,25 +570,25 @@ protected JSON data() {
568570
}
569571
}
570572

571-
572573
public void doDownloadBundle(@QueryParameter("taskId") String taskId, StaplerResponse2 rsp) throws IOException {
573-
File bundleFile = new File(SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId + "/"+SUPPORT_BUNDLE_FILE_NAME);
574+
File bundleFile = new File(SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId + "/" + SUPPORT_BUNDLE_FILE_NAME);
574575
if (!bundleFile.exists()) {
575576
rsp.sendError(HttpServletResponse.SC_NOT_FOUND, "Support bundle file not found");
576577
return;
577578
}
578579

579580
rsp.setContentType("application/zip");
580-
rsp.addHeader("Content-Disposition", "attachment; filename="+SUPPORT_BUNDLE_FILE_NAME);
581-
try (ServletOutputStream outputStream = rsp.getOutputStream(); FileInputStream inputStream = new FileInputStream(bundleFile)) {
581+
rsp.addHeader("Content-Disposition", "attachment; filename=" + SUPPORT_BUNDLE_FILE_NAME);
582+
try (ServletOutputStream outputStream = rsp.getOutputStream();
583+
FileInputStream inputStream = new FileInputStream(bundleFile)) {
582584
IOUtils.copy(inputStream, outputStream);
583585
}
584586

585587
// Clean up temporary files after assembling the full bundle
586588
Timer.get()
587589
.schedule(
588590
() -> {
589-
File outputDir = new File(SUPPORT_BUNDLE_CREATION_FOLDER+ "/" + taskId);
591+
File outputDir = new File(SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId);
590592

591593
try {
592594
FileUtils.deleteDirectory(outputDir);

src/main/resources/com/cloudbees/jenkins/support/SupportAction/progressPage.jelly

+9
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010
<h2>Generating Support Bundle</h2>
1111
<p id="progressMessage">Generating a support bundle for this Jenkins instance. This may take a few minutes.</p>
1212
<l:progressiveRendering handler="${it.getGenerateSupportBundle(request.getParameter('taskId'))}" callback="updateProgressUI"/>
13+
<l:progressAnimation/>
1314
<a id="downloadButton" style="display:none;" class="btn btn-primary" href="#">Download Support Bundle</a>
1415
</l:main-panel>
1516

1617
<script>
1718
function updateProgressUI(data) {
19+
var progressBar = document.querySelector('.app-progress-bar');
20+
if (progressBar) {
21+
progressBar.style.display = 'none';
22+
}
1823
if (data.isCompleted === true) {
24+
var ellipsisProgressBar = document.querySelector('.lds-ellipsis');
25+
if (ellipsisProgressBar) {
26+
ellipsisProgressBar.style.display = 'none';
27+
}
1928
downloadButton.style.display = "block";
2029
downloadButton.href = "downloadBundle?taskId=" + data.taskId; // Update this path accordingly
2130
document.getElementById("progressMessage").textContent = "Support bundle has been generated.";

src/test/java/com/cloudbees/jenkins/support/SupportActionTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,14 @@ public void download() throws IOException, SAXException {
9797
downloadBundle("/download?json={\"components\":1}");
9898
}
9999

100-
101100
@Test
102101
public void generateAllBundles() throws IOException, SAXException {
103102
downloadBundle("/generateAllBundles?json={\"components\":1}");
104103
}
105104

106105
@Test
107106
public void generateBundlesByUIButtonClick() throws IOException, SAXException, InterruptedException {
108-
ZipFile supportBundle = downloadSupportBundleByButtonClick();
107+
ZipFile supportBundle = downloadSupportBundleByButtonClick();
109108
assertNotNull(supportBundle.getEntry("manifest.md"));
110109
cleanUp();
111110
}

0 commit comments

Comments
 (0)