const core = require('@actions/core'); const github = require('@actions/github'); const sdk = require('matrix-js-sdk'); const md = require('markdown-it')(); try { const homeserver = core.getInput('homeserver'); const channel = core.getInput('channel'); const token = core.getInput('token'); const message = core.getInput('message'); const status = core.getInput('status'); // Debug output core.info(`homeserver: ${homeserver}`); core.info(`channel: ${channel}`); core.info(`token: ${token}`); core.info(`message: ${message}`); core.info(`status: ${status}`); const ctx = github.context; const { owner, repo } = ctx.repo; const { ref, workflow, actor, payload, serverUrl, runNumber, } = ctx; const repoURL = `${serverUrl}/${owner}/${repo}`; const runURL = `${repoURL}/actions/runs/${runNumber}`; // Create client object const client = sdk.createClient({ baseUrl: `https://${homeserver}`, accessToken: token, }); // Join channel (if we are already in the channel this does nothing) client.joinRoom(channel).then(() => { core.info('Joined channel'); }); const statusOpts = { success: { status: '🟢 Success', color: 0x28A745, }, failure: { status: '🔴 Failure', color: 0xCB2431, }, cancelled: { status: 'Cancelled', color: 0xDBAB09, }, }; const userMention = status === 'success' ? actor : `${actor}`; const formattedMessage = `
Repository ${owner}/${repo}
Commit ${payload.head_commit.id.substring(0, 7)} ${payload.head_commit.message}
Ref ${ref}
Triggered by ${userMention}
Run #${runNumber}
${md.render(message)}`;
// Send message
const content = {
msgtype: (status === 'success') ? 'm.notice' : 'm.text',
format: 'org.matrix.custom.html',
body: `${statusOpts[status].status} ${workflow} @${actor}`,
formatted_body: formattedMessage,
};
if (status !== 'success') content['m.mentions'] = { user_ids: [`@${actor}:${homeserver}`] };
client.sendEvent(channel, 'm.room.message', content, '').then(() => {
// message sent successfully
}).catch((err) => {
core.error(err);
});
} catch (error) {
core.setFailed(error.message);
}