0

I created a function in CoffeeScript that calls the Jira REST API, instead of returning the complete json object, I only want to return a single sprint id value.

But it always returns the Object??

This is the function:

jiraGetActiveSprintId = (res) ->
  options =
    rejectUnauthorized: false
  returnMsg = -1
  auth = 'Basic ' + new Buffer("user" + ':' + "passw").toString('base64');
  robot.http("http://jira.rabobank.nl/rest/greenhopper/1.0/integration/teamcalendars/sprint/list?jql=project+%3D+TEAMNAME+and+Sprint+not+in+closedSprints()", options)
    .headers(Authorization: auth, Accept: 'application/json')
    .get() (err, response, body) ->
      if err
        -2
      if response.statusCode is 404
        -3
      if response.statusCode is 200
        data = JSON.parse body
        ab=data.sprints[0].id
        return ab

This is how I call the function:

sprId = jiraGetActiveSprintId(res)
console.log('sprId:' + sprId)

But this is wat the console.log shows:

sprId:[object Object]

Why is it not returning the variable ab?

If I log the returning object I get:

sprId: ScopedClient {
  options:
   { protocol: 'http:',
     slashes: true,
     auth: null,
     host: 'jira.bla.nl',
     port: null,
     hostname: 'jira.bla.nl',
     hash: null,
     query: { jql: 'project = TEAM and Sprint not in closedSprints()' },
     pathname: '/rest/greenhopper/1.0/integration/teamcalendars/sprint/list',
     path: '/rest/greenhopper/1.0/integration/teamcalendars/sprint/list?jql=project+%3D+TEAM+and+Sprint+not+in+closedSprints()',
     rejectUnauthorized: false,
     headers:
      { 'User-Agent': 'Hubot/2.18.0',
        Authorization: 'Basic ASBC123478WxCb29tMQ==',
        Accept: 'application/json' },
        encoding: 'utf-8' },
  passthroughOptions: { rejectUnauthorized: false } }

Okay, I found a workaround, I understand now that because of the async nature of the call, you get back a promise. So I changed the code to call another method when the data (sprintId) is available instead. Not sure if this is the best solution, but at least it works.

jiraGetActiveSprintId = (res, functionToContinueWith) ->
  options =
    rejectUnauthorized: false
  returnMsg = -1
  auth = 'Basic ' + new Buffer('user' + ':' + 'password').toString('base64');
  robot.http("http://jira.rabobank.nl/rest/greenhopper/1.0/integration/teamcalendars/sprint/list?jql=project+%3D+TEAM+and+Sprint+not+in+closedSprints()", options)
    .headers(Authorization: auth, Accept: 'application/json')
    .get() (err, response, body) ->
      if err
        id=-2
       console.log "STATUS=" + response.statusCode
      if response.statusCode is 200
        data = JSON.parse body
        functionToContinueWith(res, data.sprints[0].id)
      if response.statusCode is 404
        id=-3


continueJiraGetActiveSprintIssues = (res, sprintId) ->
  console.log('sprintId:', sprintId)

jiraGetActiveSprintIssues = (res, issueType) ->
  jiraGetActiveSprintId(res, continueJiraGetActiveSprintIssues)
tazzer
  • 21
  • 3

1 Answers1

0

Your function does not return a id. It return a promise object.

Try some like this:

sprId = jiraGetActiveSprintId(res)
sprId.then (id) ->
  console.log('sprId:' + id)
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
  • promise objects are new to me, I read some ifo about it. But I am still struggling to get it working. I tried your code, It returns: ERROR TypeError: sprId.then is not a function – tazzer Jul 04 '16 at 05:23
  • My fault. ScopedClient does not return a promise. You must convert calback to promise. Look at this question: http://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises – Tomasz Jakub Rup Jul 04 '16 at 08:16