Skip to content

Commit

Permalink
Merge pull request #132 from Arcnor/master
Browse files Browse the repository at this point in the history
Allow setting file attachment
  • Loading branch information
KocproZ authored Sep 3, 2024
2 parents 92b22f0 + fbd7f3d commit c3a2a47
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
Expand All @@ -21,6 +22,11 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.InvalidPathException;
public class DiscordPipelineStep extends AbstractStepImpl {
private final String webhookURL;

Expand All @@ -34,6 +40,7 @@ public class DiscordPipelineStep extends AbstractStepImpl {
private String notes;
private String customAvatarUrl;
private String customUsername;
private String customFile;
private DynamicFieldContainer dynamicFieldContainer;
private boolean successful;
private boolean unstable;
Expand Down Expand Up @@ -158,6 +165,15 @@ public String getCustomUsername() {
return customUsername;
}

@DataBoundSetter
public void setCustomFile(String customFile) {
this.customFile = customFile;
}

Check warning on line 171 in src/main/java/nz/co/jammehcow/jenkinsdiscord/DiscordPipelineStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 170-171 are not covered by tests

public String getCustomFile() {
return customFile;
}

@DataBoundSetter
public void setEnableArtifactsList(boolean enable) {
this.enableArtifactsList = enable;
Expand Down Expand Up @@ -261,6 +277,11 @@ protected Void run() throws Exception {
wh.setCustomUsername(step.getCustomUsername());
}

if (step.getCustomFile() != null) {

Check warning on line 280 in src/main/java/nz/co/jammehcow/jenkinsdiscord/DiscordPipelineStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 280 is only partially covered, one branch is missing
InputStream fis = getFileInputStream(step.getCustomFile());
wh.setFile(fis, step.getCustomFile());
}

// Add all key value field pairs to the webhook
addDynamicFieldsToWebhook(wh);

Expand All @@ -273,6 +294,21 @@ protected Void run() throws Exception {
return null;
}

private InputStream getFileInputStream(String file) throws IOException, InterruptedException {
FilePath ws = getContext().get(FilePath.class);
final FilePath fp = ws.child(file);
if (fp.exists()) {
try {
return fp.read();
} catch (InvalidPathException var3) {
throw new IOException(var3);
}
} else {
String message = "No such file: " + file;
return new ByteArrayInputStream(message.getBytes(Charset.defaultCharset()));

Check warning on line 308 in src/main/java/nz/co/jammehcow/jenkinsdiscord/DiscordPipelineStep.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 281-308 are not covered by tests
}
}

/**
* Add all key value field pairs to the webhook
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@
<f:entry title="SCM Web URL" field="scmWebUrl">
<f:textbox />
</f:entry>
<f:entry title="Custom Avatar URL" field="customAvatarUrl">
<f:textbox />
</f:entry>
<f:entry title="Custom Username" field="customUsername">
<f:textbox />
</f:entry>
<f:entry title="Custom File" field="customFile">
<f:textbox />
</f:entry>
</j:jelly>

0 comments on commit c3a2a47

Please sign in to comment.