name: Matrix bot
on:
pull_request_target:
types: [opened, closed]
jobs:
notify:
if: github.repository == 'smoltcp-rs/smoltcp'
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: send message
uses: actions/github-script@v8
with:
script: |
const action = context.payload.action;
const merged = context.payload.pull_request.merged;
const title = context.payload.pull_request.title;
const url = context.payload.pull_request.html_url;
// Determine message prefix
let prefix;
if (action === 'opened') {
prefix = 'New PR';
} else if (action === 'closed' && merged) {
prefix = 'PR merged';
} else if (action === 'closed' && !merged) {
prefix = 'PR closed without merging';
} else {
return; // Unknown action, skip
}
// HTML escape the title
const titleEscaped = title
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
const body = `${prefix}: ${title} - ${url}`;
const formattedBody = `${prefix}: <a href="${url}">${titleEscaped}</a>`;
const roomId = '!jY1WnZYXKZmPkxLiTBTsybQg9pa4EWTYjDkLDeP4WUI';
const accessToken = process.env.MATRIX_ACCESS_TOKEN;
const response = await fetch(
`https://matrix.org/_matrix/client/r0/rooms/${roomId}/send/m.room.message`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify({
msgtype: 'm.text',
format: 'org.matrix.custom.html',
body: body,
formatted_body: formattedBody
})
}
);
if (!response.ok) {
throw new Error(`Matrix API request failed: ${response.status} ${response.statusText}`);
}
env:
MATRIX_ACCESS_TOKEN: ${{ secrets.MATRIX_ACCESS_TOKEN }}