Skip to content
This repository was archived by the owner on Feb 27, 2025. It is now read-only.

Updating the allbegray.slack-api dependency to the official Slack Java SDK - com.slack.api #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
import java.util.List;
import java.util.stream.Collectors;

import allbegray.slack.webapi.SlackWebApiClient;
import allbegray.slack.webapi.method.chats.ChatPostMessageMethod;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
Expand Down Expand Up @@ -151,18 +152,18 @@ private boolean hasChildTasks(Task task) {
@SuppressWarnings("rawtypes")
private void sendNotificationToChannel(Notifier notification, NotifierHelper helper) {
SlackClient client = (SlackClient) clientFactory.getNotificationClient(notification);
SlackWebApiClient apiClient = client.getClient();
ChatPostMessageMethod method = helper.populateSlackMessage();
MethodsClient apiClient = client.getClient();
ChatPostMessageRequest method = helper.populateSlackMessage();
method.setUsername(notification.getSlackInfo().getSenderName());
for (String channel : notification.getSlackInfo().getChannels()) {
try {
method.setChannel(channel);
apiClient.postMessage(method);
apiClient.chatPostMessage(method);
} catch (Exception e) {
log.error("Not able to send notification to slack : {}, causes : {}", notification.getName(),
e.getMessage());
}
}
client.shutdown(apiClient);
client.shutdown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2016-2019 VMware, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product may include a number of subcomponents with separate copyright notices
* and license terms. Your use of these subcomponents is subject to the terms and
* conditions of the subcomponent's license, as noted in the LICENSE file.
*/

package com.vmware.mangle.services.helpers.slack;

public enum Color {
GOOD("#007a5a"),
WARNING("#daa038"),
DANGER("#e01e5a");

public final String hexValue;

Color(String hexValue) {
this.hexValue = hexValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
import java.util.ArrayList;
import java.util.List;

import allbegray.slack.type.Attachment;
import allbegray.slack.type.Color;
import allbegray.slack.type.Field;
import allbegray.slack.webapi.method.chats.ChatPostMessageMethod;
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
import com.slack.api.model.Field;
import com.slack.api.model.Attachment;

import com.vmware.mangle.cassandra.model.faults.specs.CommandExecutionFaultSpec;
import com.vmware.mangle.cassandra.model.faults.specs.JVMAgentFaultSpec;
Expand Down Expand Up @@ -49,14 +48,9 @@ public NotifierHelper(T task) {
this.trigger = task.getTriggers().peek();
}

public ChatPostMessageMethod populateSlackMessage() {
ChatPostMessageMethod method = new ChatPostMessageMethod(DEFAULT_CHANNEL_NAME,
CommonConstants.SLACK_HELLO_MSG + commandExecutionFaultSpec.getEndpointName() + "*");
method.setUsername(Constants.DEFAULT_SENDER_NAME);
method.setAs_user(false);
List<Attachment> attachments = method.getAttachments();
public ChatPostMessageRequest populateSlackMessage() {
List<Attachment> attachments = new ArrayList<>();
Attachment attachment = new Attachment();
attachments.add(attachment);
updateAttachment(attachment);
List<Field> fields = new ArrayList<>();
attachment.setFields(fields);
Expand All @@ -70,17 +64,25 @@ public ChatPostMessageMethod populateSlackMessage() {
updateFaultEndTime(fields);
if (trigger.getTaskStatus().equals(TaskStatus.FAILED)) {
addField(fields, CommonConstants.SLACK_MSG_FAULT_FAILURE_REASON, trigger.getTaskFailureReason());
fields.get(fields.size() - 1).set_short(false);
fields.get(fields.size() - 1).setValueShortEnough(false);
}
addField(fields, CommonConstants.SLACK_MSG_FAULT_DESCRIPTION, task.getTaskDescription());
fields.get(fields.size() - 1).set_short(false);
return method;
fields.get(fields.size() - 1).setValueShortEnough(false);

attachment.setFallback(CommonConstants.SLACK_HELLO_MSG + commandExecutionFaultSpec.getEndpointName() + "*");
attachments.add(attachment);
ChatPostMessageRequest chatPostMessageRequest = ChatPostMessageRequest.builder()
.username(Constants.DEFAULT_SENDER_NAME)
.text(CommonConstants.SLACK_HELLO_MSG + commandExecutionFaultSpec.getEndpointName() + "*")
.attachments(attachments).build();

return chatPostMessageRequest;
}

private void updateAttachment(Attachment attachment) {
attachment.setTitle("Fault Summary");
attachment.setFooter("Thanks,\n Mangle");
attachment.setColor(Color.WARNING);
attachment.setColor(Color.WARNING.hexValue);
}

private void addField(List<Field> fields, String title, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Date;
import java.util.HashSet;

import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import lombok.Getter;

import com.vmware.mangle.cassandra.model.faults.specs.CommandExecutionFaultSpec;
Expand Down Expand Up @@ -77,4 +78,12 @@ public Task<K8SFaultTriggerSpec> getTaskForK8SFaultTriggerSpec(TaskType taskType
task.getTaskData().getFaultSpec().setNotifierNames(new HashSet<>(Arrays.asList("mangle")));
return task;
}

public ChatPostMessageResponse getSlackChatPostMessageResponse(){
ChatPostMessageResponse chatPostMessageResponse = new ChatPostMessageResponse();
chatPostMessageResponse.setOk(true);
chatPostMessageResponse.setChannel("dev");
chatPostMessageResponse.setTs("1645427127.335539");
return chatPostMessageResponse;
}
}
Loading