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 ], template: color ], elements: [ [ tag: 'div', text: [ tag: 'lark_md', content: """ 🚀 任务名称:${jobName} **构建编号**: [#${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 ) } }