Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#4419] Add slack sink connector. #4562

Merged
merged 3 commits into from
Nov 20, 2023
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
96 changes: 49 additions & 47 deletions eventmesh-connectors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,52 @@ Add a new connector by implementing the source/sink interface using :
## Connector Status


| Connector Name | Type | Status |
|:------------------------------------------------:|:-----------------:|:--------:|
| [RocketMQ](eventmesh-connector-rocketmq) | Source | ✅ |
| [RocketMQ](eventmesh-connector-rocketmq) | Sink | ✅ |
| ChatGPT | Source | ⬜ |
| ChatGPT | Sink | ⬜ |
| ClickHouse | Source | ⬜ |
| ClickHouse | Sink | ⬜ |
| DingDing | Source | ⬜ |
| [DingDing](eventmesh-connector-dingding) | Sink | ✅ |
| Email | Source | ⬜ |
| Email | Sink | ⬜ |
| FeiShu | Source | ⬜ |
| FeiShu | Sink | ⬜ |
| [File](eventmesh-connector-file) | Source | ✅ |
| [File](eventmesh-connector-file) | Sink | ✅ |
| Github | Source | ⬜ |
| Github | Sink | ⬜ |
| Http | Source | ⬜ |
| Http | Sink | ⬜ |
| Jdbc | Source | ⬜ |
| [Jdbc](eventmesh-connector-jdbc) | Sink | ✅ |
| [Kafka](eventmesh-connector-kafka) | Source | ✅ |
| [Kafka](eventmesh-connector-kafka) | Sink | ✅ |
| [Knative](eventmesh-connector-knative) | Source | ✅ |
| [Knative](eventmesh-connector-knative) | Sink | ✅ |
| [MongoDB](eventmesh-connector-mongodb) | Source | ✅ |
| [MongoDB](eventmesh-connector-mongodb) | Sink | ✅ |
| [OpenFunction](eventmesh-connector-openfunction) | Source | ✅ |
| [OpenFunction](eventmesh-connector-openfunction) | Sink | ✅ |
| [Pravega](eventmesh-connector-pravega) | Source | ✅ |
| [Pravega](eventmesh-connector-pravega) | Sink | ✅ |
| [Promethues](eventmesh-connector-prometheus) | Source | ✅ |
| [Promethues](eventmesh-connector-prometheus) | Sink | ⬜ |
| [Pulsar](eventmesh-connector-pulsar) | Source | ✅ |
| [Pulsar](eventmesh-connector-pulsar) | Sink | ✅ |
| [Rabbitmq](eventmesh-connector-rabbitmq) | Source | ✅ |
| [Rabbitmq](eventmesh-connector-rabbitmq) | Sink | ✅ |
| [Redis](eventmesh-connector-redis) | Source | ✅ |
| [Redis](eventmesh-connector-redis) | Sink | ✅ |
| S3File | Source | ⬜ |
| [S3File](eventmesh-connector-s3) | Sink | ✅ |
| [Spring](eventmesh-connector-spring) | Source | ✅ |
| [Spring](eventmesh-connector-spring) | Sink | ✅ |
| WeCom | Source | ⬜ |
| [WeCom](eventmesh-connector-wecom) | Sink | ✅ |
| More connectors will be added... | Source/Sink | N/A |
| Connector Name | Type | Status |
|:------------------------------------------------:|:-----------:|:-------:|
| [RocketMQ](eventmesh-connector-rocketmq) | Source | ✅ |
| [RocketMQ](eventmesh-connector-rocketmq) | Sink | ✅ |
| ChatGPT | Source | ⬜ |
| ChatGPT | Sink | ⬜ |
| ClickHouse | Source | ⬜ |
| ClickHouse | Sink | ⬜ |
| DingDing | Source | ⬜ |
| [DingDing](eventmesh-connector-dingding) | Sink | ✅ |
| Email | Source | ⬜ |
| Email | Sink | ⬜ |
| FeiShu | Source | ⬜ |
| FeiShu | Sink | ⬜ |
| [File](eventmesh-connector-file) | Source | ✅ |
| [File](eventmesh-connector-file) | Sink | ✅ |
| Github | Source | ⬜ |
| Github | Sink | ⬜ |
| Http | Source | ⬜ |
| Http | Sink | ⬜ |
| Jdbc | Source | ⬜ |
| [Jdbc](eventmesh-connector-jdbc) | Sink | ✅ |
| [Kafka](eventmesh-connector-kafka) | Source | ✅ |
| [Kafka](eventmesh-connector-kafka) | Sink | ✅ |
| [Knative](eventmesh-connector-knative) | Source | ✅ |
| [Knative](eventmesh-connector-knative) | Sink | ✅ |
| [MongoDB](eventmesh-connector-mongodb) | Source | ✅ |
| [MongoDB](eventmesh-connector-mongodb) | Sink | ✅ |
| [OpenFunction](eventmesh-connector-openfunction) | Source | ✅ |
| [OpenFunction](eventmesh-connector-openfunction) | Sink | ✅ |
| [Pravega](eventmesh-connector-pravega) | Source | ✅ |
| [Pravega](eventmesh-connector-pravega) | Sink | ✅ |
| [Promethues](eventmesh-connector-prometheus) | Source | ✅ |
| [Promethues](eventmesh-connector-prometheus) | Sink | ⬜ |
| [Pulsar](eventmesh-connector-pulsar) | Source | ✅ |
| [Pulsar](eventmesh-connector-pulsar) | Sink | ✅ |
| [Rabbitmq](eventmesh-connector-rabbitmq) | Source | ✅ |
| [Rabbitmq](eventmesh-connector-rabbitmq) | Sink | ✅ |
| [Redis](eventmesh-connector-redis) | Source | ✅ |
| [Redis](eventmesh-connector-redis) | Sink | ✅ |
| S3File | Source | ⬜ |
| [S3File](eventmesh-connector-s3) | Sink | ✅ |
| [Slack](eventmesh-connector-slack) | Source | ⬜ |
| [Slack](eventmesh-connector-slack) | Sink | ✅ |
| [Spring](eventmesh-connector-spring) | Source | ✅ |
| [Spring](eventmesh-connector-spring) | Sink | ✅ |
| WeCom | Source | ⬜ |
| [WeCom](eventmesh-connector-wecom) | Sink | ✅ |
| More connectors will be added... | Source/Sink | N/A |
37 changes: 37 additions & 0 deletions eventmesh-connectors/eventmesh-connector-slack/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

