-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgitlab_chatwork.ts
More file actions
117 lines (109 loc) · 2.91 KB
/
gitlab_chatwork.ts
File metadata and controls
117 lines (109 loc) · 2.91 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
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
let CHATWORK_API_KEY = "";
let CHATWORK_ROOM_ID = "";
let ChatWorkClient;
/* eslint-disable no-unused-vars */
/**
* Set ChatWork API key.
*
* @param {string} key - API key
*/
function setChatworkAPIKey(key: string): void {
/* eslint-enable no-unused-vars */
CHATWORK_API_KEY = key;
}
/* eslint-disable no-unused-vars */
/**
* Set ChatWork Room ID.
*
* @param {string} id - Room ID
*/
function setChatworkRoomId(id: string): void {
/* eslint-enable no-unused-vars */
CHATWORK_ROOM_ID = id;
}
/* eslint-disable no-unused-vars */
/**
* Set ChatWork client.
*
* @param {any} client - ChatWork client.
*/
function setChatWorkClient(client: any): void {
/* eslint-enable no-unused-vars */
ChatWorkClient = client;
}
/* eslint-disable no-unused-vars */
/**
* Send message to ChatWork room.
*
* @param {string} message - Message to send.
*/
function sendMessageToChatwork(message: string): void {
/* eslint-enable no-unused-vars */
const client = ChatWorkClient.factory({ token: CHATWORK_API_KEY });
client.sendMessage({ room_id: CHATWORK_ROOM_ID, body: message });
}
/* eslint-disable no-unused-vars */
/**
* Construct message fron POST data.
*
* @param {Object} data - POST data.
* @return {string}
*/
function constructChatworkMessageFromGitLab(data: Object): string {
/* eslint-enable no-unused-vars */
const title =
data["object_attributes"]["url"] + " is updated by " + data["user"]["name"];
let message = "";
switch (true) {
case data["object_kind"] == "wiki_page":
message = constructChatworkMessageFromGitLabWiki(data);
break;
case data["object_kind"] == "note":
message = constructChatworkMessageFromGitLabNote(data);
break;
case data["object_kind"] == "issue":
message = constructChatworkMessageFromGitLabIssue(data);
break;
default:
message = "not implemented" + JSON.stringify(data);
break;
}
return "[info][title]" + title + "[/title]" + message + "[/info]";
}
/**
* Construct message when the post is from wiki.
*
* @param {Object} data - POST data.
* @return {string}
*/
function constructChatworkMessageFromGitLabWiki(data): string {
return data["object_attributes"]["content"];
}
/**
* Construct message when the post is from note.
*
* @param {Object} data - POST data.
* @return {string}
*/
function constructChatworkMessageFromGitLabNote(data): string {
if ("issue" in data) {
if ("changed the description" == data["object_attributes"]["description"]) {
return (
data["object_attributes"]["description"] +
"\n" +
data["issue"]["description"]
);
}
return data["object_attributes"]["description"];
}
return data["object_attributes"]["note"];
}
/**
* Construct message when the post is from issue.
*
* @param {Object} data - POST data.
* @return {string}
*/
function constructChatworkMessageFromGitLabIssue(data): string {
return data["object_attributes"]["description"];
}