Skip to content

Instantly share code, notes, and snippets.

@AysadKozanoglu
Last active September 10, 2019 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AysadKozanoglu/de7a6780561dc08c6626de0e0e14abba to your computer and use it in GitHub Desktop.
Save AysadKozanoglu/de7a6780561dc08c6626de0e0e14abba to your computer and use it in GitHub Desktop.
RocketChat Jira webhook Integration
/*jshint esnext:true*/
const DESC_MAX_LENGTH = 140;
const JIRA_LOGO = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACRElEQVRYhbWXsUscQRTGf4iIyHHIISIWIsHisMgfkNIiBJFwiKQIkipVqpA/wEZEggSxEkmZwiKI5A84REKKkIMQrINYBQmHBDmEHJdNMW+42dk3d3O76wcDu2/e973vZvfN7EF+PAfaMjYL6AzFJFBRYh0gkdEBpryciuQVwjPgFugCu068CvQcAz1g2pnfEc6taOTGL6dIAjxw5nad+FsnvuhxrosYuPbElrz5Rc8Ucu9yfhcxsAncYZZ4fwTeO+HcUcILWgFqOXg1si9vFBrAXB7iEMySfYQZzGCeWxdoAq+Bh8BYjoJjwn0jWrYrqsOIbdIvUQLseTmPgHXgiYx1ibnYU3RuYpyfKMQ/mNWx+KzkfHHmZ4Tj55zGGNhQiAlw5OQ8VeYbzvxRQCNqUxoHLgMCa07eRyd+4sTXAtwrYCLGAJje1URugLrkVIHvMuyLVZccjfsitrhFMyD0k36bTtA/cOZkTuOckaOTFtA7IgEuSG9ONeBHILctWrnwGNO/mvA3zAk4LddaThfTpoXwKiBuVyL0yxPhloLtAUVCY7us4hb7IxQ/KLu4xWFE8cP7Kg6mld4PKH5BvoNrZBMfBphohKnFMAusyvU48ClgoA3M34eBUynwUu6ngK8BE1Gn3ihYccR79Jd5nuyXsx0rZRo498Q7mK8dMDudZuC8rOLLgQI7Ts5xIGe5DANbinCP9AfmEul/SnZslWHgTBFuKnna8a3lpRCzadSVWMiAj6GPIMbAX+/+H9BS8loyN4ibwX9j/jIXDkk+pgAAAABJRU5ErkJggg==';
function stripDesc(str) {
return (str && str.length > DESC_MAX_LENGTH) ? str.slice(0, DESC_MAX_LENGTH - 3) + '...' : str;
}
function prepareAttachment({issue, user}, text) {
let issueType = issue.fields.issuetype;
let res = {
author_name: user.displayName
, author_icon: user.avatarUrls['24x24']
, thumb_url: issueType.iconUrl
};
if (text) {
text = text.replace(/\{\{(user|issue)\.([^a-z_0-9]+)\}\}/g, (m, type, key) => (type==='user' ? user : issue)[key]);
res.text = text;
}
return res;
}
class Script {
process_incoming_request({request}) {
const data = request.content;
try {
if (!data.issue || (data.user && data.user.name === 'gitlab')) {
return;
}
let issue = data.issue;
let baseJiraUrl = issue.self.replace(/\/rest\/.*$/, '');
let user = data.user;
let assignedTo = (issue.fields.assignee && issue.fields.assignee.name !== user.name) ? `, assigned to ${issue.fields.assignee.displayName}` : '';
let issueSummary = `[${issue.key}](${baseJiraUrl}/browse/${issue.key}) ${issue.fields.summary} _(${issue.fields.priority.name.replace(/^\s*\d*\.\s*/, '')}${assignedTo})_`;
let message = {
icon_url: (issue.fields.project && issue.fields.project.avatarUrls && issue.fields.project.avatarUrls['48x48']) || JIRA_LOGO
, attachments: []
};
if (data.webhookEvent === 'jira:issue_created') {
message.attachments.push(prepareAttachment(data, `*Created* ${issueSummary}:\n${stripDesc(issue.fields.description)}`));
} else if (data.webhookEvent === 'jira:issue_deleted') {
message.attachments.push(prepareAttachment(data, `*Deleted* ${issueSummary}`));
} else if (data.webhookEvent === 'jira:issue_updated') {
if (data.changelog && data.changelog.items) { // field update
let logs = [];
data.changelog.items.forEach((change) => {
if (!change.field.match('status|resolution|comment|priority') ) {
return;
}
if (change.field==='description') {
logs.push(`Changed *description* to:\n${stripDesc(change.toString)}`);
} else {
logs.push(`*${change.field}* changed from ${change.fromString} to *${change.toString}*`);
}
});
logs.length && message.attachments.push(prepareAttachment(data, `*Updated* ${issueSummary}:\n - ${logs.join('\n - ')}`));
}
if (data.comment) { // comment update
let comment = data.comment;
let action = comment.created !== comment.updated ? 'Updated comment' : 'Commented';
message.attachments.push(prepareAttachment(data, `*${action}* on ${issueSummary}:\n${stripDesc(comment.body)}`));
}
}
if (message.text || message.attachments.length) {
return {content:message};
}
} catch(e) {
console.log('jiraevent error', e);
return {
error: {
success: false,
message: `${e.message || e} ${JSON.stringify(data)}`
}
};
}
}
}

notify on issue creation, deletion and status, resolution, comment or priority changes.

  1. In Rocket.Chat go to “Administration”->”Integrations” and create “New Integration”
  2. Choose Incoming WebHook
  3. Follow all instructions like Enable, give it a name, link to channel etc. Set “Enable Script” to true and enter content of this script in the “Script” box
  4. Press Save changes and copy the Webhook URL (added just below the script box)
  5. Go to your jira as administrator and follow instructions on adding outgoing webhook here

You can tweak the content of the script to better suit your needs Add Jira integration via Outgoing WebHook

Integration for Rocket.Chat that summarizes any JIRA issues mentioned.

Go to https://github.com/gustavkarlsson/rocketchat-jira-trigger and follow the instructions.

Example of Jira integration:

example image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment