| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package com.example
- import groovy.json.JsonOutput
- class FeishuNotifier implements Serializable {
- def script
- String webhookUrl
- FeishuNotifier(script, String webhookUrl) {
- this.script = script
- this.webhookUrl = webhookUrl
- }
- void send(String status, String jobName, String buildNumber, String buildUrl,
- String duration, String branch, String commitMsg, String author,
- String commitTime) {
- // 1. 决定左侧条颜色 + 标题
- def color = (status == 'START') ? 'blue' :
- (status == 'SUCCESS') ? 'green' :
- (status == 'FAILURE') ? 'red' : 'yellow'
- def title = (status == 'START') ? '🔵 构建开始' :
- (status == 'SUCCESS') ? '✅ 构建成功' :
- (status == 'FAILURE') ? '❌ 构建失败' : '🟡 构建不稳定'
- // 2. 飞书极简卡片:header + elements
- def payload = [
- msg_type: 'interactive',
- card: [
- header: [
- title: [
- tag: 'plain_text',
- content: title
- ],
- subtitle: [
- tag: 'lark_md',
- content: "**任务名称:${jobName}**" // ← 关键改动
- ],
- template: color // 左侧条颜色
- ],
- elements: [
- [
- tag: 'div',
- text: [
- tag: 'lark_md',
- content: """
- **构建编号**: [#${buildNumber}](${buildUrl})
- **持续时间**: ${duration}
- **分支**: ${branch}
- **提交信息**: ${commitMsg}
- **作者**: ${author}
- **提交时间**: ${commitTime}
- """.stripIndent()
- ]
- ],
- [
- tag: 'action',
- actions: [
- [
- tag: 'button',
- text: [
- tag: 'plain_text',
- content: '查看详情'
- ],
- type: 'primary',
- url: buildUrl
- ]
- ]
- ]
- ]
- ]
- ]
- def jsonBody = JsonOutput.toJson(payload)
- script.println ">>> Feishu JSON: ${jsonBody}"
- script.httpRequest(
- url : webhookUrl,
- httpMode : 'POST',
- requestBody: jsonBody,
- contentType: 'APPLICATION_JSON',
- consoleLogResponseBody: true
- )
- }
- }
|