我正在尝试使用 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]
?