Skip to content

Commit

Permalink
parser for issue and comment
Browse files Browse the repository at this point in the history
  • Loading branch information
feliperbroering committed Jun 21, 2020
1 parent 483498a commit c017e44
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 38 deletions.
54 changes: 52 additions & 2 deletions src/controllers/JiraController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,61 @@
const axios = require('axios');
const TelegramController = require("./TelegramController");

const n = `<pre>\n</pre>`;

const JiraWebhookParser = {
issue: (action, details) => {
console.log(`Parsing message for ${details.webhookEvent}`);
const { user, issue, changelog } = details;

const userActionInfo = `${user.displayName} ${action} the issue`;
const jiraURL = `${process.env.JEERAGRAM_JIRA_URL}/browse/${issue.key}`;
const issueInfo = `<a href="${jiraURL}">${issue.fields.issuetype.name}: ${issue.key} ${issue.fields.summary}</a>${n}`

let changed = "";
changelog.items.forEach(item => {
const { field, fromString, toString } = item;
changed += `<b>${field}</b> from <i>${fromString}</i> → <code>${toString}</code>${n}`;
})

return message = `${userActionInfo} ${issueInfo}${n}${changed}`;
},

comment: (action, details) => {
console.log(`Parsing message for ${details.webhookEvent}`);
const { issue, comment } = details;
const { updateAuthor } = comment;

const userActionInfo = `💬 ${updateAuthor.displayName} ${action} a comment on`;

const jiraURL = `${process.env.JEERAGRAM_JIRA_URL}/browse/${issue.key}`;
const issueInfo = `<a href="${jiraURL}">${issue.fields.issuetype.name}: ${issue.key} ${issue.fields.summary}</a>${n}`

const commentInfo = `<i>${comment.body}</i>`

return message = `${userActionInfo} ${issueInfo}${n}${commentInfo}`;
}
}

const JiraController = {
hook: async (request, response) => {
console.log(`Jira message:`, JSON.stringify(request.body));
await TelegramController.sendMessage(request);
return response.json({ message: `Jira webhook received! Sent to telegram!` });
const details = request.body;
const { webhookEvent } = details;
const entity = webhookEvent.split('_')[0].replace(/jira:/g, '');
const action = webhookEvent.split('_')[1];
parser = JiraWebhookParser[entity];
if (parser){
const message = parser(action, details);
await TelegramController.sendMessage(message);
return response.json({ message: `Jira webhook received! Sent to telegram!` });
}
else {
const message = `Parser not yet implemented for ${details.webhookEvent} 🙄${n}`;
const detailsJSON = `<pre><code class="language-JSON">${JSON.stringify(details, null, 4)}</code></pre>`;
await TelegramController.sendMessage(`${message}${detailsJSON}`);
return response.json(message);
}
},

setWebhook: async (request, response) => {
Expand Down
46 changes: 11 additions & 35 deletions src/controllers/TelegramController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,10 @@ const TelegramBot = require("node-telegram-bot-api");
const telegram = new TelegramBot(process.env.JEERAGRAM_BOT_TOKEN);

telegram.on('message', msg => {
const echoMsg = `<pre><code class="language-JSON">${JSON.stringify(msg, null, 4)}</code></pre>`
telegram.sendMessage(msg.chat.id, echoMsg, {parse_mode: "HTML"});
const echoMessage = `<pre><code class="language-JSON">${JSON.stringify(msg, null, 4)}</code></pre>`
telegram.sendMessage(msg.chat.id, echoMessage, {parse_mode: "HTML"});
});

notifyTelegram = async (request) => {
const { issue, webhookEvent, changelog, comment, sprint } = request.body;
const n = `<pre>\n</pre>`;
let issueInfo = "";
if (issue){
const jiraURL = `${process.env.JEERAGRAM_JIRA_URL}/browse/${issue.key}`;
issueInfo = `<a href="${jiraURL}">${issue.key}: ${issue.fields.summary}</a>${n}`
}
let details = "{}";
if (webhookEvent.startsWith("jira:issue")){
details = JSON.stringify(changelog.items, null, 4);
}
else if (webhookEvent.startsWith("comment")){
details = JSON.stringify(comment, null, 4);
}
else if (webhookEvent.startsWith("sprint")){
details = JSON.stringify(sprint, null, 4);
}
// version released
const detailsInfo = `<pre><code class="language-JSON">${details}</code></pre>`
let message = `${issueInfo}${webhookEvent}:${detailsInfo}`;
telegram.sendMessage(
process.env.JEERAGRAM_CHAT_IDS,
message,
{
parse_mode: "HTML",
disable_web_page_preview: true
}
);
};

const TelegramController = {

hook: async (request, response) => {
Expand Down Expand Up @@ -83,8 +52,15 @@ const TelegramController = {
});
},

sendMessage: async (request) => {
await notifyTelegram(request);
sendMessage: async (message) => {
telegram.sendMessage(
process.env.JEERAGRAM_CHAT_IDS,
message,
{
parse_mode: "HTML",
disable_web_page_preview: true
}
);
}

};
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ app.use(express.json());
app.use(routes);

app.listen('3000', () => {
console.log("🚀 Telegram-jira service started.");
console.log("🚀 Jeeragram service started.");
});

exports.jeeragram = app;

0 comments on commit c017e44

Please sign in to comment.