FeishuNotifier.groovy 2.6 KB

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