Skip to content
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
46 changes: 46 additions & 0 deletions src/main/java/hudson/plugins/s3/S3BucketPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Fingerprint;
import hudson.model.Job;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.ItemListener;
import hudson.model.listeners.RunListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
Expand Down Expand Up @@ -436,6 +439,49 @@
return fileName;
}

@Extension
public static final class S3DeletedItemListener extends ItemListener {
@Override
public void onDeleted(Item item) {
if (item instanceof Job<?, ?> job) {
handleJobDeletion(job);
} else if (item instanceof ItemGroup<?> itemGroup) {
handleItemGroupDeletion(itemGroup);
}
}

private void handleJobDeletion(Job<?, ?> job) {
for (Run<?, ?> run : job.getBuilds()) {
S3ArtifactsAction artifacts = run.getAction(S3ArtifactsAction.class);
if (artifacts != null) {
S3Profile profile = S3BucketPublisher.getProfile(artifacts.getProfile());
if (profile.isDeleteArtifactsRecursively()) {
for (FingerprintRecord record : artifacts.getArtifacts()) {
if (!record.isKeepForever()) {
try {
profile.delete(run, record);
} catch (Exception e) {
Logger.getLogger(S3DeletedItemListener.class.getName())
.log(Level.WARNING, "Failed to delete S3 artifact: " + record.getName(), e);
}
}
}
}
}
}
}

private void handleItemGroupDeletion(ItemGroup<?> itemGroup) {
for (Item item : itemGroup.getItems()) {
if (item instanceof Job<?, ?> job) {
handleJobDeletion(job);
} else if (item instanceof ItemGroup<?> childItemGroup) {
handleItemGroupDeletion(childItemGroup);
}
}
}

Check warning on line 482 in src/main/java/hudson/plugins/s3/S3BucketPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 446-482 are not covered by tests
}

@Extension
public static final class S3DeletedJobListener extends RunListener<Run> {
@Override
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/hudson/plugins/s3/S3Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
private final boolean useRole;
private final int signedUrlExpirySeconds;
private boolean usePathStyle = false;
private boolean deleteArtifactsRecursively = false;

@DataBoundConstructor
public S3Profile(String name, String accessKey, String secretKey, boolean useRole, int signedUrlExpirySeconds, String maxUploadRetries, String uploadRetryTime, String maxDownloadRetries, String downloadRetryTime, boolean keepStructure) {
Expand All @@ -69,6 +70,11 @@
this.usePathStyle = usePathStyle;
}

@DataBoundSetter
public void setDeleteArtifactsRecursively(boolean deleteArtifactsRecursively) {
this.deleteArtifactsRecursively = deleteArtifactsRecursively;
}

Check warning on line 76 in src/main/java/hudson/plugins/s3/S3Profile.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 75-76 are not covered by tests

public boolean isKeepStructure() {
return keepStructure;
}
Expand Down Expand Up @@ -123,6 +129,8 @@

public boolean isUsePathStyle() { return usePathStyle; }

public boolean isDeleteArtifactsRecursively() { return deleteArtifactsRecursively; }

public S3Client getClient(String region) {
return ClientHelper.createClient(accessKey, Secret.toString(secretKey), useRole, region, getProxy(), usePathStyle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<f:entry title="Use Path Style URL" help="/plugin/s3/help-pathStyle.html">
<f:checkbox name="usePathStyle" checked="${profile.usePathStyle}"/>
</f:entry>
<f:entry title="Delete Artifacts Recursively" help="/plugin/s3/help-deleteArtifactsRecursively.html">
<f:checkbox name="deleteArtifactsRecursively" checked="${profile.deleteArtifactsRecursively}"/>
</f:entry>
</f:advanced>

<f:entry title="">
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/help-deleteArtifactsRecursively.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>If ticked deletes artifacts recursively in case of pipeline or folder deletion</div>