Help - adding to Jira

Hi there,

Hoping for some help with creating an action to create issues in a Jira project. I’ve put something together using the example from the HTTP request class in the Drafts documentation and the Jira API documentation that… I think… should work? I’ve put what I have in below (with anonymized data) that seems to work successfully in Jira, but neither presents me with any messages nor creates the issue in Jira.

I’ve just hard-coded the issue data for now while I’m trying to POC this.

Anyone with Jira API experience who can see what I’m doing wrong?

var http = HTTP.create(); // create HTTP object

var response = http.request({
  'url': 'https://MYJIRAURL/rest/api/3/issue/',
  'method': 'POST',
  'user': 'user@domain.com: MYAPIKEY',
  'data': {
  'update': {},
  'fields': {
    'summary': 'tom test',
    'issuetype': {
      'id': '10031'
    },
    'project': {
      'id': '10023'
    },
    'description': {
      'type': 'doc',
      'version': 1,
      'content': [
        {
          'type': 'paragraph',
          'content': [
            {
              'text': 'This is the description of my test issue',
              'type': 'text'
            }
          ]
        }
      ]
    },
  	},
  },
});

if (response.success) {
  var text = response.responseText;
  var data = response.responseData;
}
else {
  console.log(response.statusCode);
  console.log(response.error);
}

If you get a successful response, there’s no logging, otherwise there should be logging taking place. Try logging the success responseText as well as the statusCode and error and see what that gives you.

One thing I would note from a quick look at the JIRA API and the code above is that you have what looks to be an additional space between the e-mail address/user identifier and the API key. You have a space after the colon that separates the two, whereas the JIRA API example does not.

Also, have you take a look at the JIRA Create action in the directory? That might help you. I note in particular that this action checks two return code options, which is interesting as it might suggest that response.success might not account for all 2xx codes.

Hmm, deleted the space after the colon which didn’t seem to do the trick.

Here’s the weird thing I’ve never actually figured out with Drafts scripting. How in heck to you actually see the “console” and what gets logged there? I’ve added logging to the success condition as you suggested but still not seeing the log output anywhere?

if (response.success) {
  var text = response.responseText;
  var data = response.responseData;
  console.log(response.statusCode);
  console.log(response.Text)
}
else {
  console.log(response.statusCode);
  console.log(response.error);
}

See this area in the docs.

You can also ‘alert` instead, but that does not persist.

Okay, sorry, so I guess I could have googled how to find the log. It’s in the Action Log!

So I’m at least getting something now that can help me:

failure!
[object Foundation.__NSSwiftData]
{"errorMessages":[],"errors":{"summary":"Field 'summary' cannot be set. It is not on the appropriate screen, or unknown.","description":"Field 'description' cannot be set. It is not on the appropriate screen, or unknown."}}
400

Script step completed.

The “failure” is a string I set for the fail condition. So at least I know that it’s failing and that’s the data and the response and code I’m getting back. It seems like a Jira-y issue I now have to sort out. Oh Jira.

From the API docs.

Suggests in concert with the message above that “summary” is not valid for the POST you are carrying out.

So I did a bit of digging into that error message. Jiraingly, it’s a very misleading message in that it actually turns out my authentication was incorrect. The challenge is that I’m trying to authenticate using an API token (the Action in the Drafts directory passes a username and password which I was not a fan of) and I needed to add an authentication header that’s a combination of my username+":"+apitoken encoded in base64. (@sylumer thanks by the way for making an easy way to base64 encode right from drafts :slight_smile:).

I may share this as an action on the directory once I get it working correctly, but pasting here for now as an example for anyone who might run across my same challenge. (This one’s very verbose, something that I’ll also fix once I have working.)

var http = HTTP.create(); // create HTTP object

var response = http.request({
  'url': 'https://jiraurl/rest/api/3/issue/',
  'method': 'POST',
  'data': {
  'update': {},
  'fields': {
    'summary': 'issue name',
    'issuetype': {
      'id': '10031'
    },
    'project': {
      'id': '10023'
    },
    'description': {
      'type': 'doc',
      'version': 1,
      'content': [
        {
          'type': 'paragraph',
          'content': [
            {
              'text': 'issue description',
              'type': 'text'
            }
          ]
        }
      ]
    },
  	},
  },
  'headers': {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'Basic ' + 'username:apitoken encoded in base64',
  },  
});

if (response.success) {
  var text = response.responseText;
  var data = response.responseData;
  var code = response.statusCode
  console.log(response.statusCode);
  console.log(text);
  console.log(data)
  draft.append ("success" + "\n" + "Code: " + code + "\n" + "Text: " + text + "\n" + "Data " + data);
  draft.update();
}
else {
  console.log("failure!")
  var data = response.responseData;
  var text = response.responseText;
  var code = response.statusCode
  var error = response.error
  console.log(data);
  console.log(text);
  console.log(response.statusCode);
  console.log(response.error);
  draft.append ("**Error!**" + "\n" + "**Code**: " + code + "\n" + "**Text:** " + text + "\n" + "**Data** " + data + "\n" + "**Error:**" + error);
  draft.update();
}