Skip to content

Instantly share code, notes, and snippets.

@rtrcolin
Last active January 8, 2024 13:59
Show Gist options
  • Select an option

  • Save rtrcolin/4455097 to your computer and use it in GitHub Desktop.

Select an option

Save rtrcolin/4455097 to your computer and use it in GitHub Desktop.

Revisions

  1. rtrcolin revised this gist Jan 4, 2013. 2 changed files with 22 additions and 21 deletions.
    22 changes: 22 additions & 0 deletions jiraRefreshTickets.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,25 @@
    // 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
    */
    21 changes: 0 additions & 21 deletions jiraSetup.js
    Original file line number Diff line number Diff line change
    @@ -1,21 +0,0 @@
    // 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);
    };
  2. rtrcolin revised this gist Jan 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jiraRefreshTickets.js
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ function refreshTickets(){
    // Show the user a friendly message
    Browser.msgBox("Jira Tickets","Updating " + numRows + " tickets",Browser.Buttons.OK);

    for (var i = 1; i <= numRows - 1; i++) {
    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);
  3. rtrcolin revised this gist Jan 4, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions jiraSetup.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@


    // 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
  4. rtrcolin created this gist Jan 4, 2013.
    52 changes: 52 additions & 0 deletions jiraRefreshTickets.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    /**
    * 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 - 1; 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;
    }
    }
    }
    22 changes: 22 additions & 0 deletions jiraSetup.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@


    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);
    };