85 lines
2.5 KiB
JavaScript

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 : `<a href="https://matrix.to/#/@${actor}:${homeserver}">${actor}</a>`;
const formattedMessage = `
<h4 data-mx-color="${statusOpts[status].color}">${statusOpts[status].status} ${workflow}</h4>
<p><strong>Repository</strong> <a href="${repoURL}">${owner}/${repo}</a><br>
<strong>Commit</strong> <a href="${payload.head_commit.url}">${payload.head_commit.id.substring(0, 7)}</a> ${payload.head_commit.message}<br>
<strong>Ref</strong> ${ref}<br>
<strong>Triggered by</strong> ${userMention}<br>
<strong>Run</strong> <a href="${runURL}">#${runNumber}</a>
${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);
}