Skip to content

Commit

Permalink
Remove reaction (#769)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Jacomb <[email protected]>
Co-authored-by: stephane <[email protected]>
  • Loading branch information
3 people authored Sep 20, 2022
1 parent 21f0d78 commit aeebb16
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ slackResponse.addReaction("thumbsup")

![image][img-emoji-reaction]

You can remove an emoji reaction to a previously-sent message like this:

Example:

```groovy
def slackResponse = slackSend(channel: "emoji-demo", message: "Here is the primary message")
slackResponse.addReaction("thumbsup")
// ... do some stuff
slackResponse.removeReaction("thumbsup")
```

This may only work reliably in channels (as opposed to private messages) due to [limitations in the Slack API](https://api.slack.com/methods/chat.postMessage) (See "Post to an IM channel").

This does not currently work in a situation where Jenkins is restarted between sending the initial message and adding the reaction. If this is something you need, please file an issue.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/jenkins/plugins/slack/SlackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,15 @@ public interface SlackService {

boolean addReaction(String channelId, String timestamp, String emojiName);

/**
* Remove an emoji reaction to a message.
* @param channelId - Slack's internal channel id (i.e. what's returned in a `chat.postMessage` response)
* @param timestamp - Timestamp identifying the message
* @param emojiName - The name of the emoji to add in reaction to the message (no colons)
*
* @return boolean indicating whether the API request succeeded
*/
boolean removeReaction(String channelId, String timestamp, String emojiName);

String getResponseString();
}
16 changes: 16 additions & 0 deletions src/main/java/jenkins/plugins/slack/StandardSlackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,22 @@ public boolean addReaction(String channelId, String timestamp, String emojiName)
return postToSlack("reactions.add", json);
}

/**
* Remove an emoji reaction from a message.
*/
@Override
public boolean removeReaction(String channelId, String timestamp, String emojiName) {
JSONObject json = SlackReactionRequest.builder()
.withChannelId(channelId)
.withTimestamp(timestamp)
.withEmojiName(emojiName)
.build()
.getBody();

logger.fine("Removing reaction: " + json.toString());
return postToSlack("reactions.remove", json);
}

private String getTokenToUse(String authTokenCredentialId, String token) {
if (!StringUtils.isEmpty(authTokenCredentialId)) {
StringCredentials credentials = CredentialsObtainer.lookupCredentials(authTokenCredentialId);
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/jenkins/plugins/slack/workflow/SlackResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@ public boolean addReaction(String emojiName) {

return slackService.addReaction(channelId, ts, emojiName);
}

/**
* Remove an emoji reaction to the message that this `SlackResponse` points to.
*
* @param emojiName - name of the emoji (no colons), e.g. `thumbsup`
*
* @return boolean indicating whether the API request succeeded
*/
@Whitelisted
public boolean removeReaction(String emojiName) {
if (slackService == null) {
return false;
}

return slackService.removeReaction(channelId, ts, emojiName);
}
}
5 changes: 5 additions & 0 deletions src/test/java/jenkins/plugins/slack/SlackNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public boolean addReaction(String channelId, String timestamp, String emojiName)
return response;
}

@Override
public boolean removeReaction(String channelId, String timestamp, String emojiName) {
return response;
}

public void setResponse(boolean response) {
this.response = response;
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/jenkins/plugins/slack/StandardSlackServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,20 @@ public void usernameAndNotBotUserReturnsTrue() {
assertTrue(service.publish("message"));
}

@Test
public void shouldRemoveAReaction() {
StandardSlackServiceStub service = new StandardSlackServiceStub(
StandardSlackService.builder()
.withBaseUrl("")
.withTeamDomain("domain")
.withBotUser(true)
.withRoomId("#room1")
.withPopulatedToken("token")
.withUsername("username"));
CloseableHttpClientStub httpClientStub = new CloseableHttpClientStub();
httpClientStub.setHttpStatus(HttpStatus.SC_OK);
service.setHttpClient(httpClientStub);
assertTrue(service.removeReaction("#my-room", "12345", "thumbup"));
}

}

0 comments on commit aeebb16

Please sign in to comment.