sync-issue-to-feishu.yml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. name: Sync GitHub issues to Feishu
  2. on:
  3. issues:
  4. types: [opened]
  5. permissions:
  6. contents: read
  7. issues: read
  8. jobs:
  9. notify-feishu:
  10. name: Notify Feishu
  11. runs-on: ubuntu-latest
  12. timeout-minutes: 5
  13. # Preserve the public support integration without forwarding private review issues.
  14. if: ${{ github.repository == 'MiniMax-AI/minimax-code' && github.event.repository.private == false && !github.event.issue.pull_request }}
  15. env:
  16. FEISHU_BOT_WEBHOOK: ${{ secrets.FEISHU_BOT_WEBHOOK }}
  17. FEISHU_BOT_SECRET: ${{ secrets.FEISHU_BOT_SECRET }}
  18. steps:
  19. - name: Check Feishu webhook
  20. if: ${{ env.FEISHU_BOT_WEBHOOK == '' }}
  21. run: |
  22. echo "::error::Missing repository secret FEISHU_BOT_WEBHOOK"
  23. exit 1
  24. - name: Build Feishu payload
  25. shell: bash
  26. run: |
  27. node <<'EOF'
  28. const crypto = require('crypto');
  29. const fs = require('fs');
  30. const event = JSON.parse(
  31. fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'),
  32. );
  33. const issue = event.issue;
  34. const repository = event.repository;
  35. const repoName = repository.full_name;
  36. const normalizeText = (value) =>
  37. String(value || '')
  38. .replace(/\r\n/g, '\n')
  39. .replace(/\n{3,}/g, '\n\n')
  40. .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, '')
  41. .trim();
  42. const truncate = (value, maxLength) =>
  43. value.length > maxLength
  44. ? `${value.slice(0, maxLength - 1)}...`
  45. : value;
  46. const escapeLarkMarkdown = (value) =>
  47. normalizeText(value)
  48. .replace(/\\/g, '\\\\')
  49. .replace(/`/g, '\\`')
  50. .replace(/\*/g, '\\*')
  51. .replace(/_/g, '\\_')
  52. .replace(/\[/g, '\\[')
  53. .replace(/\]/g, '\\]')
  54. .replace(/\(/g, '\\(')
  55. .replace(/\)/g, '\\)')
  56. .replace(/~/g, '\\~')
  57. .replace(/</g, '‹')
  58. .replace(/>/g, '›');
  59. const labelNames = issue.labels.map((label) => label.name);
  60. const getIssueKind = (labels) => {
  61. if (labels.includes('bug')) {
  62. return 'bug';
  63. }
  64. if (labels.includes('enhancement')) {
  65. return 'enhancement';
  66. }
  67. if (labels.includes('question')) {
  68. return 'question';
  69. }
  70. return 'issue';
  71. };
  72. const issueKind = getIssueKind(labelNames);
  73. const primaryLabel = labelNames[0] || issueKind;
  74. const headerTemplateByKind = {
  75. bug: 'red',
  76. enhancement: 'green',
  77. question: 'blue',
  78. issue: 'blue',
  79. };
  80. const getLabelColor = (label) => ({
  81. bug: 'red',
  82. enhancement: 'green',
  83. question: 'blue',
  84. })[label] || 'neutral';
  85. const headerTemplate = headerTemplateByKind[issueKind];
  86. const labelTags =
  87. labelNames
  88. .map((label) => {
  89. const color = getLabelColor(label);
  90. return `<text_tag color='${color}'>${escapeLarkMarkdown(label)}</text_tag>`;
  91. })
  92. .join(' ') || "<font color='grey'>none</font>";
  93. const body = issue.body || '';
  94. const bodyPreview = truncate(escapeLarkMarkdown(body) || '(empty)', 1000);
  95. const title = truncate(normalizeText(issue.title).replace(/\n/g, ' '), 100);
  96. const createdAt = new Intl.DateTimeFormat('zh-CN', {
  97. year: 'numeric',
  98. month: '2-digit',
  99. day: '2-digit',
  100. hour: '2-digit',
  101. minute: '2-digit',
  102. hour12: false,
  103. timeZone: 'Asia/Shanghai',
  104. })
  105. .format(new Date(issue.created_at))
  106. .replace(/\//g, '-');
  107. const payload = {
  108. msg_type: 'interactive',
  109. card: {
  110. config: {
  111. enable_forward: true,
  112. update_multi: true,
  113. },
  114. card_link: {
  115. url: issue.html_url,
  116. },
  117. header: {
  118. template: headerTemplate,
  119. title: {
  120. tag: 'plain_text',
  121. content: `GitHub Issue #${issue.number}: ${title}`,
  122. },
  123. },
  124. elements: [
  125. {
  126. tag: 'markdown',
  127. content: [
  128. `**Repository:** [${repoName}](${repository.html_url})`,
  129. `**Type:** ${escapeLarkMarkdown(primaryLabel)}`,
  130. `**Author:** ${escapeLarkMarkdown(issue.user.login)}`,
  131. `**Created:** ${createdAt} UTC+8`,
  132. `**Labels:** ${labelTags}`,
  133. ].join('\n'),
  134. },
  135. {
  136. tag: 'hr',
  137. },
  138. {
  139. tag: 'markdown',
  140. content: `**Summary**\n${bodyPreview}`,
  141. },
  142. {
  143. tag: 'action',
  144. actions: [
  145. {
  146. tag: 'button',
  147. text: {
  148. tag: 'plain_text',
  149. content: 'Open issue',
  150. },
  151. type: 'primary',
  152. url: issue.html_url,
  153. },
  154. ],
  155. },
  156. ],
  157. },
  158. };
  159. const secret = process.env.FEISHU_BOT_SECRET;
  160. if (secret) {
  161. const timestamp = Math.floor(Date.now() / 1000).toString();
  162. const stringToSign = `${timestamp}\n${secret}`;
  163. payload.timestamp = timestamp;
  164. payload.sign = crypto
  165. .createHmac('sha256', stringToSign)
  166. .update('')
  167. .digest('base64');
  168. }
  169. fs.writeFileSync('feishu-payload.json', JSON.stringify(payload));
  170. EOF
  171. - name: Send Feishu notification
  172. shell: bash
  173. run: |
  174. response="$(
  175. curl --fail-with-body --show-error --silent \
  176. --request POST \
  177. --header 'Content-Type: application/json' \
  178. --data-binary @feishu-payload.json \
  179. "$FEISHU_BOT_WEBHOOK"
  180. )"
  181. RESPONSE="$response" node <<'EOF'
  182. const response = JSON.parse(process.env.RESPONSE);
  183. const success =
  184. response.code === 0 ||
  185. response.StatusCode === 0 ||
  186. response.StatusMessage === 'success';
  187. if (!success) {
  188. console.error(JSON.stringify(response));
  189. process.exit(1);
  190. }
  191. console.log('Feishu notification sent.');
  192. EOF