configurations {
implementation.exclude group: 'ch.qos.logback', module: 'logback-classic'
implementation.exclude group: 'log4j', module: 'log4j'
testImplementation.exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
}

dependencies {
implementation project(":eventmesh-common")
implementation project(":eventmesh-sdks:eventmesh-sdk-java")
implementation project(":eventmesh-openconnect:eventmesh-openconnect-java")

implementation "com.slack.api:bolt:1.1.+"
implementation 'com.google.guava:guava'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

testImplementation "org.mockito:mockito-core"
testImplementation "org.mockito:mockito-junit-jupiter"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.connector.slack.config;

import org.apache.eventmesh.openconnect.api.config.Config;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data

Check warning on line 25 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/config/SlackConnectServerConfig.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/config/SlackConnectServerConfig.java#L25

Added line #L25 was not covered by tests
@EqualsAndHashCode(callSuper = true)
public class SlackConnectServerConfig extends Config {

private boolean sinkEnable;

Check warning on line 29 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/config/SlackConnectServerConfig.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/config/SlackConnectServerConfig.java#L29

Added line #L29 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.connector.slack.server;

import org.apache.eventmesh.common.Constants;
import org.apache.eventmesh.connector.slack.config.SlackConnectServerConfig;
import org.apache.eventmesh.connector.slack.sink.connector.SlackSinkConnector;
import org.apache.eventmesh.openconnect.Application;
import org.apache.eventmesh.openconnect.util.ConfigUtil;

public class SlackConnectServer {

Check warning on line 26 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/server/SlackConnectServer.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/server/SlackConnectServer.java#L26

Added line #L26 was not covered by tests

public static void main(String[] args) throws Exception {

SlackConnectServerConfig slackConnectServerConfig = ConfigUtil.parse(SlackConnectServerConfig.class,

Check warning on line 30 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/server/SlackConnectServer.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/server/SlackConnectServer.java#L30

Added line #L30 was not covered by tests
Constants.CONNECT_SERVER_CONFIG_FILE_NAME);

if (slackConnectServerConfig.isSinkEnable()) {
Application application = new Application();
application.run(SlackSinkConnector.class);

Check warning on line 35 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/server/SlackConnectServer.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/server/SlackConnectServer.java#L34-L35

Added lines #L34 - L35 were not covered by tests
}
}

Check warning on line 37 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/server/SlackConnectServer.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/server/SlackConnectServer.java#L37

Added line #L37 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.connector.slack.sink.config;

import lombok.Data;

@Data
public class SinkConnectorConfig {

private String connectorName;

Check warning on line 25 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/config/SinkConnectorConfig.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/config/SinkConnectorConfig.java#L25

Added line #L25 was not covered by tests

private String appToken;

Check warning on line 27 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/config/SinkConnectorConfig.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/config/SinkConnectorConfig.java#L27

Added line #L27 was not covered by tests

private String channelId;

Check warning on line 29 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/config/SinkConnectorConfig.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/config/SinkConnectorConfig.java#L29

Added line #L29 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.connector.slack.sink.config;

import org.apache.eventmesh.openconnect.api.config.SinkConfig;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class SlackSinkConfig extends SinkConfig {

private SinkConnectorConfig sinkConnectorConfig;

Check warning on line 29 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/config/SlackSinkConfig.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/config/SlackSinkConfig.java#L29

Added line #L29 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.connector.slack.sink.connector;

import org.apache.eventmesh.connector.slack.sink.config.SlackSinkConfig;
import org.apache.eventmesh.openconnect.api.config.Config;
import org.apache.eventmesh.openconnect.api.connector.ConnectorContext;
import org.apache.eventmesh.openconnect.api.connector.SinkConnectorContext;
import org.apache.eventmesh.openconnect.api.sink.Sink;
import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;

import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

import com.slack.api.Slack;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
* Slack sink connector.
* Slack doc: <a href="https://api.slack.com/messaging/sending">...</a>
*/
@Slf4j
public class SlackSinkConnector implements Sink {

private SlackSinkConfig sinkConfig;

private volatile boolean isRunning = false;

private MethodsClient client;

@Override
public Class<? extends Config> configClass() {
return SlackSinkConfig.class;
}

@Override
public void init(Config config) throws Exception {
// init config for dingding sink connector
this.sinkConfig = (SlackSinkConfig) config;
this.client = Slack.getInstance().methods();
}

@Override
public void init(ConnectorContext connectorContext) throws Exception {
// init config for dingding source connector
SinkConnectorContext sinkConnectorContext = (SinkConnectorContext) connectorContext;
this.sinkConfig = (SlackSinkConfig) sinkConnectorContext.getSinkConfig();
this.client = Slack.getInstance().methods();
}

Check warning on line 70 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java#L67-L70

Added lines #L67 - L70 were not covered by tests

@Override
public void start() {
isRunning = true;
}

@Override
public void commit(ConnectRecord record) {

}

Check warning on line 80 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java#L80

Added line #L80 was not covered by tests

@Override
public String name() {
return this.sinkConfig.getSinkConnectorConfig().getConnectorName();

Check warning on line 84 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java#L84

Added line #L84 was not covered by tests
}

@Override
public void stop() {
isRunning = false;
}

public boolean isRunning() {
return isRunning;

Check warning on line 93 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java#L93

Added line #L93 was not covered by tests
}

@SneakyThrows
@Override
public void put(List<ConnectRecord> sinkRecords) {
for (ConnectRecord record : sinkRecords) {
publishMessage(record);
}
}

private void publishMessage(ConnectRecord record) {
try {
ChatPostMessageResponse response = client.chatPostMessage(r -> r
.token(sinkConfig.getSinkConnectorConfig().getAppToken())
.channel(sinkConfig.getSinkConnectorConfig().getChannelId())
.text(new String((byte[]) record.getData())));

Check warning on line 109 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java#L107-L109

Added lines #L107 - L109 were not covered by tests
if (Objects.nonNull(response) && StringUtils.isNotBlank(response.getError())) {
throw new IllegalAccessException(response.getError());

Check warning on line 111 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java#L111

Added line #L111 was not covered by tests
}
} catch (Exception e) {
log.error("Send message to slack error.", e);

Check warning on line 114 in eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-connectors/eventmesh-connector-slack/src/main/java/org/apache/eventmesh/connector/slack/sink/connector/SlackSinkConnector.java#L113-L114

Added lines #L113 - L114 were not covered by tests
}
}
}
Loading
Loading