我正在尝试正确解析JIRA webhook注释部分。现在,以下代码可很好地用于第一个注释:
data = request.json
jira_comment = data['issue']['fields']['comment']['comments'][0].get('body')
但是,例如,每次触发新的webhook时,都会添加新注释,我只能在JSON消息中收到第一个注释。如果我能以某种方式识别每个评论中的每个评论,或者仅解析该问题的最后一个评论。最好的方法是什么,或者有可能,因为JIRA webhook JSON看起来坏了(多个“ body”键)。
请参阅下面的JSON数据,使其更整洁,请使用以下http://jsbeautifier.org/:
{
“时间戳”:1443024903340,
“ webhookEvent”:“ jira:issue_updated”,
“用户”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/user?username=jhdoe”,
“ name”:“ jhdoe”,
“ key”:“ jhdoe”,
“ emailAddress”:“ [email protected]”,
“ avatarUrls”:{
“ 48x48”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48”,
“ 24x24”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24”,
“ 16x16”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16”,
“ 32x32”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32”
},
“ displayName”:“ John Doe”,
“有效”:是,
“ timeZone”:“欧洲/柏林”
},
“问题”: {
“ id”:“ 10300”,
“ self”:“ http://192.168.10.58:8080/rest/api/2/issue/10300”,
“ key”:“ DEMO-6”,
“字段”:{
“ issuetype”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/issuetype/3”,
“ id”:“ 3”,
“ description”:“需要完成的任务。”,
“ iconUrl”:“ http://192.168.10.58:8080/images/icons/issuetypes/task.png”,
“ name”:“任务”,
“子任务”:false
},
“组件”: [],
“耗时”:null,
“ timeoriginalestimate”:null,
“ description”:“嘿”,
“项目”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/project/10000”,
“ id”:“ 10000”,
“ key”:“ DEMO”,
“ name”:“ DEMO”,
“ avatarUrls”:{
“ 48x48”:“ http://192.168.10.58:8080/secure/projectavatar?avatarId=10011”,
“ 24x24”:“ http://192.168.10.58:8080/secure/projectavatar?size=small&avatarId=10011”,
“ 16x16”:“ http://192.168.10.58:8080/secure/projectavatar?size=xsmall&avatarId=10011”,
“ 32x32”:“ http://192.168.10.58:8080/secure/projectavatar?size=medium&avatarId=10011”
}
},
“ fixVersions”:[],
“ aggregatetimespent”:null,
“分辨率”:null,
“时间跟踪”: {},
“ customfield_10006”:null,
“附件”: [],
“ customfield_10009”:“ 0 | i0000f:”,
“ aggregatetimeestimate”:null,
“ resolutiondate”:null,
“ workratio”:-1,
“ summary”:“评论测试”,
“ lastViewed”:“ 2015-09-23T19:14:11.979 + 0300”,
“手表”: {
“ self”:“ http://192.168.10.58:8080/rest/api/2/issue/DEMO-6/watchers”,
“ watchCount”:1
“ isWatching”:是
},
“创建者”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/user?username=jhdoe”,
“ name”:“ jhdoe”,
“ key”:“ jhdoe”,
“ emailAddress”:“ [email protected]”,
“ avatarUrls”:{
“ 48x48”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48”,
“ 24x24”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24”,
“ 16x16”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16”,
“ 32x32”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32”
},
“ displayName”:“ John Doe”,
“有效”:是,
“ timeZone”:“欧洲/柏林”
},
“子任务”:[],
“ created”:“ 2015-09-21T17:39:14.518 + 0300”,
“记者”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/user?username=jhdoe”,
“ name”:“ jhdoe”,
“ key”:“ jhdoe”,
“ emailAddress”:“ [email protected]”,
“ avatarUrls”:{
“ 48x48”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48”,
“ 24x24”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24”,
“ 16x16”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16”,
“ 32x32”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32”
},
“ displayName”:“ John Doe”,
“有效”:是,
“ timeZone”:“欧洲/柏林”
},
“ customfield_10000”:null,
“ aggregateprogress”:{
“进度”:0,
“总计”:0
},
“优先”: {
“ self”:“ http://192.168.10.58:8080/rest/api/2/priority/2”,
“ iconUrl”:“ http://192.168.10.58:8080/images/icons/priorities/high.png”,
“ name”:“ High”,
“ id”:“ 2”
},
“ customfield_10001”:null,
“ customfield_10100”:“ 1234”,
“ customfield_10200”:null,
“标签”: [],
“ customfield_10004”:null,
“环境”:null,
“ timeestimate”:null,
“ aggregatetimeoriginalestimate”:null,
“版本”:[],
“ duedate”:null,
“进度”:{
“进度”:0,
“总计”:0
},
“评论”:{
“ startAt”:0,
“ maxResults”:3,
“总计”:3,
“注释”: [{
“ self”:“ http://192.168.10.58:8080/rest/api/2/issue/10300/comment/10600”,
“ id”:“ 10600”,
“作者”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/user?username=jhdoe”,
“ name”:“ jhdoe”,
“ key”:“ jhdoe”,
“ emailAddress”:“ [email protected]”,
“ avatarUrls”:{
“ 48x48”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48”,
“ 24x24”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24”,
“ 16x16”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16”,
“ 32x32”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32”
},
“ displayName”:“ John Doe”,
“有效”:是,
“ timeZone”:“欧洲/柏林”
},
“ body”:“评论1”,
“ updateAuthor”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/user?username=jhdoe”,
“ name”:“ jhdoe”,
“ key”:“ jhdoe”,
“ emailAddress”:“ [email protected]”,
“ avatarUrls”:{
“ 48x48”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48”,
“ 24x24”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24”,
“ 16x16”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16”,
“ 32x32”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32”
},
“ displayName”:“ John Doe”,
“有效”:是,
“ timeZone”:“欧洲/柏林”
},
“ created”:“ 2015-09-23T19:05:26.593 + 0300”,
“已更新”:“ 2015-09-23T19:08:07.010 + 0300”
},{
“ self”:“ http://192.168.10.58:8080/rest/api/2/issue/10300/comment/10601”,
“ id”:“ 10601”,
“作者”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/user?username=jhdoe”,
“ name”:“ jhdoe”,
“ key”:“ jhdoe”,
“ emailAddress”:“ [email protected]”,
“ avatarUrls”:{
“ 48x48”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48”,
“ 24x24”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24”,
“ 16x16”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16”,
“ 32x32”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32”
},
“ displayName”:“ John Doe”,
“有效”:是,
“ timeZone”:“欧洲/柏林”
},
“ body”:“ Comment 2”,
“ updateAuthor”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/user?username=jhdoe”,
“ name”:“ jhdoe”,
“ key”:“ jhdoe”,
“ emailAddress”:“ [email protected]”,
“ avatarUrls”:{
“ 48x48”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48”,
“ 24x24”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24”,
“ 16x16”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16”,
“ 32x32”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32”
},
“ displayName”:“ John Doe”,
“有效”:是,
“ timeZone”:“欧洲/柏林”
},
“ created”:“ 2015-09-23T19:08:13.644 + 0300”,
“已更新”:“ 2015-09-23T19:08:13.644 + 0300”
},{
“ self”:“ http://192.168.10.58:8080/rest/api/2/issue/10300/comment/10606”,
“ id”:“ 10606”,
“作者”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/user?username=jhdoe”,
“ name”:“ jhdoe”,
“ key”:“ jhdoe”,
“ emailAddress”:“ [email protected]”,
“ avatarUrls”:{
“ 48x48”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48”,
“ 24x24”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24”,
“ 16x16”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16”,
“ 32x32”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32”
},
“ displayName”:“ John Doe”,
“有效”:是,
“ timeZone”:“欧洲/柏林”
},
“ body”:“评论3”,
“ updateAuthor”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/user?username=jhdoe”,
“ name”:“ jhdoe”,
“ key”:“ jhdoe”,
“ emailAddress”:“ [email protected]”,
“ avatarUrls”:{
“ 48x48”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=48”,
“ 24x24”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=24”,
“ 16x16”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=16”,
“ 32x32”:“ http://www.gravatar.com/avatar/f94b6b6c15d155f4d4b3b2c05509ab0a?d=mm&s=32”
},
“ displayName”:“ John Doe”,
“有效”:是,
“ timeZone”:“欧洲/柏林”
},
“ created”:“ 2015-09-23T19:13:24.836 + 0300”,
“已更新”:“ 2015-09-23T19:14:04.464 + 0300”
}]
},
“问题链接”:[],
“投票”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/issue/DEMO-6/votes”,
“票数”:0,
“已投票”:false
},
“工作日志”:{
“ startAt”:0,
“ maxResults”:20,
“总计”:0,
“工作日志”:[]
},
“代理人”:null,
“ updated”:“ 2015-09-23T19:15:03.338 + 0300”,
“状态”: {
“ self”:“ http://192.168.10.58:8080/rest/api/2/status/10000”,
“ description”:“”,
“ iconUrl”:“ http://192.168.10.58:8080/images/icons/subtask.gif”,
“ name”:“待办事项”,
“ id”:“ 10000”,
“ statusCategory”:{
“ self”:“ http://192.168.10.58:8080/rest/api/2/statuscategory/2”,
“ id”:2
“ key”:“ new”,
“ colorName”:“蓝灰色”,
“ name”:“待办事项”
}
}
}
},
“变更日志”:{
“ id”:“ 10611”,
“项目”:[{
“ field”:“自定义ID”,
“ fieldtype”:“ custom”,
“ from”:null,
“ fromString”:null,
“至”:null,
“ toString”:“ 1234”
}]
}
}
最佳答案
好吧,您在json中的注释部分是字典列表,因此您始终可以获取列表中的最后一个,然后调用.get('body')
jira_comment = data['issue']['fields']['comment']['comments'][-1].get('body')
之所以有效,是因为在python中,将数组索引中的负数视为在数组中向后移动。因此-1为您提供了数组中的最后一项。
根本没有注释时,您的代码可以工作吗?
如果不考虑检查comments数组的长度是否大于0或将其包装在try try块中。
关于python - 解析JIRA webhook注释,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32745086/