Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jiraboy - Status compilation from JIRA API - Response JSON set to a variable
function jiraAPIResponseProcessor(data) {
/**
* This is the API to be exposed which creates the status HTML from given `status` json.
* @param {json} status - Your json from JIRA API
* @param {string} checkDate - The date for which the status has to be created,
* like if you want to create status for comments from 2 days ago, etc.
* Format: yyyy-MM-dd. Default: today's date
* @return {string} Final status HTML
*/
function processStatus(status, checkDate) {
if (!status) return;
checkDate = checkDate ? new Date(checkDate) : new Date();
checkDate.setHours(0, 0, 0, 0);
//initialize final status object after processing `status` and cleaning it up
var finalStatus = {
issues: []
};
for (var i = 0; i < status.issues.length; i++) {
//loop through each JIRA issue that came up in my JQL
var processedIssueDetails = _processIssue(status.issues[i], checkDate);
if (processedIssueDetails) finalStatus.issues.push(processedIssueDetails);
}
//generate HTML for all processed JIRA issues, including issue description and comments
var statusDiv = $('<div></div>');
$('#jira-status').tmpl(finalStatus).appendTo(statusDiv);
return statusDiv.html();
}
function _processIssue(item, checkDate) {
//initialize and get basic details required for your status
var processedIssueDetails = {
title: item.fields.summary,
link: getMyIssueLink(item.key),
description: item.renderedFields.description,
comments: [] //array to accomodate multiple comments on same issue on the same day
};
for (var j = 0; j < item.renderedFields.comment.comments.length; j++) {
//loop through each JIRA issue's comments
var processedComment = _processIssueComment(item.fields.comment.comments[j], item.renderedFields.comment.comments[j], checkDate);
//add to comments if valid
if (processedComment) {
processedComment.mailToSubject = escape(processedIssueDetails.title);
processedIssueDetails.comments.push(processedComment);
}
}
//if no comments at all, do not send anything
return processedIssueDetails.comments.length ? processedIssueDetails : null;
}
function _processIssueComment(issueComment, renderedIssueComment, checkDate) {
//collect relevant issue properties together
var processedComment = {
html: renderedIssueComment.body,
author: issueComment.author && issueComment.author.displayName,
date: issueComment.created,
mailToSubject: '', //we'll fill this in parent function
mailToBody: ''
};
//your status prefix to be detected in comments
var regexStatus = /\&#91;statuscomment\&#93;/i;
//comments validations
if (!regexStatus.test(processedComment.html))
return null; //comment should start with "[statuscomment]"
var createdDate = new Date(processedComment.date);
if (createdDate < checkDate) return null; //comment should be of today
//regex to detect and remove the `statuscomment` to remove it from final HTML
var regexCommentCleanup = /<span class=\"error\">\&#91;statuscomment\&#93;<\/span>(<br\/>)?/ig;
//comment cleanup and add to data
processedComment.html = processedComment.html.replace(regexCommentCleanup, '');
processedComment.date = ''; //not required anymore
//additional reply-to feature for quick reply to issues in status mail
processedComment.mailToBody = $(processedComment.html).text();
if (processedComment.mailToBody) {
processedComment.mailToBody = ' \n\n____________________\n\n'
+ processedComment.author + ' comment: '
+ processedComment.mailToBody;
processedComment.mailToBody = escape(processedComment.mailToBody);
}
return processedComment;
}
function getMyIssueLink(key) {
return "https://myproject.atlassian.cloud/browse/" + key;
}
function htmlEncode(value) {
// Explanation: Create an in-memory div, set it's inner text (which jQuery automatically encodes)
// then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
//run your code and get your final processed status html
return processStatus(data, "2016-02-11"));
//NOTE: date parameter is optional. Above date is as per `03-jira-api-result-sample.json`,
//so that when you run the script, it builds you _that day's_ status HTML, accordingly.
}
<script id="jira-comments" type="x-jquery-tmpl">
<table border="0" cellpadding="1" cellspacing="1" class="issue-article" style="width: 100%;">
<tr>
<td>{{html description}}</td>
</tr>
{{each comments}}
<tr>
<td class="comment">
{{tmpl($value) "#jira-comment"}}
</td>
</tr>
{{/each}}
</table>
</script>
<script id="jira-comment" type="x-jquery-tmpl">
<table class="comment-head" width="100%">
<tr>
<td align="left"><span class="title" style="font-weight: 600; float:left;">Comment:</span></td>
<td align="right">
<span class="author"> (By ${author})</span>&nbsp;
<a class="reply-to" href="mailto:[ReplyToEmail]?Subject=RE:%20${mailToSubject}&Body=${mailToBody}">Reply &gt;</a>
</td>
</tr>
</table>
<div>{{html html}}</div>
</script>
<script id="jira-status" type="x-jquery-tmpl">
<html>
<body>
{{each issues}}
<a href="${link}">${title}</a><br />
{{tmpl($value) "#jira-comments"}}
{{/each}}
</body>
</html>
</script>
{
"expand": "schema,names",
"startAt": 0,
"maxResults": 50,
"total": 1,
"issues": [{
"expand": "operations,versionedRepresentations,editmeta,changelog,transitions,renderedFields",
"id": "22116",
"self": "https://myproject.atlassian.cloud/rest/api/latest/issue/22116",
"key": "PROJ-1340",
"fields": {
"summary": "Reports - Drip Reports Oddity - Super watch",
"description": "*Task one* This is the task description of our drip reports oddity super watch issue.",
"comment": {
"startAt": 0,
"maxResults": 1,
"total": 1,
"comments": [{
"self": "https://myproject.atlassian.cloud/rest/api/2/issue/22116/comment/22645",
"id": "22645",
"author": {
"self": "https://myproject.atlassian.cloud/rest/api/2/user?username=Krishnan",
"name": "Krishnan",
"key": "krishnan",
"emailAddress": "krishnan@myproject.com",
"displayName": "Krishnan Mudaliar",
"active": true,
"timeZone": "Asia/Kolkata"
},
"body": "[statuscomment]\r\nHello, this is my first status comment today, but second overall",
"created": "2016-02-19T16:40:42.177+0530"
}, {
"self": "https://myproject.atlassian.cloud/rest/api/2/issue/22116/comment/22645",
"id": "22640",
"author": {
"self": "https://myproject.atlassian.cloud/rest/api/2/user?username=Krishnan",
"name": "Krishnan",
"key": "krishnan",
"emailAddress": "krishnan@myproject.com",
"displayName": "Krishnan Mudaliar",
"active": true,
"timeZone": "Asia/Kolkata"
},
"body": "[statuscomment]\r\nHello, this is my _first status comment_ *ever*.",
"created": "2016-02-18T16:40:42.177+0530"
}]
}
},
"renderedFields": {
"summary": null,
"description": "<strong>Task one</strong> This is the task description of our drip reports oddity super watch issue.",
"comment": {
"startAt": 0,
"maxResults": 1,
"total": 1,
"comments": [{
"self": "https://myproject.atlassian.cloud/rest/api/2/issue/22116/comment/22645",
"id": "22645",
"author": {
"self": "https://myproject.atlassian.cloud/rest/api/2/user?username=Krishnan",
"name": "Krishnan",
"key": "krishnan",
"emailAddress": "krishnan@myproject.com",
"displayName": "Krishnan Mudaliar",
"active": true,
"timeZone": "Asia/Kolkata"
},
"body": "<p><span class=\"error\">&#91;statuscomment&#93;</span></p>\n\n<p>Hi, this is my first status comment today, but second overall.</p>",
"created": "2016-02-19T16:40:42.177+0530"
}, {
"self": "https://myproject.atlassian.cloud/rest/api/2/issue/22116/comment/22645",
"id": "22640",
"author": {
"self": "https://myproject.atlassian.cloud/rest/api/2/user?username=Krishnan",
"name": "Krishnan",
"key": "krishnan",
"emailAddress": "krishnan@myproject.com",
"displayName": "Krishnan Mudaliar",
"active": true,
"timeZone": "Asia/Kolkata"
},
"body": "<p><span class=\"error\">&#91;statuscomment&#93;</span></p>\n\n<p>Hi, and this is my <i>first status comment</i> <b>ever</b>.</p>",
"created": "2016-02-18T16:40:42.177+0530"
}]
}
}
}],
"names": {
"summary": "Summary",
"description": "Description",
"comment": "Comment"
},
"schema": {
"summary": {
"type": "string",
"system": "summary"
},
"description": {
"type": "string",
"system": "description"
},
"comment": {
"type": "comments-page",
"system": "comment"
}
}
}
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
<script type="text/javascript">
//NOTE: jiraAPIResponse is your JIRA API Result-set, like `03-jira-api-result-sample.json`
var finalHTML = jiraAPIResponseProcessor(jiraAPIResponse);
$('#div-final-html').html(finalHTML); //this fills the div shown below
</script>
<div id="div-final-html">
<a href="https://myproject.atlassian.cloud/browse/PROJ-1340">Reports - Drip Reports Oddity - LandWatch</a><br>
<table border="1" cellpadding="1" cellspacing="1" class="issue-article" style="width: 100%;">
<tbody>
<tr>
<td><strong>Task one</strong> This is your task description</td>
</tr>
<tr>
<td class="comment">
<table class="comment-head" width="100%">
<tbody>
<tr>
<td align="left"><span class="title" style="font-weight: 600; float:left;">Comment:</span></td>
<td align="right"> <span class="author"> (By Krishnan Mudaliar)</span>&nbsp; <a class="reply-to" href="mailto:[ReplyToEmail]?Subject=RE:%20Reports%20-%20Drip%20Reports%20Oddity%20-%20LandWatch&amp;Body=%20%0A%0A____________________%0A%0AKrishnan%20Mudaliar%20comment%3A%20%0A%0AHi%2C%20this%20is%20my%20first%20status%20comment%20today%2C%20but%20second%20overall.">Reply &gt;</a> </td>
</tr>
</tbody>
</table>
<div>
<p></p>
<p>Hi, this is my first status comment today, but second overall.</p>
</div>
</td>
</tr>
<tr>
<td class="comment">
<table class="comment-head" width="100%">
<tbody>
<tr>
<td align="left"><span class="title" style="font-weight: 600; float:left;">Comment:</span></td>
<td align="right"> <span class="author"> (By Krishnan Mudaliar)</span>&nbsp; <a class="reply-to" href="mailto:[ReplyToEmail]?Subject=RE:%20Reports%20-%20Drip%20Reports%20Oddity%20-%20LandWatch&amp;Body=%20%0A%0A____________________%0A%0AKrishnan%20Mudaliar%20comment%3A%20%0A%0AHi%2C%20and%20this%20is%20my%20first%20status%20comment%20ever.">Reply &gt;</a> </td>
</tr>
</tbody>
</table>
<div>
<p></p>
<p>Hi, and this is my <i>first status comment</i> <b>ever</b>.</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment