sync-issue-to-feishu.yml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 labels = issue.labels.map((label) => label.name).join(', ') || 'none';
  36. const body = issue.body || '';
  37. const bodyPreview =
  38. body
  39. .replace(/\r\n/g, '\n')
  40. .replace(/\n{3,}/g, '\n\n')
  41. .slice(0, 1600) || '(empty)';
  42. const payload = {
  43. msg_type: 'post',
  44. content: {
  45. post: {
  46. zh_cn: {
  47. title: `GitHub Issue #${issue.number}: ${issue.title}`,
  48. content: [
  49. [
  50. {
  51. tag: 'text',
  52. text: `Repository: ${repoName}\n`,
  53. },
  54. ],
  55. [
  56. {
  57. tag: 'text',
  58. text: `Author: ${issue.user.login} | Labels: ${labels}\n`,
  59. },
  60. ],
  61. [
  62. {
  63. tag: 'text',
  64. text: `Summary:\n${bodyPreview}\n`,
  65. },
  66. ],
  67. [
  68. {
  69. tag: 'a',
  70. text: 'Open issue',
  71. href: issue.html_url,
  72. },
  73. ],
  74. ],
  75. },
  76. },
  77. },
  78. };
  79. const secret = process.env.FEISHU_BOT_SECRET;
  80. if (secret) {
  81. const timestamp = Math.floor(Date.now() / 1000).toString();
  82. const stringToSign = `${timestamp}\n${secret}`;
  83. payload.timestamp = timestamp;
  84. payload.sign = crypto
  85. .createHmac('sha256', stringToSign)
  86. .update('')
  87. .digest('base64');
  88. }
  89. fs.writeFileSync('feishu-payload.json', JSON.stringify(payload));
  90. EOF
  91. - name: Send Feishu notification
  92. shell: bash
  93. run: |
  94. response="$(
  95. curl --fail-with-body --show-error --silent \
  96. --request POST \
  97. --header 'Content-Type: application/json' \
  98. --data-binary @feishu-payload.json \
  99. "$FEISHU_BOT_WEBHOOK"
  100. )"
  101. RESPONSE="$response" node <<'EOF'
  102. const response = JSON.parse(process.env.RESPONSE);
  103. const success =
  104. response.code === 0 ||
  105. response.StatusCode === 0 ||
  106. response.StatusMessage === 'success';
  107. if (!success) {
  108. console.error(JSON.stringify(response));
  109. process.exit(1);
  110. }
  111. console.log('Feishu notification sent.');
  112. EOF