-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
68 lines (52 loc) · 1.98 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios');
async function sendMessage(roomId, text, token, messageId, ref, eventName, actionUrl) {
console.log("RoomID: " + roomId);
console.log("MessageID: " + messageId);
console.log("Ref: " + ref);
console.log("EventName: " + eventName);
console.log("ActionUrl: " + actionUrl);
let url = `https://api.gitter.im/v1/rooms/${roomId}/chatMessages`;
const headers = {
"Content-Type": 'application/json',
"Accept": 'application/json',
"Authorization": `Bearer ${token}`
};
const completeText = `**Ref:** ${ref}\n**Event:** ${eventName}\n**Action:** ${actionUrl}\n**Message:** ${text}`;
const body = {
"text": completeText
};
const config = {
headers: headers
};
if (messageId != null && messageId !== '') {
url = `${url}/${messageId}`;
console.log(`PUT: ${url}, Body: ${JSON.stringify(body)}`);
await axios.put(url, body, config);
return messageId;
} else {
console.log(`POST: ${url}, Body: ${JSON.stringify(body)}`);
const response = await axios.post(url, body, config);
return response.data.id;
}
}
async function run() {
try {
const roomId = core.getInput('room-id');
const text = core.getInput('text');
const token = core.getInput('token');
const messageId = core.getInput('message-id');
const ref = github.context.ref;
const eventName = github.context.eventName;
const repository = github.context.repo;
const sha = github.context.sha;
const actionUrl = `https://github.com/${repository.owner}/${repository.repo}/commit/${sha}/checks`;
const newMessageId = await sendMessage(roomId, text, token, messageId, ref, eventName, actionUrl);
core.setOutput('message-id', newMessageId);
}
catch (error) {
core.setFailed(error.message);
}
}
run();