0

我正在尝试使用 Hubot 脚本解析 JIRA webhook。

目前,我只有一个非常简单的 Hubot 脚本来打印发布的正文:

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'

打印出正文似乎有效 - 除了某些字段(例如问题、字段、报告者被打印为[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: 'victor.hooi@foobar.com',
     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: 'victor.hooi@foobar.com',
     ...

我不完全确定为什么?

有效载荷未定义。

在 req.body 上使用 JSON.parse 似乎给了我一个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)

这是因为身体已经以某种方式被解析了吗?如果是这样,是什么解析的?如果它已经被解析,为什么有些字段仍然显示为[Object]

4

1 回答 1

1

根据您的堆栈跟踪 bodyParser 正在运行。这意味着如果交付 JSON 数据,它将在到达您之前自动解析。

这意味着 req.body 已经是您解码的 JS 对象,因此您不必再次解析它。

某些字段被标记为 [Object] 的原因是自动转储不会一直深入到结构中。在它决定停止的地方,它会显示 [Object] 而不是更深的结构。您可以使用 JSON.stringify 将其全部转储...您可以在此处查看 Jira 发送的 webhook 结构。

于 2014-10-07T19:27:55.773 回答