How to use Google Apps Script to create issues in Redmine?

1.4k views Asked by At

I'm trying to create an issue in Redmine using Google Apps Script, the code is following:

function create_issue() {
  var payload = {
    'project_id': 'helpdesk',
    'subject': 'This is a test ticket',
    'description': 'This is just a genius test ticket',
    'category_id': 1
  };
  var headers = {
    'X-Redmine-API-Key': '<myapikey>',
    'Content-Type': 'application/json'
  };
  var url = 'http://myredmine.com/issues.json';
  var options = {
    'method': 'POST',
    'headers': headers,
    'payload': payload,
    //uteHttpExceptions': true
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}

Every time I ran that script, it threw the following exception:

Execution failed: Request failed for http://myredmine.com/issues.json returned code 422. Truncated server response: {"errors":["Subject can't be blank"]} (use muteHttpExceptions option to examine full response) (line 25, file "Code") [0.645 seconds total runtime]

But as you see, the "subject" parameter was passed in the payload already. What am I missing?

Thanks,

Trinh

1

There are 1 answers

0
Trinh Nguyen On

I found the problem, I need to indicate the issue in the payload:

function create_issue() {
  var issue = {
    "description": "Test ticket", 
    "subject": "Genius ticket"
  }
  var payload = {
    "issue": issue, 
    "key": "<myapikey>", 
    "project_id": "helpdesk",
  };
  payload = JSON.stringify(payload);
  var headers = {
    'X-Redmine-API-Key': '<myapikey>',
    'Content-Type': 'application/json'
  };
  var url = 'http://myredmine.com/issues.json';
  var options = {
    'method': 'POST',
    'headers': headers,
    'payload': payload,
    'contentType': 'application/json',
    //'muteHttpExceptions': true
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}

And it works!