Skip to content

Commit

Permalink
lkbot-notifier v1.0 (#1)
Browse files Browse the repository at this point in the history
* implement bot notifier

Signed-off-by: Kevin Xiaohua Cai <[email protected]>
  • Loading branch information
kevincai committed Jul 19, 2022
1 parent f8b10d9 commit cf64550
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Jenkins Notifications Plugin for Lark Group Robot via OpenAPIs
This plugin allows sending jenkins job status notification to lark work group.

To use lark @Jenkins CI Assistant chat bot, please go to the plugin provided by bytedance at [github bytedance/jenkins-plugin-lark](https://github.com/bytedance/jenkins-plugin-lark).

This plugin is inspired by the `jenkins-plugin-lark` plugin, implements a message card style notification from a custom bot via its webhook address.

## How to Use

**1. Add a custom bot into chat group**

Refer to [lark custom bot documentation](https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN?fb=1), get the webhook address.

**2. Get plugin and install into Jenkins System**

**3. Configure the Jenkins plugin**
- Select the Job in Jenkins and configure the plugin and Webhook address.

![](./static/config_plugin.png?raw=true)

![](./static/job_plugin.png?raw=true)

- Bot sends the build result to the specified group.

![](./static/card_message.png?raw=true)

## Acknowledgement
This plugin is inspired by the **jenkins-plugin-lark** provided by bytedance at [github bytedance/jenkins-plugin-lark](https://github.com/bytedance/jenkins-plugin-lark).

## License
Apache 2.0 License
79 changes: 79 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.11</version>
<relativePath/>
</parent>
<groupId>com.starrocks</groupId>
<artifactId>lkbotv2-notifier</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>hpi</packaging>

<properties>
<jenkins.version>2.7.3</jenkins.version>
<java.level>8</java.level>
<jenkins-test-harness.version>2.13</jenkins-test-harness.version>
</properties>

<name>Botv2 Notification Plugin</name>
<description>Botv2 Notification Plugin</description>

<licenses>
<license>
<name>Apache License</name>
<url>https://opensource.org/licenses/Apache-2.0</url>
</license>
</licenses>

<developers>
<developer>
<id>kevincai</id>
<name>Kevin Cai</name>
<email>[email protected]</email>
</developer>
</developers>

<scm>
<connection>scm:git:ssh://github.com/starrocks/lkbot-notifier.git</connection>
<developerConnection>scm:git:ssh://[email protected]/starrocks/lbot-notifier.git</developerConnection>
<url>https://github.com/starrocks/lkbot-notifier</url>
</scm>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.7</version>
</dependency>
</dependencies>
</project>
86 changes: 86 additions & 0 deletions src/main/java/com/starrocks/botv2/BotNotifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.starrocks.botv2;

import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
import org.kohsuke.stapler.DataBoundConstructor;

public class BotNotifier extends Notifier {
private boolean onStart;
private boolean onSuccess;
private boolean onFailed;
private boolean onAbort;
private String sendUrlList;

public String getSendUrlList() {
return sendUrlList;
}

public boolean isOnStart() {
return onStart;
}

public boolean isOnSuccess() {
return onSuccess;
}

public boolean isOnFailed() {
return onFailed;
}

public boolean isOnAbort() {
return onAbort;
}

@DataBoundConstructor
public BotNotifier(
boolean onStart, boolean onSuccess, boolean onFailed, boolean onAbort, String sendUrlList) {
super();

this.onStart = onStart;
this.onSuccess = onSuccess;
this.onFailed = onFailed;
this.onAbort = onAbort;
this.sendUrlList = sendUrlList;
}

public BotService newBotService(AbstractBuild build, TaskListener listener) {
return new BotServiceImpl(
sendUrlList, onStart, onSuccess, onFailed, onAbort, listener, build);
}

@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) {
return true;
}

@Override
public BotNotifierDescriptor getDescriptor() {
return (BotNotifierDescriptor) super.getDescriptor();
}

@Extension
public static class BotNotifierDescriptor extends BuildStepDescriptor<Publisher> {
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

@Override
public String getDisplayName() {
return "Lark Bot(v2) Notification";
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/starrocks/botv2/BotService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.starrocks.botv2;

public interface BotService {
void start(String userDescription);
void success(String userDescription);
void failed();
void abort();
}
Loading

0 comments on commit cf64550

Please sign in to comment.