Skip to content

Commit 8facfe4

Browse files
PR comments
1 parent 931b8d6 commit 8facfe4

File tree

4 files changed

+67
-62
lines changed

4 files changed

+67
-62
lines changed

.DS_Store

-6 KB
Binary file not shown.

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

+41-33
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,11 @@ public class SupportAction implements RootAction, StaplerProxy {
107107
*/
108108
private final Logger logger = Logger.getLogger(SupportAction.class.getName());
109109

110-
private static final String SUPPORT_BUNDLE_FILE_NAME = "support-bundle.zip";
111110
private static final String SUPPORT_BUNDLE_CREATION_FOLDER = Paths.get(System.getProperty("java.io.tmpdir"))
112111
.resolve("support-bundle")
113112
.toString();
114113

115-
private static final Map<UUID, SupportBundleAsyncGenerator> generatorMap = new ConcurrentHashMap<>();
114+
private static final Map<UUID, SupportBundleAsyncGenerator> generatorByTaskId = new ConcurrentHashMap<>();
116115

117116
@Override
118117
@Restricted(NoExternalUse.class)
@@ -378,7 +377,7 @@ public HttpRedirect doGenerateAllBundlesAsync(StaplerRequest2 req, StaplerRespon
378377
UUID taskId = UUID.randomUUID();
379378
SupportBundleAsyncGenerator supportBundleAsyncGenerator = new SupportBundleAsyncGenerator();
380379
supportBundleAsyncGenerator.init(taskId, components);
381-
generatorMap.put(taskId, supportBundleAsyncGenerator);
380+
generatorByTaskId.put(taskId, supportBundleAsyncGenerator);
382381

383382
return new HttpRedirect("progressPage?taskId=" + taskId);
384383
}
@@ -506,8 +505,8 @@ public boolean isSelected() {
506505
}
507506
}
508507

