FeishuNotifier.groovy 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. subtitle: [
  30. tag: 'lark_md',
  31. content: "**任务名称:${jobName}**" // ← 关键改动
  32. ],
  33. template: color // 左侧条颜色
  34. ],
  35. elements: [
  36. [
  37. tag: 'div',
  38. text: [
  39. tag: 'lark_md',
  40. content: """
  41. **构建编号**: [#${buildNumber}](${buildUrl})
  42. **持续时间**: ${duration}
  43. **分支**: ${branch}
  44. **提交信息**: ${commitMsg}
  45. **作者**: ${author}
  46. **提交时间**: ${commitTime}
  47. """.stripIndent()
  48. ]
  49. ],
  50. [
  51. tag: 'action',
  52. actions: [
  53. [
  54. tag: 'button',
  55. text: [
  56. tag: 'plain_text',
  57. content: '查看详情'
  58. ],
  59. type: 'primary',
  60. url: buildUrl
  61. ]
  62. ]
  63. ]
  64. ]
  65. ]
  66. ]
  67. def jsonBody = JsonOutput.toJson(payload)
  68. script.println ">>> Feishu JSON: ${jsonBody}"
  69. script.httpRequest(
  70. url : webhookUrl,
  71. httpMode : 'POST',
  72. requestBody: jsonBody,
  73. contentType: 'APPLICATION_JSON',
  74. consoleLogResponseBody: true
  75. )
  76. }
  77. }