forked from baloise/rocket-chat-notifier
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for per-build webhook configuration
Add field to build for webhook token or URL. This overrides any saved authentication and channel data and can only post to a single channel. Update `RocketChatClientImpl` and `RocketChatClientCallBuilder` to take webhook token and invoke a new authenticator for webhooks. Related: JENKINS-42099
- Loading branch information
1 parent
8efccc4
commit c7a0696
Showing
6 changed files
with
177 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/jenkins/plugins/rocketchatnotifier/RocketClientWebhookImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package jenkins.plugins.rocketchatnotifier; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import jenkins.plugins.rocketchatnotifier.rocket.RocketChatClient; | ||
import jenkins.plugins.rocketchatnotifier.rocket.RocketChatClientImpl; | ||
import sun.security.validator.ValidatorException; | ||
|
||
public class RocketClientWebhookImpl implements RocketClient { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(RocketClientImpl.class.getName()); | ||
|
||
private RocketChatClient client; | ||
|
||
public RocketClientWebhookImpl(String serverUrl, boolean trustSSL, String token) { | ||
client = new RocketChatClientImpl(serverUrl, trustSSL, token); | ||
} | ||
|
||
public boolean publish(String message) { | ||
try { | ||
LOGGER.fine("Starting sending message to webhook"); | ||
this.client.send("", message); | ||
return true; | ||
} catch (IOException e) { | ||
LOGGER.log(Level.SEVERE, "I/O error error during publishing message", e); | ||
return false; | ||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Unknown error error during publishing message", e); | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean publish(final String message, final String emoji, final String avatar) { | ||
try { | ||
LOGGER.fine("Starting sending message to webhook"); | ||
this.client.send("", message, emoji, avatar); | ||
return true; | ||
} catch (IOException e) { | ||
LOGGER.log(Level.SEVERE, "I/O error error during publishing message", e); | ||
return false; | ||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Unknown error error during publishing message", e); | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean publish(final String message, final String emoji, final String avatar, final List<Map<String, Object>> attachments) { | ||
try { | ||
LOGGER.fine("Starting sending message to webhook"); | ||
this.client.send("", message, emoji, avatar, attachments); | ||
return true; | ||
} catch (IOException e) { | ||
LOGGER.log(Level.SEVERE, "I/O error error during publishing message", e); | ||
return false; | ||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Unknown error error during publishing message", e); | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public void validate() throws ValidatorException, IOException { | ||
this.client.send("", "Test message from Jenkins via Webhook"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/jenkins/plugins/rocketchatnotifier/rocket/RocketChatWebhookAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package jenkins.plugins.rocketchatnotifier.rocket; | ||
|
||
import java.io.IOException; | ||
import com.mashape.unirest.request.HttpRequest; | ||
|
||
public class RocketChatWebhookAuthentication implements RocketChatCallAuthentication { | ||
private static final String HOOKS_PATH = "hooks/"; | ||
|
||
private String serverUrl; | ||
private String webhookUrl; | ||
|
||
public RocketChatWebhookAuthentication(String serverUrl, String webhookToken) { | ||
super(); | ||
this.serverUrl = serverUrl + (serverUrl.endsWith("/") ? "" : "/"); | ||
this.webhookUrl = webhookToken.contains(HOOKS_PATH) ? webhookToken : (this.serverUrl + HOOKS_PATH + webhookToken); | ||
} | ||
|
||
@Override | ||
public boolean isAuthenticated() { | ||
// No authentication needed | ||
return true; | ||
} | ||
|
||
@Override | ||
public void doAuthentication() throws IOException { | ||
// No authentication needed | ||
} | ||
|
||
@Override | ||
public String getUrlForRequest(RocketChatRestApiV1 call) { | ||
switch (call) { | ||
case Info: | ||
return serverUrl + "/api/" + call.getMethodName(); | ||
case PostMessage: | ||
return webhookUrl; | ||
default: | ||
throw new UnsupportedOperationException("Unable to handle request " + call.name() + " using webhook"); | ||
} | ||
} | ||
|
||
@Override | ||
public void addAuthenticationDataToRequest(HttpRequest request) { | ||
// No authentication needed | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters