|
4 | 4 | types: [opened, closed] |
5 | 5 |
|
6 | 6 | jobs: |
7 | | - new-pr: |
8 | | - if: github.event.action == 'opened' && github.repository == 'embassy-rs/embassy' |
| 7 | + notify: |
| 8 | + if: github.repository == 'embassy-rs/embassy' |
9 | 9 | runs-on: ubuntu-latest |
10 | 10 | continue-on-error: true |
11 | 11 | steps: |
12 | | - - name: send message |
13 | | - uses: s3krit/matrix-message-action@v0.0.3 |
14 | | - with: |
15 | | - room_id: ${{ secrets.MATRIX_ROOM_ID }} |
16 | | - access_token: ${{ secrets.MATRIX_ACCESS_TOKEN }} |
17 | | - message: "New PR: [${{ github.event.pull_request.title }}](${{ github.event.pull_request.html_url }})" |
18 | | - server: "matrix.org" |
| 12 | + - name: send message |
| 13 | + uses: actions/github-script@v8 |
| 14 | + with: |
| 15 | + script: | |
| 16 | + const action = context.payload.action; |
| 17 | + const merged = context.payload.pull_request.merged; |
| 18 | + const title = context.payload.pull_request.title; |
| 19 | + const url = context.payload.pull_request.html_url; |
19 | 20 |
|
20 | | - merged-pr: |
21 | | - if: github.event.action == 'closed' && github.event.pull_request.merged == true && github.repository == 'embassy-rs/embassy' |
22 | | - runs-on: ubuntu-latest |
23 | | - continue-on-error: true |
24 | | - steps: |
25 | | - - name: send message |
26 | | - uses: s3krit/matrix-message-action@v0.0.3 |
27 | | - with: |
28 | | - room_id: ${{ secrets.MATRIX_ROOM_ID }} |
29 | | - access_token: ${{ secrets.MATRIX_ACCESS_TOKEN }} |
30 | | - message: "PR merged: [${{ github.event.pull_request.title }}](${{ github.event.pull_request.html_url }})" |
31 | | - server: "matrix.org" |
| 21 | + // Determine message prefix |
| 22 | + let prefix; |
| 23 | + if (action === 'opened') { |
| 24 | + prefix = 'New PR'; |
| 25 | + } else if (action === 'closed' && merged) { |
| 26 | + prefix = 'PR merged'; |
| 27 | + } else if (action === 'closed' && !merged) { |
| 28 | + prefix = 'PR closed without merging'; |
| 29 | + } else { |
| 30 | + return; // Unknown action, skip |
| 31 | + } |
32 | 32 |
|
33 | | - abandoned-pr: |
34 | | - if: github.event.action == 'closed' && github.event.pull_request.merged == false && github.repository == 'embassy-rs/embassy' |
35 | | - runs-on: ubuntu-latest |
36 | | - continue-on-error: true |
37 | | - steps: |
38 | | - - name: send message |
39 | | - uses: s3krit/matrix-message-action@v0.0.3 |
40 | | - with: |
41 | | - room_id: ${{ secrets.MATRIX_ROOM_ID }} |
42 | | - access_token: ${{ secrets.MATRIX_ACCESS_TOKEN }} |
43 | | - message: "PR closed without merging: [${{ github.event.pull_request.title }}](${{ github.event.pull_request.html_url }})" |
44 | | - server: "matrix.org" |
| 33 | + // HTML escape the title |
| 34 | + const titleEscaped = title |
| 35 | + .replace(/&/g, '&') |
| 36 | + .replace(/</g, '<') |
| 37 | + .replace(/>/g, '>') |
| 38 | + .replace(/"/g, '"') |
| 39 | + .replace(/'/g, '''); |
| 40 | +
|
| 41 | + const body = `${prefix}: ${title} - ${url}`; |
| 42 | + const formattedBody = `${prefix}: <a href="${url}">${titleEscaped}</a>`; |
| 43 | +
|
| 44 | + const roomId = '!YoLPkieCYHGzdjUhOK:matrix.org'; |
| 45 | + const accessToken = process.env.MATRIX_ACCESS_TOKEN; |
| 46 | +
|
| 47 | + const response = await fetch( |
| 48 | + `https://matrix.org/_matrix/client/r0/rooms/${roomId}/send/m.room.message`, |
| 49 | + { |
| 50 | + method: 'POST', |
| 51 | + headers: { |
| 52 | + 'Content-Type': 'application/json', |
| 53 | + 'Authorization': `Bearer ${accessToken}` |
| 54 | + }, |
| 55 | + body: JSON.stringify({ |
| 56 | + msgtype: 'm.text', |
| 57 | + format: 'org.matrix.custom.html', |
| 58 | + body: body, |
| 59 | + formatted_body: formattedBody |
| 60 | + }) |
| 61 | + } |
| 62 | + ); |
| 63 | +
|
| 64 | + if (!response.ok) { |
| 65 | + throw new Error(`Matrix API request failed: ${response.status} ${response.statusText}`); |
| 66 | + } |
| 67 | + env: |
| 68 | + MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }} |
0 commit comments