公司开始用钉钉了,想把redmine项目管理退送到钉钉里,
不想专门写脚本拉token调api发信息,
想使用的钉钉自定义机器人,百度了下还真有人已经做好了
redmine2.4 -> 3.x 插件 https://github.com/phanan/redmine_webhook
redmine4.x 插件 https://github.com/suer/redmine_webhook
下好插件,索性写几个脚本,省的每次打命令
插件安装
#!/bin/bash
echo "bundle install"
bundle install
echo "install"
rake redmine:plugins:migrate RAILS_ENV=production
启动
#!/bin/bash
echo "start redmine"
bundle exec rails server webrick -e production -d
关闭
echo "find redmine running pross: "
pros=`ps -ef |grep '/ruby bin/rails server webrick -e production -d'|grep -v grep`
echo $pros
prosid=`ps -ef |grep '/ruby bin/rails server webrick -e production -d'|grep -v grep|awk -F " " '{print $2}'`
echo "3s后关闭" $prosid
sleep 3
kill -9 $prosid
装好插件,启动
随便test了个一项目,结果居然没报警
发现插件目录lib/redmine_webhook/webhook_listener.rb
里的post方法存在问题,可能是作者留的扩展点,方便用到其他地方
post 的json是
{
"payload": {
"action": "updated",
"issue": {
"id": 11,
"subject": "test",
"description": "test.x.x.x.x.dingtalk",
"created_on": "2020-03-05T07:13:40.000Z",
"updated_on": "2020-03-10T07:45:37.000Z",
"closed_on": null,
"root_id": 11,
"parent_id": null,
"done_ratio": 100,
"start_date": "2020-03-05",
"due_date": "2020-03-06",
"estimated_hours": 1.0,
"is_private": false,
"lock_version": 6,
"custom_field_values": [],
"project": {
"id": 10,
"identifier": "hx005",
"name": "开发与生产",
"description": "这是一个演示demo",
"created_on": "2020-03-04T09:48:06.000Z",
"homepage": ""
},
"status": {
"id": 1,
"name": "新建"
},
"tracker": {
"id": 18,
"name": "15量产"
},
"priority": {
"id": 2,
"name": "普通"
},
"author": {
"id": 106,
"login": "tokyohuang123",
"mail": "[email protected]",
"firstname": "tokyohuang123",
"lastname": "tokyohuang123",
"identity_url": null,
"icon_url": "https://www.gravatar.com/avatar/80588af530c40b28a84f123f1842548b?rating=PG&size=24"
},
"assignee": {
"id": 106,
"login": "tokyohuang123",
"mail": "[email protected]",
"firstname": "tokyohuang123",
"lastname": "tokyohuang123",
"identity_url": null,
"icon_url": "https://www.gravatar.com/avatar/80588af530c40b28a84f123f1842548b?rating=PG&size=24"
},
"watchers": [{
"id": 106,
"login": "tokyohuang123",
"mail": "[email protected]",
"firstname": "tokyohuang123",
"lastname": "tokyohuang123",
"identity_url": null,
"icon_url": "https://www.gravatar.com/avatar/80588af530c40b28a84f123f1842548b?rating=PG&size=24"
}]
},
"journal": {
"id": 23,
"notes": "",
"created_on": "2020-03-10T07:45:37.000Z",
"private_notes": false,
"author": {
"id": 106,
"login": "tokyohuang123",
"mail": "[email protected]",
"firstname": "tokyohuang123",
"lastname": "tokyohuang123",
"identity_url": null,
"icon_url": "https://www.gravatar.com/avatar/80588af530c40b28a84f123f1842548b?rating=PG&size=24"
},
"details": [{
"id": 34,
"value": "test.x.x.x.x.dingtalk",
"old_value": "test",
"prop_key": "description",
"property": "attr"
}, {
"id": 35,
"value": "2",
"old_value": "1",
"prop_key": "priority_id",
"property": "attr"
}]
},
"url": "http://x.x.x.x:30002/issues/11"
}
}
而钉钉机器人的json格式要求
msgcont ={
"msgtype"=> "text",
"text"=> {
"content"=>"#{content}"
}
}
所以这里body的内容需要改下
def post(webhooks, request_body)
Thread.start do
webhooks.each do |webhook|
begin
Faraday.post do |req|
req.url webhook.url
req.headers['Content-Type'] = 'application/json'
req.body = request_body
end
rescue => e
Rails.logger.error e
end
end
end
end
为了方便测试 先用text,后面再用MD
def post(webhooks, request_body)
Thread.start do
webhooks.each do |webhook|
begin
Faraday.post do |req|
req.url webhook.url
req.headers['Content-Type'] = 'application/json'
req.body = {
"msgtype"=> "text",
"text"=> {
"content"=>"#{request_body}"
}
}.to_json
end
rescue => e
Rails.logger.error e
end
end
end
end
```
马上试了下 成了,
不懂作者为啥在这里留个坑,国外也没用钉钉啊