Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,560,525
Community Members
 
Community Events
185
Community Groups

Using Google Apps Script to have JIRA API pull in issue ticket field values to Google Sheet

I wanted to automate a Google Sheet that I have been creating manually for a weekly meeting which shows the issues in a project that have not been updated in the last 15 days. When running the script, but nothing writes to the Google Sheet despite no error showing up. What I did note was that after running a "console.log(response.getContentText())" before the IF, I see that my query is returning a Cloudflare error

ax2jEZL

 

Is there something improper in my construction and/or use of the JIRA API to cause this?

 

function getJiraIssues() {
  var username = "<my_jira_username>";
  var apiToken = "<my_jira_password>";
  var jqlQuery = "project=xyz AND issuetype = Bug)";

  var sheet = SpreadsheetApp.getActiveSheet();
  var headers = sheet.getRange(1, 1, 1, 8).getValues()[0];

  var options = {
    'method': 'get',
    'contentType': 'application/json',
    'headers': {
      'Authorization': 'Basic ' + Utilities.base64Encode(username + ':' + apiToken)
    },
    'muteHttpExceptions': true
  };

  var url = 'https://<my_jira_instance>.jira.com/rest/api/2/search?jql=' + encodeURIComponent(jqlQuery);

  var response = UrlFetchApp.fetch(url, options);

  if (response.getResponseCode() === 200) {
    var data = JSON.parse(response.getContentText());

    var issues = data.issues.map(function(issue) {
      var issueData = {};

      issueData['Issue Summary'] = issue.fields.summary;
      issueData['Assignee'] = issue.fields.assignee ? issue.fields.assignee.displayName : '';
      issueData['Issue Key'] = issue.key;
      issueData['Issue Type'] = issue.fields.issuetype.name;
      issueData['Component/s'] = issue.fields.components.map(function(component) {
        return component.name;
      }).join(', ');
      issueData['Status'] = issue.fields.status.name;
      issueData['Created'] = issue.fields.created;
      issueData['Updated'] = issue.fields.updated;

      return issueData;
    });

    var row = sheet.getLastRow() + 1;
    var numRows = issues.length;
    var numCols = headers.length;

    sheet.getRange(row, 1, numRows, numCols).setValues(issues.map(function(issue) {
      return headers.map(function(header) {
        return issue[header];
      });
    }));
  }
}

1 answer

0 votes
Oday Rafeh
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Mar 27, 2023

Hi @Jeff A

It seems that the JQL query string you're using has an extra closing parenthesis at the end, which is causing the Cloudflare error.

Please try to use this : 

 

function getJiraIssues() {
var username = "<my_jira_username>";
var apiToken = "<my_jira_password>";
var jqlQuery = "project=xyz AND issuetype=Bug";

var sheet = SpreadsheetApp.getActiveSheet();
var headers = sheet.getRange(1, 1, 1, 8).getValues()[0];

var options = {
'method': 'get',
'contentType': 'application/json',
'headers': {
'Authorization': 'Basic ' + Utilities.base64Encode(username + ':' + apiToken)
},
'muteHttpExceptions': true
};

var url = 'https://<my_jira_instance>.jira.com/rest/api/2/search?jql=' + encodeURIComponent(jqlQuery);

var response = UrlFetchApp.fetch(url, options);

if (response.getResponseCode() === 200) {
var data = JSON.parse(response.getContentText());

var issues = data.issues.map(function(issue) {
var issueData = {};

issueData['Issue Summary'] = issue.fields.summary;
issueData['Assignee'] = issue.fields.assignee ? issue.fields.assignee.displayName : '';
issueData['Issue Key'] = issue.key;
issueData['Issue Type'] = issue.fields.issuetype.name;
issueData['Component/s'] = issue.fields.components.map(function(component) {
return component.name;
}).join(', ');
issueData['Status'] = issue.fields.status.name;
issueData['Created'] = issue.fields.created;
issueData['Updated'] = issue.fields.updated;

return issueData;
});

var row = sheet.getLastRow() + 1;
var numRows = issues.length;
var numCols = headers.length;

sheet.getRange(row, 1, numRows, numCols).setValues(issues.map(function(issue) {
return headers.map(function(header) {
return issue[header];
});
}));
}
}

 

the corrected query string is

 

var jqlQuery = "project=xyz AND issuetype=Bug";

That's a good catch, thank you very much. Unfortunately, I am still getting the same Cloudflare Error even after correcting it.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events