|
| 1 | +# Example: drop into a *public* m5stack repo as |
| 2 | +# .github/workflows/wecom-digest-forward.yml |
| 3 | +# Forwards issue/PR/comment/assign events to the private ops repo via |
| 4 | +# repository_dispatch. Requires a secret on the *public* repo: |
| 5 | +# WECOM_DIGEST_DISPATCH_PAT — PAT (or fine-grained token) with |
| 6 | +# permission to create repository_dispatch on m5stack/wecom-github-digest. |
| 7 | +# |
| 8 | +# Never put webhook URLs or phones in this file. |
| 9 | +name: Forward events to WeCom digest |
| 10 | + |
| 11 | +on: |
| 12 | + issues: |
| 13 | + types: [opened, assigned] |
| 14 | + pull_request_target: |
| 15 | + # pull_request_target so fork PRs still forward metadata; we only read github.event |
| 16 | + types: [opened, assigned] |
| 17 | + issue_comment: |
| 18 | + types: [created] |
| 19 | + pull_request_review_comment: |
| 20 | + types: [created] |
| 21 | + |
| 22 | +permissions: |
| 23 | + contents: read |
| 24 | + |
| 25 | +jobs: |
| 26 | + forward: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - name: Require dispatch PAT |
| 30 | + id: check |
| 31 | + env: |
| 32 | + DISPATCH_PAT: ${{ secrets.WECOM_DIGEST_DISPATCH_PAT }} |
| 33 | + run: | |
| 34 | + if [ -z "$DISPATCH_PAT" ]; then |
| 35 | + echo "skip=true" >> "$GITHUB_OUTPUT" |
| 36 | + echo "WECOM_DIGEST_DISPATCH_PAT not set; skipping forward" |
| 37 | + else |
| 38 | + echo "skip=false" >> "$GITHUB_OUTPUT" |
| 39 | + fi |
| 40 | +
|
| 41 | + - name: Build client_payload + dispatch |
| 42 | + if: steps.check.outputs.skip != 'true' |
| 43 | + uses: actions/github-script@v7 |
| 44 | + env: |
| 45 | + OPS_OWNER: m5stack |
| 46 | + OPS_REPO: wecom-github-digest |
| 47 | + with: |
| 48 | + github-token: ${{ secrets.WECOM_DIGEST_DISPATCH_PAT }} |
| 49 | + script: | |
| 50 | + const ev = context.payload; |
| 51 | + const name = context.eventName; |
| 52 | + const action = context.payload.action; |
| 53 | + let event_type = null; |
| 54 | + let number, title, body, html_url, kind, assignees, assignee, comment_body, comment_url, comment_id; |
| 55 | +
|
| 56 | + const asLogins = (arr) => (arr || []).map(a => a && a.login).filter(Boolean); |
| 57 | + const sender = (ev.sender && ev.sender.login) || ''; |
| 58 | + const sender_type = (ev.sender && ev.sender.type) || 'User'; |
| 59 | + const repo = context.repo.owner + '/' + context.repo.repo; |
| 60 | +
|
| 61 | + if (name === 'issues' || name === 'pull_request_target' || name === 'pull_request') { |
| 62 | + const item = ev.issue || ev.pull_request; |
| 63 | + number = item.number; |
| 64 | + title = item.title || ''; |
| 65 | + body = (item.body || '').slice(0, 2000); |
| 66 | + html_url = item.html_url || ''; |
| 67 | + kind = (ev.pull_request || String(name).startsWith('pull_request')) ? 'pull_request' : 'issue'; |
| 68 | + assignees = asLogins(item.assignees); |
| 69 | + if (action === 'opened') { |
| 70 | + event_type = 'opened'; |
| 71 | + } else if (action === 'assigned') { |
| 72 | + event_type = 'assigned'; |
| 73 | + assignee = (ev.assignee && ev.assignee.login) || ''; |
| 74 | + } |
| 75 | + } else if (name === 'issue_comment' || name === 'pull_request_review_comment') { |
| 76 | + event_type = 'comment'; |
| 77 | + const item = ev.issue || ev.pull_request; |
| 78 | + const comment = ev.comment || {}; |
| 79 | + number = item ? item.number : 0; |
| 80 | + title = item ? (item.title || '') : ''; |
| 81 | + html_url = comment.html_url || (item && item.html_url) || ''; |
| 82 | + kind = (item && item.pull_request) || name === 'pull_request_review_comment' ? 'pull_request' : 'issue'; |
| 83 | + assignees = asLogins(item && item.assignees); |
| 84 | + comment_body = (comment.body || '').slice(0, 2000); |
| 85 | + comment_url = comment.html_url || ''; |
| 86 | + comment_id = comment.id || ''; |
| 87 | + body = comment_body; |
| 88 | + } |
| 89 | +
|
| 90 | + if (!event_type) { |
| 91 | + core.info(`No forward mapping for ${name}.${action}`); |
| 92 | + return; |
| 93 | + } |
| 94 | +
|
| 95 | + const client_payload = { |
| 96 | + event_type, |
| 97 | + repo, |
| 98 | + number, |
| 99 | + title, |
| 100 | + body, |
| 101 | + html_url, |
| 102 | + kind, |
| 103 | + sender, |
| 104 | + sender_type, |
| 105 | + assignees, |
| 106 | + assignee: assignee || '', |
| 107 | + comment_body: comment_body || '', |
| 108 | + comment_url: comment_url || '', |
| 109 | + comment_id: comment_id || '', |
| 110 | + }; |
| 111 | +
|
| 112 | + await github.rest.repos.createDispatchEvent({ |
| 113 | + owner: process.env.OPS_OWNER, |
| 114 | + repo: process.env.OPS_REPO, |
| 115 | + event_type: 'wecom-digest', |
| 116 | + client_payload, |
| 117 | + }); |
| 118 | + core.info(`Dispatched wecom-digest ${event_type} for ${repo}#${number}`); |
0 commit comments