509-
public ProgressiveRendering getGenerateSupportBundle(@QueryParameter String taskId) throws Exception {
510-
return generatorMap.get(UUID.fromString(taskId));
508+
public ProgressiveRendering getGeneratorByTaskId(@QueryParameter String taskId) throws Exception {
509+
return generatorByTaskId.get(UUID.fromString(taskId));
511510
}
512511

513512
public static class SupportBundleAsyncGenerator extends ProgressiveRendering {
@@ -517,6 +516,7 @@ public static class SupportBundleAsyncGenerator extends ProgressiveRendering {
517516
private String pathToBundle;
518517
private List<Component> components;
519518
private boolean supportBundleGenerationInProgress = false;
519+
private String supportBundleName;
520520

521521
public SupportBundleAsyncGenerator init(UUID taskId, List<Component> components) {
522522
this.taskId = taskId;
@@ -526,36 +526,40 @@ public SupportBundleAsyncGenerator init(UUID taskId, List<Component> components)
526526

527527
@Override
528528
protected void compute() throws Exception {
529-
if (!supportBundleGenerationInProgress) {
530-
this.supportBundleGenerationInProgress = true;
531-
logger.info("Generating support bundle... task id " + taskId);
532-
File outputDir = new File(SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId);
533-
if (!outputDir.exists()) {
534-
if (!outputDir.mkdirs()) {
535-
throw new IOException("Failed to create directory: " + outputDir.getAbsolutePath());
536-
}
529+
if (supportBundleGenerationInProgress) {
530+
return;
531+
}
532+
this.supportBundleName = BundleFileName.generate();
533+
this.supportBundleGenerationInProgress = true;
534+
// Thread.sleep(6000);
535+
logger.fine("Generating support bundle... task id " + taskId);
536+
File outputDir = new File(SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId);
537+
if (!outputDir.exists()) {
538+
if (!outputDir.mkdirs()) {
539+
throw new IOException("Failed to create directory: " + outputDir.getAbsolutePath());
537540
}
541+
}
538542

539-
try (FileOutputStream fileOutputStream =
540-
new FileOutputStream(new File(outputDir, SUPPORT_BUNDLE_FILE_NAME))) {
541-
SupportPlugin.setRequesterAuthentication(Jenkins.getAuthentication2());
542-
try {
543-
try (ACLContext ignored = ACL.as2(ACL.SYSTEM2)) {
544-
SupportPlugin.writeBundle(fileOutputStream, components);
545-
} catch (IOException e) {
546-
logger.log(Level.FINE, e.getMessage(), e);
547-
}
548-
} finally {
549-
SupportPlugin.clearRequesterAuthentication();
543+
try (FileOutputStream fileOutputStream =
544+
new FileOutputStream(new File(outputDir, supportBundleName))) {
545+
SupportPlugin.setRequesterAuthentication(Jenkins.getAuthentication2());
546+
try {
547+
try (ACLContext ignored = ACL.as2(ACL.SYSTEM2)) {
548+
SupportPlugin.writeBundle(fileOutputStream, components);
549+
} catch (IOException e) {
550+
logger.log(Level.WARNING, e.getMessage(), e);
550551
}
551552
} finally {
552-
logger.fine("Response completed");
553+
SupportPlugin.clearRequesterAuthentication();
553554
}
554-
555-
pathToBundle = outputDir.getAbsolutePath() + "/" + SUPPORT_BUNDLE_FILE_NAME;
556-
isCompleted = true;
557-
progress(1);
555+
} finally {
556+
logger.fine("Response completed");
558557
}
558+
559+
pathToBundle = outputDir.getAbsolutePath() + "/" + supportBundleName;
560+
isCompleted = true;
561+
progress(1);
562+
559563
}
560564

561565
@NonNull
@@ -564,21 +568,25 @@ protected JSON data() {
564568
JSONObject json = new JSONObject();
565569
json.put("isCompleted", isCompleted);
566570
json.put("pathToBundle", pathToBundle);
567-
json.put("status", 1);
568571
json.put("taskId", String.valueOf(taskId));
569572
return json;
570573
}
574+
575+
public String getSupportBundleName() {
576+
return supportBundleName;
577+
}
571578
}
572579

573580
public void doDownloadBundle(@QueryParameter("taskId") String taskId, StaplerResponse2 rsp) throws IOException {
574-
File bundleFile = new File(SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId + "/" + SUPPORT_BUNDLE_FILE_NAME);
581+
String supportBundleName = generatorByTaskId.get(UUID.fromString(taskId)).getSupportBundleName();
582+
File bundleFile = new File(SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId + "/" + supportBundleName);
575583
if (!bundleFile.exists()) {
576584
rsp.sendError(HttpServletResponse.SC_NOT_FOUND, "Support bundle file not found");
577585
return;
578586
}
579587

580588
rsp.setContentType("application/zip");
581-
rsp.addHeader("Content-Disposition", "attachment; filename=" + SUPPORT_BUNDLE_FILE_NAME);
589+
rsp.addHeader("Content-Disposition", "attachment; filename=" + supportBundleName);
582590
try (ServletOutputStream outputStream = rsp.getOutputStream();
583591
FileInputStream inputStream = new FileInputStream(bundleFile)) {
584592
IOUtils.copy(inputStream, outputStream);
@@ -592,7 +600,7 @@ public void doDownloadBundle(@QueryParameter("taskId") String taskId, StaplerRes
592600

593601
try {
594602
FileUtils.deleteDirectory(outputDir);
595-
generatorMap.remove(taskId);
603+
generatorByTaskId.remove(taskId);
596604
logger.fine(() -> "Cleaned up temporary directory " + outputDir);
597605

598606
} catch (IOException e) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<l:main-panel>
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>
12-
<l:progressiveRendering handler="${it.getGenerateSupportBundle(request.getParameter('taskId'))}" callback="updateProgressUI"/>
12+
<l:progressiveRendering handler="${it.getGeneratorByTaskId(request.getParameter('taskId'))}" callback="updateProgressUI"/>
1313
<l:progressAnimation/>
1414
<a id="downloadButton" style="display:none;" class="btn btn-primary" href="#">Download Support Bundle</a>
1515
</l:main-panel>

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

+25-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cloudbees.jenkins.support;
22

3+
import static org.awaitility.Awaitility.await;
34
import static org.hamcrest.MatcherAssert.assertThat;
45
import static org.hamcrest.Matchers.containsString;
56
import static org.hamcrest.Matchers.equalTo;
@@ -38,6 +39,8 @@
3839
import java.util.Collection;
3940
import java.util.Collections;
4041
import java.util.List;
42+
import java.util.concurrent.TimeUnit;
43+
import java.util.concurrent.atomic.AtomicReference;
4144
import java.util.logging.Level;
4245
import java.util.logging.LogRecord;
4346
import java.util.logging.Logger;
@@ -71,8 +74,6 @@
7174
*/
7275
public class SupportActionTest {
7376

74-
private static final Logger LOGGER = Logger.getLogger(SupportActionTest.class.getName());
75-
7677
private static final Path SUPPORT_BUNDLE_CREATION_FOLDER =
7778
Paths.get(System.getProperty("java.io.tmpdir")).resolve("support-bundle");
7879

@@ -106,7 +107,7 @@ public void generateAllBundles() throws IOException, SAXException {
106107
public void generateBundlesByUIButtonClick() throws IOException, SAXException, InterruptedException {
107108
ZipFile supportBundle = downloadSupportBundleByButtonClick();
108109
assertNotNull(supportBundle.getEntry("manifest.md"));
109-
cleanUp();
110+
cleanUpSupportBundleInTempFolder();
110111
}
111112

112113
@Test
@@ -340,25 +341,20 @@ private ZipFile downloadSupportBundleByButtonClick() throws IOException, SAXExce
340341

341342
HtmlAnchor downloadLink = null;
342343
int maxRetries = 10;
343-
int retryCount = 0;
344-
int waitTime = 1000;
345344
File zipFile;
345+
AtomicReference<HtmlPage> pageRef = new AtomicReference<>(page);
346346

347-
while (retryCount < maxRetries) {
347+
HtmlPage finalPage = page;
348+
await().timeout(10, TimeUnit.SECONDS).until(() -> {
348349
try {
349-
// Check if the download link is present
350-
downloadLink = page.getAnchorByText("Download Support Bundle");
351-
if (downloadLink != null) {
352-
break;
353-
}
350+
return pageRef.get().getAnchorByText("Download Support Bundle") != null;
354351
} catch (ElementNotFoundException e) {
355-
// If the link is not found, wait and retry
356-
page = (HtmlPage) page.refresh();
357-
Thread.sleep(waitTime);
358-
retryCount++;
352+
pageRef.set((HtmlPage) pageRef.get().refresh());
353+
return false;
359354
}
360-
}
355+
});
361356

357+
downloadLink = pageRef.get().getAnchorByText("Download Support Bundle");
362358
if (downloadLink != null) {
363359
// Download the zip file
364360
WebResponse response = downloadLink.click().getWebResponse();
@@ -461,7 +457,7 @@ public void takeSnapshotAndMakeSureSomethingHappens() throws Exception {
461457
fail(r.getMessage());
462458
}
463459
}
464-
cleanUp();
460+
cleanUpSupportBundleInTempFolder();
465461
}
466462
}
467463

@@ -644,18 +640,19 @@ private void assertBundleNotContains(ZipFile zip, Collection<String> fileNames)
644640
}
645641
}
646642

647-
public void cleanUp() {
643+
public void cleanUpSupportBundleInTempFolder() {
648644
try {
649-
if (Files.exists(SUPPORT_BUNDLE_CREATION_FOLDER)) {
650-
try (Stream<Path> paths = Files.walk(SUPPORT_BUNDLE_CREATION_FOLDER)) {
651-
paths.filter(Files::isRegularFile).forEach(path -> {
652-
try {
653-
Files.delete(path);
654-
} catch (IOException e) {
655-
throw new RuntimeException(e);
656-
}
657-
});
658-
}
645+
if (!Files.exists(SUPPORT_BUNDLE_CREATION_FOLDER)) {
646+
return;
647+
}
648+
try (Stream<Path> paths = Files.walk(SUPPORT_BUNDLE_CREATION_FOLDER)) {
649+
paths.filter(Files::isRegularFile).forEach(path -> {
650+
try {
651+
Files.delete(path);
652+
} catch (IOException e) {
653+
throw new RuntimeException(e);
654+
}
655+
});
659656
}
660657
} catch (IOException e) {
661658
throw new RuntimeException(e);

0 commit comments

Comments
 (0)