diff --git a/.gitignore b/.gitignore index 175982b..2794263 100644 --- a/.gitignore +++ b/.gitignore @@ -1,31 +1,8 @@ -### gradle ### -.gradle -/build/ -!gradle/wrapper/gradle-wrapper.jar - -### STS ### -.settings/ -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -bin/ - ### IntelliJ IDEA ### .idea *.iws *.iml *.ipr -rebel.xml - -### NetBeans ### -nbproject/private/ -build/ -nbbuild/ -nbdist/ -.nb-gradle/ ### maven ### target/ @@ -35,9 +12,6 @@ target/ *.tar *.tar.gz -### vscode ### -.vscode - ### logs ### /logs/ *.log @@ -57,5 +31,5 @@ Thumbs.db Servers .metadata -### jenkins ### +### Jenkins ### work/ \ No newline at end of file diff --git a/README.md b/README.md index 8f23ba9..7c0c121 100644 --- a/README.md +++ b/README.md @@ -1,191 +1,14 @@ ## 🌐 插件简介 -`feishu-notification-plugin` 是一个用于 `Jenkins` -的[飞书机器人](https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN)通知插件,可以将 `Jenkins` -构建过程以及结果通知推送到 `飞书` 协作平台。 +`lark-notice-plugin` 是一个用于 `Jenkins` 的 `构建通知机器人` 通知插件,可以将 `Jenkins` +构建过程以及结果通知推送到 `Lark`、`飞书`、`钉钉` 协作平台。 可配置多个的通知时机,包括 `构建启动时`、`构建中断`、`构建失败`、`构建成功时`、`构建不稳定`等。 支持多种不同类型的消息,包括 `文本消息`、`图片消息`, `群名片消息`、`富文本消息`、`卡片消息`; 同时该插件还提供了`自定义模板`和`变量`的功能,使您能够根据自己的需求来定制通知消息的内容和格式。 ## 📝 使用文档 -> 请参考 [官方文档](https://721806280.github.io/feishu-notification-plugin-doc) - -#### TEXT 消息 - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo '发送文本消息...' - } - post { - success { - feiShuTalk ( - robot: 'f72aa1bb-0f0b-47c7-8387-272d266dc25c', - type: 'TEXT', - text: [ - "新更新提醒", - '所有人' - ] - ) - } - } - } - } -} -``` - -#### 群名片消息 - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo '发送群名片消息...' - } - post { - success { - feiShuTalk ( - robot: 'f72aa1bb-0f0b-47c7-8387-272d266dc25c', - type: 'SHARE_CHAT', - shareChatId: 'oc_f5b1a7eb27ae2c7b6adc2a74faf339ff' - ) - } - } - } - } -} -``` - -#### 图片消息 - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo '发送图片消息...' - } - post { - success { - feiShuTalk ( - robot: 'f72aa1bb-0f0b-47c7-8387-272d266dc25c', - type: 'IMAGE', - imageKey: 'img_ecffc3b9-8f14-400f-a014-05eca1a4310g' - ) - } - } - } - } -} -``` - -#### 富文本消息 - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo '发送富文本消息...' - } - post { - success { - feiShuTalk ( - robot: 'f72aa1bb-0f0b-47c7-8387-272d266dc25c', - type: 'POST', - title: '项目更新通知', - post: [ - [ - [ - "tag": "text", - "text": "项目有更新: " - ], - [ - "tag": "a", - "text": "请查看", - "href": "https://www.example.com/" - ], - [ - "tag": "at", - "user_id": "ou_xxxxxx", - "user_name": "tom" - ] - ], - [ - [ - "tag": "text", - "text": "项目有更新:" - ], - [ - "tag": "at", - "user_id": "all", - "user_name": "所有人" - ] - ] - ] - ) - } - } - } - } -} - -``` - -#### 卡片消息 - -> 1. 按钮颜色 `type` 的取值范围: primary | danger | default -> 2. 字体颜色 `color` 的取值范围: green:绿色文本 | red:红色文本 | grey:灰色文本 | default:白底黑字样式 - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo "发送卡片消息..." - } - post { - success { - feiShuTalk ( - robot: "f72aa1bb-0f0b-47c7-8387-272d266dc25c", - type: "INTERACTIVE", - title: "📢 Jenkins 构建通知", - text: [ - "📋 **任务名称**:[${JOB_NAME}](${JOB_URL})", - "🔢 **任务编号**:[${BUILD_DISPLAY_NAME}](${BUILD_URL})", - "🌟 **构建状态**: 成功", - "🕐 **构建用时**: ${currentBuild.duration} ms", - "👤 **执 行 者**: Started by user anonymous", - "" - ], - buttons: [ - [ - title: "更改记录", - url: "${BUILD_URL}changes" - ], - [ - title: "控制台", - type: "danger", - url: "${BUILD_URL}console" - ] - ] - ) - } - } - } - } -} - -``` +> 请参考 [官方文档](https://721806280.github.io/lark-notice-plugin-doc) ## 🧑‍💻 开发服务 @@ -218,8 +41,10 @@ pipeline { 校验代码规范。 2. 使用 [Google Style Guide](https://github.com/google/styleguide) 统一代码风格。 -> `IDEA`下载 [intellij-java-google-style.xml](https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml) -> 通过 `Settings` -> `Editor` `Code Style` 进行导入。 +> `IDEA` +> +下载 [intellij-java-google-style.xml](https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml) +> 通过 `Settings` -> `Editor` `Code Style` 进行导入。 ## 🔍️ 相关链接 @@ -227,12 +52,8 @@ pipeline { - [Jenkins 插件开发之旅:两天内从 idea 到发布(上篇)](https://jenkins-zh.cn/wechat/articles/2019/05/2019-05-06-jenkins-plugin-develop-within-two-days-part01/) - [Jenkins 插件开发之旅:两天内从 idea 到发布(下篇)](https://jenkins-zh.github.io/wechat/articles/2019/05/2019-05-08-jenkins-plugin-develop-within-two-days-part02/) -## 🍻 开源推荐 - -- `钉钉插件`:[https://github.com/jenkinsci/dingtalk-plugin](https://github.com/jenkinsci/dingtalk-plugin) - ## 💚 鸣谢 感谢 JetBrains 提供的免费开源 License -[![JetBrains](docs/img/jetbrains.png)](https://www.jetbrains.com/?from=feishu-notification-plugin) \ No newline at end of file +[![JetBrains](docs/img/jetbrains.png)](https://www.jetbrains.com/?from=lark-notice-plugin) \ No newline at end of file diff --git a/README_EN.md b/README_EN.md index 761ebdf..e308d7d 100644 --- a/README_EN.md +++ b/README_EN.md @@ -1,195 +1,18 @@ ## 🌐 Plugin Introduction -`feishu-notification-plugin` is -a [FeiShu robot notification](https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN) plugin for `Jenkins`, -which can push the `Jenkins` build process and result notifications to the `Feishu` collaboration platform. +`lark-notice-plugin` is a `build notification robot` notification plug-in for `Jenkins`, which can push the `Jenkins` +build process and result notifications to the `Lark`、`Feishu` and `DingTalk` collaboration platforms. Multiple notification timings can be configured, -including `build startup`, `build interruption`, `build failure`, `build success`, `build unstable`, etc. -Support many different types of messages, -including `text message`, `picture message`, `group business card message`, `rich text message`, `card message`; -At the same time, the plug-in also provides the functions of `custom template` and `variable`, enabling you to customize -the content and format of notification messages according to your needs. +including `when the build starts`, `build interruption`, `build failure`, `when the build succeeds`, `build instability`, +etc. +Supports many different types of messages, +including `text messages`, `picture messages`, `group business card messages`, `rich text messages`, `card messages`; +At the same time, the plug-in also provides the functions of `custom template` and `variables', allowing you to +customize the content and format of notification messages according to your own needs. ## 📝 Using documentation -> Please refer to [official document](https://721806280.github.io/feishu-notification-plugin-doc) - -#### TEXT message - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo 'Send text message...' - } - post { - success { - feiShuTalk ( - robot: 'f72aa1bb-0f0b-47c7-8387-272d266dc25c', - type: 'TEXT', - text: [ - "New Update Reminder", - 'everyone' - ] - ) - } - } - } - } -} -``` - -#### Group business card message - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo 'Send group business card message...' - } - post { - success { - feiShuTalk ( - robot: 'f72aa1bb-0f0b-47c7-8387-272d266dc25c', - type: 'SHARE_CHAT', - shareChatId: 'oc_f5b1a7eb27ae2c7b6adc2a74faf339ff' - ) - } - } - } - } -} -``` - -#### Picture Message - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo 'Send picture message...' - } - post { - success { - feiShuTalk ( - robot: 'f72aa1bb-0f0b-47c7-8387-272d266dc25c', - type: 'IMAGE', - imageKey: 'img_ecffc3b9-8f14-400f-a014-05eca1a4310g' - ) - } - } - } - } -} -``` - -#### Rich text messages - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo 'Send rich text message...' - } - post { - success { - feiShuTalk ( - robot: 'f72aa1bb-0f0b-47c7-8387-272d266dc25c', - type: 'POST', - title: 'Project update notification', - post: [ - [ - [ - "tag": "text", - "text": "Item has been updated: " - ], - [ - "tag": "a", - "text": "Please check", - "href": "https://www.example.com/" - ], - [ - "tag": "at", - "user_id": "ou_xxxxxx", - "user_name": "tom" - ] - ], - [ - [ - "tag": "text", - "text": "Item has been updated:" - ], - [ - "tag": "at", - "user_id": "all", - "user_name": "Everyone" - ] - ] - ] - ) - } - } - } - } -} - -``` - -#### Card message - -> 1. The value range of the button color `type`: primary | danger | default -> 2. Value range of font color `color`: green: green text | red: red text | grey: gray text | default: black text on - white background - -``` -pipeline { - agent any - stages { - stage('text'){ - steps { - echo "Send card message..." - } - post { - success { - feiShuTalk ( - robot: "f72aa1bb-0f0b-47c7-8387-272d266dc25c", - type: "INTERACTIVE", - title: "📢 Jenkins Build Notifications", - text: [ - "📋 **JOB NAME**: [${JOB_NAME}](${JOB_URL})", - "🔢 **Task ID**: [${BUILD_DISPLAY_NAME}](${BUILD_URL})", - "🌟 **Build Status**: Success", - "🕐 **Build Duration**: ${currentBuild.duration} ms", - "👤 **Performer**: Started by user anonymous", - "" - ], - buttons: [ - [ - title: "Change Record", - url: "${BUILD_URL}changes" - ], - [ - title: "Console", - type: "danger", - url: "${BUILD_URL}console" - ] - ] - ) - } - } - } - } -} - -``` +> Please refer to [official document](https://721806280.github.io/lark-notice-plugin-doc) ## 🧑‍💻 Development Services @@ -223,7 +46,8 @@ Add `hpi:run` to the startup configuration in the `maven` control panel on the r 2. Use the [Google Style Guide](https://github.com/google/styleguide) to unify the code style. > `IDEA` -> download [intellij-java-google-style.xml](https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml) +> +download [intellij-java-google-style.xml](https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml) > Import via `Settings` -> `Editor` `Code Style`. ## 🔍️ Related Links @@ -234,12 +58,8 @@ Add `hpi:run` to the startup configuration in the `maven` control panel on the r - [Jenkins plug-in development journey: from idea to release in two days (Part 2)](https://jenkins-zh.github.io/wechat/articles/2019/05/2019-05-08-jenkins-plugin- develop-within-two-days-part02/) -## 🍻 Open source recommendation - -- `Dingtalk Plugin`: [https://github.com/jenkinsci/dingtalk-plugin](https://github.com/jenkinsci/dingtalk-plugin) - ## 💚 Thanks Thanks to JetBrains for the free open source license -[![JetBrains](docs/img/jetbrains.png)](https://www.jetbrains.com/?from=feishu-notification-plugin) \ No newline at end of file +[![JetBrains](docs/img/jetbrains.png)](https://www.jetbrains.com/?from=lark-notice-plugin) \ No newline at end of file diff --git a/pom.xml b/pom.xml index cef1646..df6468e 100644 --- a/pom.xml +++ b/pom.xml @@ -10,11 +10,11 @@ io.jenkins.plugins - feishu-notification + lark-notice ${revision}-${changelist} hpi - 1.2.3 + 2.0.0 M1 2.414.3 @@ -22,8 +22,8 @@ ${project.artifactId} - 飞书 Jenkins 通知插件 - https://github.com/jenkinsci/feishu-notification-plugin + Lark通知插件 + https://github.com/jenkinsci/lark-notice-plugin @@ -41,8 +41,8 @@ - scm:git:git@github.com:jenkinsci/feishu-notification-plugin.git - scm:git:git@github.com:jenkinsci/feishu-notification-plugin.git + scm:git:git@github.com:jenkinsci/lark-notice-plugin.git + scm:git:git@github.com:jenkinsci/lark-notice-plugin.git ${project.url} ${project.name}-${project.version} diff --git a/src/main/java/io/jenkins/plugins/feishu/notification/FeiShuTalkStep.java b/src/main/java/io/jenkins/plugins/feishu/notification/FeiShuTalkStep.java deleted file mode 100644 index 07c847a..0000000 --- a/src/main/java/io/jenkins/plugins/feishu/notification/FeiShuTalkStep.java +++ /dev/null @@ -1,383 +0,0 @@ -package io.jenkins.plugins.feishu.notification; - -import edu.umd.cs.findbugs.annotations.NonNull; -import hudson.EnvVars; -import hudson.Extension; -import hudson.model.Run; -import hudson.model.TaskListener; -import io.jenkins.plugins.feishu.notification.config.FeiShuTalkGlobalConfig; -import io.jenkins.plugins.feishu.notification.config.FeiShuTalkRobotConfig; -import io.jenkins.plugins.feishu.notification.enums.MsgTypeEnum; -import io.jenkins.plugins.feishu.notification.enums.NoticeOccasionEnum; -import io.jenkins.plugins.feishu.notification.model.ButtonModel; -import io.jenkins.plugins.feishu.notification.model.ImgModel; -import io.jenkins.plugins.feishu.notification.model.MessageModel; -import io.jenkins.plugins.feishu.notification.sdk.impl.FeiShuTalkServiceImpl; -import io.jenkins.plugins.feishu.notification.sdk.model.SendResult; -import io.jenkins.plugins.feishu.notification.sdk.model.entity.support.Alt; -import io.jenkins.plugins.feishu.notification.sdk.model.entity.support.Button; -import io.jenkins.plugins.feishu.notification.sdk.model.entity.support.ImgElement; -import io.jenkins.plugins.feishu.notification.tools.JsonUtils; -import io.jenkins.plugins.feishu.notification.tools.Logger; -import io.jenkins.plugins.feishu.notification.tools.Utils; -import jenkins.model.Jenkins; -import lombok.Getter; -import lombok.Setter; -import org.jenkinsci.plugins.workflow.steps.Step; -import org.jenkinsci.plugins.workflow.steps.StepContext; -import org.jenkinsci.plugins.workflow.steps.StepDescriptor; -import org.jenkinsci.plugins.workflow.steps.StepExecution; -import org.kohsuke.stapler.DataBoundConstructor; -import org.kohsuke.stapler.DataBoundSetter; -import org.springframework.util.CollectionUtils; - -import java.util.*; -import java.util.stream.Collectors; - -/** - * FeiShuTalkStep 是 Jenkins 插件中用于发送飞书消息的 Step 实现类。 - * 支持发送文本消息、图片消息、富文本消息、分享聊天消息等类型,同时支持添加按钮交互操作。 - * 插件中运用了 FeiShuTalk GlobalConfig 进行机器人配置,支持多个机器人同时使用。 - * - * @author xm.z - */ -@Getter -@Setter -@SuppressWarnings("unused") -public class FeiShuTalkStep extends Step { - - /** - * 用于发送飞书消息的机器人名称。 - */ - private String robot; - - /** - * 消息类型,目前支持 TEXT、IMAGE、POST、INTERACTIVE 和 SHARE_CHAT。 - */ - private MsgTypeEnum type; - - /** - * 消息标题,在不同类型的消息中具有不同的作用。 - */ - private String title; - - /** - * 文本类型消息中的内容,是一个字符串列表。 - */ - private List text; - - /** - * 分享聊天类型消息中需要分享的聊天 ID。 - */ - private String shareChatId; - - /** - * 图片类型消息中需要展示的图片 key。 - */ - private String imageKey; - - /** - * 富文本类型消息中展示内容的数据结构。 - */ - private List>> post; - - /** - * 消息正文顶部的图片 -- 仅适用于卡片消息。 - */ - private ImgModel topImg; - - /** - * 消息正文底部的图片 -- 仅适用于卡片消息。 - */ - private ImgModel bottomImg; - - /** - * 消息中需要包含的按钮列表。 - */ - private List buttons; - - /** - * Jenkins 主页的路径,用于构造文本消息中的链接和按钮链接。 - */ - private String rootPath = Jenkins.get().getRootUrl(); - - /** - * 飞书机器人实现类的实例。 - */ - private FeiShuTalkServiceImpl service = new FeiShuTalkServiceImpl(); - - /** - * 插件的构造函数,指定机器人名称。 - * - * @param robot 用于发送飞书消息的机器人名称。 - */ - @DataBoundConstructor - public FeiShuTalkStep(String robot) { - this.robot = robot; - } - - /** - * 设置消息类型,如果为空,则使用默认值 TEXT。 - * - * @param type 消息类型。 - */ - @DataBoundSetter - public void setType(MsgTypeEnum type) { - this.type = type == null ? MsgTypeEnum.TEXT : type; - } - - /** - * 设置消息标题。 - * - * @param title 消息标题。 - */ - @DataBoundSetter - public void setTitle(String title) { - this.title = title; - } - - /** - * 设置文本消息中的内容数据列表。 - * - * @param text 文本消息中的内容数据列表。 - */ - @DataBoundSetter - public void setText(List text) { - this.text = text; - } - - /** - * 设置分享聊天消息中需要分享的聊天 ID。 - * - * @param shareChatId 分享聊天消息中需要分享的聊天 ID。 - */ - @DataBoundSetter - public void setShareChatId(String shareChatId) { - this.shareChatId = shareChatId; - } - - /** - * 设置图片类型消息中需要展示的图片 key。 - * - * @param imageKey 图片类型消息中需要展示的图片 key。 - */ - @DataBoundSetter - public void setImageKey(String imageKey) { - this.imageKey = imageKey; - } - - /** - * 设置富文本类型消息中展示内容的数据结构。 - * - * @param post 富文本类型消息中展示内容的数据结构。 - */ - @DataBoundSetter - public void setPost(List>> post) { - this.post = post; - } - - /** - * 设置消息中正文顶部的图片。 - * - * @param topImg 消息中正文顶部的图片。 - */ - @DataBoundSetter - public void setTopImg(ImgModel topImg) { - this.topImg = topImg; - } - - /** - * 设置消息中正文底部的图片。 - * - * @param bottomImg 消息中正文底部的图片。 - */ - @DataBoundSetter - public void setBottomImg(ImgModel bottomImg) { - this.bottomImg = bottomImg; - } - - /** - * 设置消息中需要包含的按钮列表。 - * - * @param buttons 消息中需要包含的按钮列表。 - */ - @DataBoundSetter - public void setButtons(List buttons) { - this.buttons = buttons; - } - - /** - * 发送飞书消息的方法。 - * - * @param run Jenkins 任务运行时的上下文信息。 - * @param envVars Jenkins 任务运行时的环境变量。 - * @param listener Jenkins 任务运行时的监听器。 - * @return 消息发送结果。 - */ - public SendResult send(Run run, EnvVars envVars, TaskListener listener) { - NoticeOccasionEnum noticeOccasion = NoticeOccasionEnum.getNoticeOccasion(run.getResult()); - - MessageModel message = MessageModel.builder().type(type) - .statusType(noticeOccasion.buildStatus()).title(envVars.expand(title)) - .text(envVars.expand(buildText())).buttons(buildButtons(run, envVars)) - .topImg(buildImg(envVars, topImg)).bottomImg(buildImg(envVars, bottomImg)) - .build(); - - Logger.log(listener, "当前机器人信息: %s", - FeiShuTalkGlobalConfig.getRobot(robot).map(FeiShuTalkRobotConfig::getName)); - Logger.log(listener, "发送的消息详情: %s", JsonUtils.toJson(message)); - - return service.send(envVars.expand(robot), message); - } - - /** - * 消息正文的图片节点 - * - * @param envVars Jenkins 任务运行时的环境变量。 - * @param imgModel 消息正文的图片模型 - * @return 消息正文的图片节点 - */ - private ImgElement buildImg(EnvVars envVars, ImgModel imgModel) { - if (Objects.isNull(imgModel)) { - return null; - } - ImgElement imgElement = new ImgElement(); - imgElement.setImgKey(imgModel.getImgKey()); - imgElement.setMode(imgModel.getMode()); - imgElement.setCompactWidth(imgModel.isCompactWidth()); - imgElement.setCustomWidth(imgModel.getCustomWidth()); - imgElement.setAlt(Alt.build(envVars.expand(imgModel.getAltContent()))); - return imgElement; - } - - /** - * 构造消息中要包含的按钮列表。 - * - * @param run Jenkins 任务运行时的上下文信息。 - * @param envVars Jenkins 任务运行时的环境变量。 - * @return 消息中要包含的按钮列表。 - */ - private List