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'); const messagetype = core.getInput('messagetype'); // Debug output core.info(`homeserver: ${homeserver}`); core.info(`channel: ${channel}`); core.info(`token: ${token}`); core.info(`message: ${message}`); core.info(`status: ${status}`); core.info(`messagetype: ${messagetype}`); const ctx = github.context; const { owner, repo } = ctx.repo; const { 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 = `

${statusOpts[status].status} ${workflow.name}

Repository ${owner}/${repo}
Commit ${payload.head_commit.id.substring(0, 7)} ${payload.head_commit.message}
Triggered by ${userMention}
Run #${runNumber}
${md.render(message)}`; // Send message const content = { msgtype: messagetype, format: 'org.matrix.custom.html', body: message, 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); }