FeishuNotifier.groovy 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.example
  2. import groovy.json.JsonOutput
  3. class FeishuNotifier implements Serializable {
  4. def script
  5. String webhookUrl
  6. FeishuNotifier(script, String webhookUrl) {
  7. this.script = script
  8. this.webhookUrl = webhookUrl
  9. }
  10. void send(String status, String jobName, String buildNumber, String buildUrl,
  11. String duration, String branch, String commitMsg, String author,
  12. String commitTime) {
  13. // 1. 决定左侧条颜色 + 标题
  14. def color = (status == 'START') ? 'blue' :
  15. (status == 'SUCCESS') ? 'green' :
  16. (status == 'FAILURE') ? 'red' : 'yellow'
  17. def title = (status == 'START') ? '🔵 构建开始' :
  18. (status == 'SUCCESS') ? '✅ 构建成功' :
  19. (status == 'FAILURE') ? '❌ 构建失败' : '🟡 构建不稳定'
  20. // 2. 飞书卡片:header + elements
  21. def payload = [
  22. msg_type: 'interactive',
  23. card: [
  24. header: [
  25. title: [
  26. tag: 'plain_text',
  27. content: title
  28. ],
  29. template: color
  30. ],
  31. elements: [
  32. // 单独一行:任务名称(加粗 + 火箭 emoji)
  33. [
  34. tag: 'div',
  35. text: [
  36. tag: 'plain_text',
  37. content: "🚀 任务名称:${jobName}",
  38. bold: true
  39. ]
  40. ],
  41. // 其余信息
  42. [
  43. tag: 'div',
  44. text: [
  45. tag: 'lark_md',
  46. content: """
  47. **构建编号**: [#${buildNumber}](${buildUrl})
  48. **持续时间**: ${duration}
  49. **分支**: ${branch}
  50. **提交信息**: ${commitMsg}
  51. **作者**: ${author}
  52. **提交时间**: ${commitTime}
  53. """.stripIndent()
  54. ]
  55. ],
  56. [
  57. tag: 'action',
  58. actions: [
  59. [
  60. tag: 'button',
  61. text: [
  62. tag: 'plain_text',
  63. content: '查看详情'
  64. ],
  65. type: 'primary',
  66. url: buildUrl
  67. ]
  68. ]
  69. ]
  70. ]
  71. ]
  72. ]
  73. def jsonBody = JsonOutput.toJson(payload)
  74. script.println ">>> Feishu JSON: ${jsonBody}"
  75. script.httpRequest(
  76. url : webhookUrl,
  77. httpMode : 'POST',
  78. requestBody: jsonBody,
  79. contentType: 'APPLICATION_JSON',
  80. consoleLogResponseBody: true
  81. )
  82. }
  83. }