-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (78 loc) · 3.23 KB
/
index.js
File metadata and controls
86 lines (78 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const core = require('@actions/core');
const http = require('@actions/http-client');
async function run() {
try {
core.info("Starting Mattermost post GitHub Action...");
const webHookUrl = core.getInput('webhook-url');
const mattermostUrl = core.getInput('mattermost-url');
const channelID = core.getInput('channel-id');
const botToken = core.getInput('bot-token');
const threadId = core.getInput('thread-id');
const text = core.getInput("text");
const color = core.getInput("color");
const useAttachment = core.getInput("use-attachment") === "true";
let title = core.getInput("title") ;
let fallback = text;
core.info(`Inputs received:
webHookUrl: ${webHookUrl}
mattermostUrl: ${mattermostUrl}
channelID: ${channelID}
botToken: ${botToken ? '[REDACTED]' : ''}
threadId: ${threadId}
text: ${text}
color: ${color}
useAttachment: ${useAttachment}
title: ${title}
`);
if (title == '') {
title = undefined;
}
else {
fallback = title + "\n\n" + text;
}
const attachments = [
{
color: color,
title: title,
text: text,
fallback: fallback
}
];
const httpClient = new http.HttpClient('mattermost-post');
if (botToken && threadId) {
// Send message as a reply in the thread using Mattermost API
const apiUrl = `${mattermostUrl}/api/v4/posts`;
const postPayload = {
channel_id: channelID,
root_id: threadId
};
if (useAttachment) {
postPayload.props = { attachments };
} else {
postPayload.message = text;
}
core.info(`Sending POST request to Mattermost API with payload: ${JSON.stringify(postPayload)}`);
const res = await httpClient.postJson(apiUrl, postPayload, {
'Authorization': `Bearer ${botToken}`,
'Content-Type': 'application/json'
});
core.info(`Message posted to Mattermost thread ${threadId} via API. Status code=${res.statusCode}.`);
} else if (webHookUrl) {
// Send message via webhook
core.info("Preparing to send message via webhook...");
const payload = useAttachment ? { attachments } : { text };
core.info(`Sending POST request to webhook URL with payload: ${JSON.stringify(payload)}`);
const res = await httpClient.postJson(webHookUrl, payload);
core.info(`Message posted to Mattermost on URL: ${webHookUrl}. Status code=${res.statusCode}.`);
} else {
core.error("Neither (bot-token and thread-id) nor webhook-url has been defined, cannot continue");
throw new Error("Neither (bot-tokend and thread-id) or webhook-url has been defined, cannot continue")
}
core.info("Mattermost post GitHub Action completed.");
}
catch (error) {
core.error(`Action failed: ${error.message}`);
core.setFailed(error.message);
}
}
run();