victorhooi
victorhooi

Reputation: 17275

Parsing JIRA webhooks with Hubot

I'm attempting to parse a JIRA webhook using a Hubot script.

At the moment, I just have a very simple Hubot script to print out the posted body:

module.exports = (robot) ->
  robot.router.post '/jirawebhooks/foo-tickets', (req, res) ->
    console.dir("#{new Date()} jira webhook post_received")
    console.dir(req.body)
    console.dir(req.body.payload)
    console.dir(JSON.parse req.body)

    res.send 'OK'

Printing out the body seems to work - except that certain fields (e.g. isssue, fields, reporter are printed out as [Object]:

'Wed Oct 01 2014 11:35:36 GMT+0000 (UTC) jira webhook post_received'
{ webhookEvent: 'jira:issue_updated',
  timestamp: 1412163338429,
  user:
   { self: 'https://jira.foobar.org/rest/api/2/user?username=victor.hooi',
     name: 'victor.hooi',
     emailAddress: '[email protected]',
     avatarUrls:
      { '16x16': 'https://jira.foobar.org/secure/useravatar?size=xsmall&ownerId=victor.hooi&avatarId=13400',
        '24x24': 'https://jira.foobar.org/secure/useravatar?size=small&ownerId=victor.hooi&avatarId=13400',
        '32x32': 'https://jira.foobar.org/secure/useravatar?size=medium&ownerId=victor.hooi&avatarId=13400',
        '48x48': 'https://jira.foobar.org/secure/useravatar?ownerId=victor.hooi&avatarId=13400' },
     displayName: 'Victor Hooi',
     active: true },
  issue:
   { id: '161211',
     self: 'https://jira.foobar.org/rest/api/2/issue/161211',
     key: 'BA-15424',
     fields:
      { summary: 'THIS IS A TEST TICKET',
        issuetype: [Object],
        customfield_10857: null,
        reporter: [Object],
        customfield_10041: [Object],
        created: '2014-09-30T22:01:08.000+0000',
        updated: '2014-10-01T10:45:21.000+0000',
        description: 'This is just a test ticket - please ignore.\r\n\r\nhttps://www.youtube.com/watch?v=dQw4w9WgXcQ',
        priority: [Object],
        customfield_10558: '139180',
        customfield_10557: null,
        issuelinks: [],
        customfield_10559: null,
        subtasks: [],
        status: [Object],
        labels: [Object],
        workratio: -1,
        customfield_11151: 'Wed Oct 01 03:15:53 UTC 2014',
        customfield_11050: [Object],
        project: [Object],
        customfield_10057: 'true',
        environment: null,
        customfield_10056: 'victor.hooi(victor.hooi)',
        customfield_10055: null,
        customfield_10053: '3_*:*_2_*:*_486000_*|*_1_*:*_2_*:*_15205000_*|*_10006_*:*_2_*:*_37000_*|*_6_*:*_1_*:*_3566000_*|*_5_*:*_2_*:*_15000_*|*_4_*:*_1_*:*_37000',
        customfield_10052: '29745',
        customfield_10051: [Object],
        customfield_10050: '3.0',
        lastViewed: '2014-10-01T11:35:33.070+0000',
        components: [],
        comment: [Object],
        votes: [Object],
        customfield_11453: null,
        resolution: [Object],
        customfield_11450: null,
        resolutiondate: '2014-10-01T03:23:34.000+0000',
        customfield_11452: null,
        customfield_11451: null,
        duedate: null,
        watches: [Object],
        customfield_10552: null,
        customfield_10551: null,
        assignee: null,
        customfield_10554: null,
        customfield_10553: null,
        attachment: [],
        customfield_10550: null,
        versions: [],
        customfield_10030: [Object],
        customfield_10031: null } },
  comment:
   { self: 'https://jira.foobar.org/rest/api/2/issue/161211/comment/731107',
     id: '731107',
     author:
      { self: 'https://jira.foobar.org/rest/api/2/user?username=victor.hooi',
        name: 'victor.hooi',
        emailAddress: '[email protected]',
     ...

I'm not entirely sure why?

The payload isn't defined.

And using JSON.parse on the req.body seems to give me an Unexpected token o:

SyntaxError: Unexpected token o
  at Object.parse (native)
  at /home/ubuntu/mongodb-glados/scripts/test_http_listener.coffee:6:12, <js>:7:24
  at callbacks (/home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/lib/router/index.js:161:37)
  at param (/home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/lib/router/index.js:135:11)
  at pass (/home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/lib/router/index.js:142:5)
  at Router._dispatch (/home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/lib/router/index.js:170:5)
  at Object.router (/home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/lib/router/index.js:33:10)
  at next (/home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/node_modules/connect/lib/proto.js:190:15)
  at multipart (/home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/node_modules/connect/lib/middleware/multipart.js:60:27)
  at /home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:57:9
  at urlencoded (/home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:48:27)
  at /home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:55:7
  at IncomingMessage.<anonymous> (/home/ubuntu/mongodb-glados/node_modules/hubot/node_modules/express/node_modules/connect/lib/middleware/json.js:82:9)
  at IncomingMessage.emit (events.js:92:17)
  at _stream_readable.js:943:16
  at process._tickCallback (node.js:419:13)

Is this because the body is somehow already parsed? If so, what parsed it? And if it is already parsed, why are some fields still showing up as [Object]?

Upvotes: 0

Views: 1195

Answers (1)

JayKuri
JayKuri

Reputation: 849

According to your stack trace bodyParser is running. This means that if JSON data is delivered, it will be auto-parsed before it gets to you.

Which means that req.body is already your decoded JS object, so you don't have to parse it again.

The reason some of the fields are marked as [Object] is that the automatic dumping doesn't go all the way down into the structure. Where it decides to stop, it shows [Object] instead of the deeper structure. You can use JSON.stringify to dump it out in its entirety... You can see the webhook structure Jira sends here.

Upvotes: 1

Related Questions