Skip to content

Instantly share code, notes, and snippets.

@rtrcolin
Last active January 8, 2024 13:59
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save rtrcolin/4455097 to your computer and use it in GitHub Desktop.
Save rtrcolin/4455097 to your computer and use it in GitHub Desktop.
Super simple Google Docs integration with Jira Issues/Tickets
// URL for Jira's REST API for issues
var getIssueURL = "https://[Your Jira host]/rest/api/2/issue/";
// Personally I prefer the script to handle request failures, hence muteHTTPExceptions = true
var fetchArgs = {
contentType: "application/json",
headers: {"Authorization":"Basic [Your BASE64 Encoded user:pass]"},
muteHttpExceptions : true
};
/**
* Add a nice menu option for the users.
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Refresh Ticket Data",
functionName : "refreshTickets"
}];
sheet.addMenu("Jira", entries);
};
/**
* Make a request to jira for all listed tickets, and update the spreadsheet
*/
function refreshTickets(){
// Pull the bits and pieces you need from the spreadsheet
var sheet = getTicketSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
// Show the user a friendly message
Browser.msgBox("Jira Tickets","Updating " + numRows + " tickets",Browser.Buttons.OK);
for (var i = 1; i < numRows; i++) {
var row = values[i];
var ri = i + 1; // Set the cell row index
var httpResponse = UrlFetchApp.fetch(getIssueURL + row[0], fetchArgs);
if (httpResponse) {
switch(httpResponse.getResponseCode()){
case 200:
var data = JSON.parse(httpResponse.getContentText());
// Check the data is valid and the Jira fields exist
if(data && data.fields) {
// Set some basic ticket data in your spreadsheet
rows.getCell(ri, 2).setValue(data.fields.issuetype.name);
rows.getCell(ri, 3).setValue(data.fields.reporter.displayName);
rows.getCell(ri, 4).setValue(data.fields.assignee.displayName);
rows.getCell(ri, 5).setValue(data.fields.summary);
}
else {
// Something funky is up with the JSON response.
resetRow(i,"Failed to retrive ticket data!");
}
break;
case 404:
rows.getCell(ri, 5).setValue("This ticket does not exist");
break;
default:
// Jira returns all errors that occured in an array (if using the application/json mime type)
var data = JSON.parse(httpResponse.getContentText());
rows.getCell(ri, 5).setValue("Error: " + data.errorMessages.join(","));
break;
}
}
else {
Browser.msgBox("Jira Error","Unable to make requests to Jira!", Browser.Buttons.OK);
break;
}
}
}
@tpackert
Copy link

Hello,

Thanks for this code, it looks like its just what I need to create dashboards in Google Docs for our JIRA projects.

I am missing something though, is there additional code, I need to add to the spreadsheet. I keep getting getTicketSheet is not defined.

Tom

@FirefighterBlu3
Copy link

@tpackert getTicketSheet() would be a local function that gets the specific sheet you want to place these results on. if you only have one sheet in use, it could be replaced with SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

@maglez
Copy link

maglez commented Jul 15, 2015

Hi and thank you for the code.

I always get an empty response and searching on Google I get lots of posts where most people reports that UrlFetchApp returns an empty response, at least when trying to get to Jira securely.

Is this code still working for you? Do you know what could be the problem?

Getting data from a non-secure web site is fine, but I can't from Jira.

Any help wil be much appreciated.

Miguel.

@ryancollingwood
Copy link

Thanks for this :)

Here's some pointers for others looking to use this in a google scripts/sheets:

Utilities.base64Encode is the function you can use for encoding the credentials as thus:
var encCred = Utilities.base64Encode("username@company.com:jirapassword");
Take note of the : between the username and password

As mentioned above you should specify a local function to , or you could point it to a specific tab in your spreadsheet, e.g. if you have a tab named "Jira Tickets"
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Jira Tickets");

@mniroula
Copy link

How do I define the JIRA project?

@ljay79
Copy link

ljay79 commented Jun 28, 2017

I started with your little snippet, and then i extended it to actual GAS Add-on.
Maybe peoples out there start contributing to improve the add-on even more.

Check it out, feedback welcome: https://github.com/ljay79/jira-tools/

@PravinrajV
Copy link

I still face the unauthorised 401 error.
I tried the var encCred = Utilities.base64Encode("username@company.com:jirapassword");
Still same error.
Any help appreciated.

@PravinrajV
Copy link

How to fetch a multi select custom field?

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