|
1 | 1 | name: Feishu Pull Request Notification |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request: |
| 4 | + pull_request_target: |
5 | 5 | types: [opened] |
6 | 6 |
|
7 | | -permissions: |
8 | | - contents: read |
| 7 | +permissions: {} |
9 | 8 |
|
10 | 9 | jobs: |
11 | 10 | notify: |
12 | 11 | name: Notify Feishu |
| 12 | + if: ${{ github.event.pull_request.head.repo.fork }} |
13 | 13 | runs-on: ubuntu-latest |
14 | 14 | steps: |
15 | | - - name: Create GitHub App token |
16 | | - id: app-token |
17 | | - uses: actions/create-github-app-token@v1 |
18 | | - with: |
19 | | - app-id: ${{ secrets.NEXU_PAL_APP_ID }} |
20 | | - private-key: ${{ secrets.NEXU_PAL_PRIVATE_KEY_PEM }} |
21 | | - |
22 | | - - uses: actions/checkout@v4 |
23 | | - with: |
24 | | - sparse-checkout: scripts |
25 | | - |
26 | 15 | - name: Send Feishu notification |
27 | 16 | env: |
28 | 17 | WEBHOOK_URL: ${{ secrets.NOTIFY_PR_FEISHU_WEBHOOK }} |
29 | | - EVENT_TYPE: pull_request |
30 | | - GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} |
31 | | - GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }} |
32 | 18 | TITLE: ${{ github.event.pull_request.title }} |
33 | 19 | URL: ${{ github.event.pull_request.html_url }} |
34 | 20 | NUMBER: ${{ github.event.pull_request.number }} |
35 | 21 | AUTHOR: ${{ github.event.pull_request.user.login }} |
36 | | - BODY: ${{ github.event.pull_request.body }} |
37 | 22 | LABELS_OR_CATEGORY: ${{ join(github.event.pull_request.labels.*.name, ', ') }} |
38 | 23 | REPO: ${{ github.repository }} |
39 | | - run: node scripts/notify/feishu-notify.mjs |
| 24 | + run: | |
| 25 | + node <<'EOF' |
| 26 | + const webhookUrl = process.env.WEBHOOK_URL; |
| 27 | + const title = process.env.TITLE ?? ""; |
| 28 | + const url = process.env.URL ?? ""; |
| 29 | + const number = process.env.NUMBER ?? ""; |
| 30 | + const author = process.env.AUTHOR ?? ""; |
| 31 | + const labelsOrCategory = process.env.LABELS_OR_CATEGORY || "none"; |
| 32 | + const repo = process.env.REPO ?? ""; |
| 33 | +
|
| 34 | + function truncate(value, maxLength) { |
| 35 | + return value.length > maxLength |
| 36 | + ? `${value.slice(0, maxLength)}...` |
| 37 | + : value; |
| 38 | + } |
| 39 | +
|
| 40 | + function sanitizeText(value) { |
| 41 | + return truncate( |
| 42 | + value |
| 43 | + .replace(/[\r\n\t]+/g, " ") |
| 44 | + .replace(/[\\`*_{}\[\]()#+\-.!|<>~]/g, "\\$&") |
| 45 | + .replace(/@/g, "@") |
| 46 | + .trim(), |
| 47 | + 200, |
| 48 | + ); |
| 49 | + } |
| 50 | +
|
| 51 | + if (!webhookUrl) { |
| 52 | + console.error("WEBHOOK_URL is required"); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | +
|
| 56 | + if (!author) { |
| 57 | + console.error("AUTHOR is required"); |
| 58 | + process.exit(1); |
| 59 | + } |
| 60 | +
|
| 61 | + if (author === "sentry[bot]") { |
| 62 | + console.log( |
| 63 | + `Skipping Feishu notification for internal-equivalent author: ${author}`, |
| 64 | + ); |
| 65 | + process.exit(0); |
| 66 | + } |
| 67 | +
|
| 68 | + const safeTitle = sanitizeText(title) || "(untitled PR)"; |
| 69 | + const safeAuthor = sanitizeText(author); |
| 70 | + const safeLabels = sanitizeText(labelsOrCategory) || "none"; |
| 71 | + const safeRepo = sanitizeText(repo) || "unknown repo"; |
| 72 | +
|
| 73 | + let safeUrl = url; |
| 74 | + try { |
| 75 | + const parsedUrl = new URL(url); |
| 76 | + if (parsedUrl.protocol !== "https:" || parsedUrl.hostname !== "github.com") { |
| 77 | + throw new Error("Only https://github.com URLs are allowed"); |
| 78 | + } |
| 79 | + safeUrl = parsedUrl.toString(); |
| 80 | + } catch (error) { |
| 81 | + console.error(`Invalid pull request URL: ${error}`); |
| 82 | + process.exit(1); |
| 83 | + } |
| 84 | +
|
| 85 | + const payload = { |
| 86 | + msg_type: "interactive", |
| 87 | + card: { |
| 88 | + schema: "2.0", |
| 89 | + header: { |
| 90 | + title: { |
| 91 | + tag: "plain_text", |
| 92 | + content: `[${safeRepo}] New Pull Request #${number}: ${safeTitle}`, |
| 93 | + }, |
| 94 | + template: "purple", |
| 95 | + }, |
| 96 | + body: { |
| 97 | + direction: "vertical", |
| 98 | + elements: [ |
| 99 | + { tag: "markdown", content: `**Author:** ${safeAuthor}` }, |
| 100 | + { tag: "markdown", content: `**Labels:** ${safeLabels}` }, |
| 101 | + { |
| 102 | + tag: "button", |
| 103 | + text: { tag: "plain_text", content: "View Pull Request" }, |
| 104 | + url: safeUrl, |
| 105 | + type: "primary", |
| 106 | + }, |
| 107 | + ], |
| 108 | + }, |
| 109 | + }, |
| 110 | + }; |
| 111 | +
|
| 112 | + const response = await fetch(webhookUrl, { |
| 113 | + method: "POST", |
| 114 | + headers: { "Content-Type": "application/json" }, |
| 115 | + body: JSON.stringify(payload), |
| 116 | + }); |
| 117 | +
|
| 118 | + if (!response.ok) { |
| 119 | + const text = await response.text(); |
| 120 | + console.error(`Webhook request failed (${response.status}): ${text}`); |
| 121 | + process.exit(1); |
| 122 | + } |
| 123 | + EOF |
0 commit comments