-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
178 lines (159 loc) · 6.19 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { setFailed, getInput } from '@actions/core';
import { context } from '@actions/github';
import axios from 'axios';
/**
* check field validity
* @param {*} field
* @param {*} message
*/
const checkFieldValidity = (field, message) => {
if (!field) {
throw Error(message);
}
}
/**
* send telegram text message
* @param {*} token
* @param {*} chat_id
* @param {*} text
* @param {*} thread_id
* @param {*} disable_web_page_preview
* @param {*} disable_notification
*/
const sendTextMessage = async (token, chat_id, text, thread_id = null, disable_web_page_preview = false, disable_notification = false) => {
const URL = new URLSearchParams(`chat_id=${chat_id}`);
/**
* append params
* @param {*} name
* @param {*} param
*/
const appendFn = (name, param) => {
if (param) {
URL.append(name, param);
}
}
appendFn('message_thread_id', thread_id);
appendFn('disable_web_page_preview', disable_web_page_preview);
appendFn('disable_notification', disable_notification);
URL.append('text', text);
URL.append('parse_mode', 'Markdown');
try {
await axios.get(`/bot${token}/sendMessage`, {
baseURL: 'https://api.telegram.org',
params: URL
});
} catch (error) {
console.error('Error Sending Telegram Message', error);
}
}
/**
*
* get and prevalidate action inputs
*/
const parseAndValidateInputs = () => {
// get & check inputs and validity
const token = getInput('token');
const to = getInput('to');
const thread_id = getInput('thread_id');
const disable_web_page_preview = getInput('disable_web_page_preview') || false;
const disable_notification = getInput('disable_notification') || false;
const status = getInput('status');
const event = getInput('event');
checkFieldValidity(token, 'Token is not valid');
checkFieldValidity(to, 'to address is not valid');
return {
token, to, thread_id, disable_web_page_preview,
disable_notification, status, event
};
}
/**
* compose our message
* @param {*} status
* @param {*} event
* @returns
*/
const composer = (status, event) => {
const icons = { "failure": "❗️", "cancelled": "❕", "success": "✅" };
const action = context?.payload?.action;
const senderUser = context?.payload?.sender?.login;
const userURL = context?.payload?.sender?.html_url;
/**
* every event has a handler method that returns the corresponding message to the event and it's action
*/
const enevtHandlers = {
"issue_comment": {
fn: () => {
const { payload: { issue: { html_url, number } } } = context;
if (action !== 'created') return null;
return `💬 new comment on [#${number}](${html_url})`;
}
},
"issues": {
fn: () => {
const { issue: { number, html_url: issueURL } } = context?.payload;
if (action === 'assigned') {
const { assignee: { login: assineeUserName, html_url: asigneeURL } } = context?.payload;
return `📝 issue [#${number}](${issueURL}) has been assigned to [${assineeUserName}](${asigneeURL})`;
} else if (action === 'labeled') {
const { label: { name: labelName, url: labelURL } } = context?.payload;
return `🏷️ issue [#${number}](${issueURL}) has been labeled as [${labelName}](${labelURL})`;
} else {
return `🏷️ issue [#${number}](${issueURL}) has been ${action}`;
}
}
},
"pull_request": {
fn: () => {
const { pull_request: { number, html_url: prURL } } = context?.payload;
if (action === 'create') {
return `📦 PR [#${number}](${prURL}) has been created`;
} if (action === 'ready_for_review') {
return `📦 PR [#${number}](${prURL}) is now ready for review`;
} if (action === 'review_requested') {
return `📦 review is requested on PR [#${number}](${prURL})`;
} else {
return `📦 PR [#${number}](${prURL}) has been ${action}`;
}
}
},
"push": {
fn: () => {
const { ref, commits, repository: { html_url: repoURL } } = context?.payload;
const branchName = ref.split('/').reverse()[0];
const branchURL = `${repoURL}/tree/${branchName}`
let commitList = ``;
let index = 1;
for (let commit of commits) {
const { url, message, committer: { name, username } } = commit;
const committerURL = `https://github.com/${username}`;
commitList += `\n ${index++}- [${message.replace(/[\r\n]+/g, " ")}](${url}) by [${name}](${committerURL})`
}
return `🆕 new changes pushed to [${branchName}](${branchURL}) \n total commits: ${commits.length} ${commitList}`;
}
},
"pull_request_review_comment": {
fn: () => {
const { pull_request: { number, html_url: prURL } } = context?.payload;
return `📦 PR review comment on [#${number}](${prURL}) has been ${action}`;
}
},
"default": {
fn: () => {
return `something went wrong! I couldn't find the event ${event}!`;
}
}
};
let handledEvent = (enevtHandlers[event]) ? enevtHandlers[event].fn() : enevtHandlers['default'].fn();
handledEvent += `\n Action by [${senderUser}](${userURL}) \n Action status: ${icons[status]} ${status}`;
return handledEvent;
}
async function run() {
// get & check inputs and validity
const {
event: Event, status,
token, to, thread_id, disable_web_page_preview,
disable_notification } = parseAndValidateInputs();
const message = composer(status, Event);
sendTextMessage(token, to, message, thread_id, disable_web_page_preview, disable_notification);
}
run().catch(e => setFailed(e));