sync-issue-to-feishu.yml 6.6 KB

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