通过代码库 Webhook,允许服务端在接收到特殊事件的时候,调用指定的URL触发期望行为,本文介绍云效代码管理的 WebHooks 以及对应参数说明。
功能说明
当满足触发事件时,代码管理 Codeup 会发送一个带数据的 Post 请求到 Webhook 指定的 URL。Webhooks 可用于触发 CI 构建、执行镜像备份、触发部署到生产环境等场景。功能设置如下:
代码库管理员通过左侧菜单栏点击设置,选择WebHooks。
点击新建 WebHook 打开对话框。
填写事件接收方 URL 地址。
(可选)填写 Secret Token ,将在请求的 X-Codeup-Token HTTP头中发送,用于接收方验证请求合法性。
选择触发器,包括:推送事件、标签推送事件、评论事件、合并请求事件。
(可选)填写描述信息。
点击确定完成创建。
事件说明
推送事件
推送事件指客户端将代码 Push 到服务端,一个例外:Push 标签,不会形成推送事件。
请求头
Codeup-Event: Push Hook
请求内容
{
"object_kind": "push",
"before": "f2e2d577fab1562a6239b82721fd9827e05fdce6",
"after": "eb63d0277e64684236ebf8394b919230c4b8a286",
"ref": "refs/heads/master",
"user_id": 4,
"user_name": "Codeup",
"user_email": "codeup@aliyun.com",
"project_id": 15,
"repository": {
"name": "Diaspora",
"url": "git@codeup.aliyun.com:demo/demo.git",
"description": "",
"homepage": "https://codeup.aliyun.com/demo/demo",
"git_http_url":"https://codeup.aliyun.com/demo/demo.git",
"git_ssh_url":"git@codeup.aliyun.com:demo/demo.git",
"visibility_level":0
},
"commits": [
{
"id": "f2e2d577fab1562a6239b82721fd9827e05fdce6",
"message": "Update readme.",
"timestamp": "2019-01-01T00:08:00+08:00",
"url": "https://codeup.aliyun.com/demo/demo/commits/f2e2d577fab1562a6239b82721fd9827e05fdce6",
"author": {
"name": "Codeup",
"email": "codeup@aliyun.com"
}
},
{
"id": "eb63d0277e64684236ebf8394b919230c4b8a286",
"message": "fixed readme",
"timestamp": "2019-01-03T23:36:29+08:00",
"url": "https://codeup.aliyun.com/demo/demo/commits/eb63d0277e64684236ebf8394b919230c4b8a286",
"author": {
"name": "Codeup",
"email": "codeup@aliyun.com"
}
}
],
"total_commits_count": 2
}
标签推送事件
当创建或者删除标签时触发
请求头
Codeup-Event: Tag Push Hook
请求体
{
"object_kind": "tag_push",
"ref": "refs/tags/v1.0.0",
"before": "0000000000000000000000000000000000000000",
"after": "eb63d0277e64684236ebf8394b919230c4b8a286",
"user_id": 1,
"user_name": "Codeup",
"project_id": 1,
"repository": {
"name": "demo",
"url": "ssh://git@codeup.aliyun.com/demo/demo.git",
"description": "",
"homepage": "https://codeup.aliyun.com/demo/demo",
"git_http_url":"https://codeup.aliyun.com/demo/demo.git",
"git_ssh_url":"git@codeup.aliyun.com:demo/demo.git",
"visibility_level":0
},
"commits": [],
"total_commits_count": 0
}
评论事件
当任何一条评论生成时触发,无论是在代码提交、代码评审的评论。评论数据会存在object_attributes字段里。
提交的评论
请求头
Codeup-Event: Note Hook
请求体
{
"object_kind": "note",
"user": {
"name": "Codeup",
"username": "Codeup",
"avatar_url": ""
},
"project_id": 5,
"repository": {
"name": "demo",
"url": "https://codeup.aliyun.com/demo/demo.git",
"description": "demo.",
"homepage": "https://codeup.aliyun.com/demo/demo"
},
"object_attributes": {
"id": 1243,
"note": "This is a commit comment. How does this work?",
"noteable_type": "Commit",
"author_id": 1,
"created_at": "2019-05-17 18:08:09 UTC",
"updated_at": "2019-05-17 18:08:09 UTC",
"project_id": 5,
"attachment":null,
"line_code": "bec9703f7a456cd2b4ab5fb3220ae016e3e394e3_0_1",
"commit_id": "cfe32cf61b73a0d5e9f13e774abde7ff789b1660",
"noteable_id": null,
"system": false,
"st_diff": {
"diff": "--- /dev/null\n+++ b/six\n@@ -0,0 +1 @@\n+Subproject commit 409f37c4f05865e4fb208c771485f211a22c4c2d\n",
"new_path": "six",
"old_path": "six",
"a_mode": "0",
"b_mode": "160000",
"new_file": true,
"renamed_file": false,
"deleted_file": false
},
"url": "https://codeup.aliyun.com/demo/demo/commits/cfe32cf61b73a0d5e9f13e774abde7ff789b1660#note_1243"
},
"commit": {
"id": "cfe32cf61b73a0d5e9f13e774abde7ff789b1660",
"message": "Add submodule",
"timestamp": "2019-02-27T10:06:20+02:00",
"url": "https://codeup.aliyun.com/demo/demo/commits/cfe32cf61b73a0d5e9f13e774abde7ff789b1660",
"author": {
"name": "Demo",
"email": "demo@gmail.com"
}
}
}
合并请求的评论(旧版)
请求头
Codeup-Event: Note Hook
请求体
{
"object_kind": "note",
"user": {
"name": "Codeup",
"username": "codeup",
"avatar_url": ""
},
"project_id": 5,
"repository": {
"name": "demo",
"url": "https://codeup.aliyun.com/demo/demo/.git",
"description": "Aut reprehenderit ut est.",
"homepage": "https://codeup.aliyun.com/demo/demo"
},
"object_attributes": {
"id": 1244,
"note": "This MR needs work.",
"noteable_type": "MergeRequest",
"author_id": 1,
"created_at": "2019-05-17 18:21:36 UTC",
"updated_at": "2019-05-17 18:21:36 UTC",
"project_id": 5,
"attachment": null,
"line_code": null,
"commit_id": "",
"noteable_id": 7,
"system": false,
"st_diff": null,
"url": "https://codeup.aliyun.com/demo/demo/merge_requests/1#note_1244"
},
"merge_request": {
"id": 7,
"target_branch": "markdown",
"source_branch": "master",
"source_project_id": 5,
"author_id": 8,
"assignee_id": 28,
"title": "Tempora et eos debitis quae laborum et.",
"created_at": "2019-03-01 20:12:53 UTC",
"updated_at": "2019-03-21 18:27:27 UTC",
"milestone_id": 11,
"state": "opened",
"merge_status": "cannot_be_merged",
"target_project_id": 5,
"iid": 1,
"description": "Et voluptas corrupti assumenda temporibus. Architecto cum animi eveniet amet asperiores. Vitae numquam voluptate est natus sit et ad id.",
"position": 0,
"locked_at": null,
"source": {
"name": "demo",
"ssh_url": "git@codeup.aliyun.com:demo/demo.git",
"http_url": "https://codeup.aliyun.com/demo/demo.git",
"web_url": "https://codeup.aliyun.com/demo/demo",
"namespace": "demo",
"visibility_level": 10
},
"target": {
"name": "demo",
"ssh_url": "git@codeup.aliyun.com:demo/demo.git",
"http_url": "https://codeup.aliyun.com/demo/demo.git",
"web_url": "https://codeup.aliyun.com/demo/demo",
"namespace": "demo",
"visibility_level": 10
},
"last_commit": {
"id": "562e173be03b8ff2efb05345d12df18815438a4b",
"message": "Merge branch 'another-branch' into 'master'\n\nCheck in this test\n",
"timestamp": "2019-04-08T21: 00:25-07:00",
"url": "https://codeup.aliyun.com/demo/demo/commits/562e173be03b8ff2efb05345d12df18815438a4b",
"url": "https://codeup.aliyun.com/demo/demo/commits/562e173be03b8ff2efb05345d12df18815438a4b",
"author": {
"name": "Codeup",
"email": "codeup@aliyun.com"
}
},
"work_in_progress": false
}
}
合并请求的评论(新版)
合并请求新版自2023年8月逐步开放中,新版介绍参见合并请求能力概述。
请求头
Codeup-Event: Note Hook
请求体
{
"commit": null,
"merge_request": {
"author_aliyun_pk": "297541067872644100",
"author_id": 19271,
"biz_id": "4901786402db48aaab6450b8d12480d2",
"created_at": "2023-07-10T14:25:30+08:00",
"description": "",
"is_update_by_push": false,
"last_commit": {
"author": {
"email": "test@test.com",
"name": "test"
},
"id": "b4f8b799e9698278efb7d23bfae4fff17252eba3",
"message": "更新 2.txt",
"timestamp": "2023-06-13T10:09:53+08:00",
"url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/commits/b4f8b799e9698278efb7d23bfae4fff17252eba3"
},
"local_id": 247,
"merge_status": "cannot_be_merged",
"project_id": 609042,
"source": {
"http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
"name": "test_new_crd",
"namespace": "6369b7bb801ae2503bb96f8b",
"ssh_url": "git@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
"visibility_level": 0,
"web_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr"
},
"source_branch": "test_xxx",
"source_project_id": 609042,
"source_type": "BRANCH",
"state": "opened",
"target": {
"http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
"name": "test_new_crd",
"namespace": "6369b7bb801ae2503bb96f8b",
"ssh_url": "git@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
"visibility_level": 0,
"web_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr"
},
"target_branch": "master",
"target_project_id": 609042,
"title": "更新 2.txt",
"updated_at": "2023-07-10T14:25:30+08:00",
"url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/change/247",
"work_in_progress": false
},
"object_attributes": {
"author_aliyun_pk": "297541067872644100",
"author_id": 19271,
"closed": 0,
"commit_id": "b4f8b799e9698278efb7d23bfae4fff17252eba3",
"created_at": "2023-07-10T14:25:39+08:00",
"file_path": "1.txt",
"line_code": "9",
"note": "xaxas",
"note_biz_id": "051efb1b3aef435a968bca00e7592c32",
"note_type": "INLINE_COMMENT",
"noteable_id": "29bd1475cffd4c1cb1f17f604fc4e104",
"noteable_type": "MergeRequest",
"project_id": 609042,
"related_biz_id": "29bd1475cffd4c1cb1f17f604fc4e104",
"related_patch_set": {
"branch": "test_xxx",
"commit_id": "b4f8b799e9698278efb7d23bfae4fff17252eba3",
"create_time": "2023-07-10T14:25:30+08:00",
"mr_biz_id": "4901786402db48aaab6450b8d12480d2",
"patch_set_biz_id": "29bd1475cffd4c1cb1f17f604fc4e104",
"patch_set_name": "版本1",
"version_no": 1
},
"related_type": "CODE_REVIEW_PATCH_SET",
"state": "OPENED",
"system": false,
"updated_at": "2023-07-10T14:25:39+08:00",
"url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/change/247?comment=051efb1b3aef435a968bca00e7592c32"
},
"object_kind": "note",
"repository": {
"git_http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
"git_ssh_url": "git@codeup-proxy.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
"homepage": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr",
"name": "test_new_crd",
"url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git"
},
"user": {
"aliyun_pk": "297541067872644100",
"avatar_url": "https://tcs-devops.aliyuncs.com/thumbnail/112n76790347a42173fdca7ace673b123a59/w/100/h/100",
"extern_uid": "6369b7b14f6a9c17e247d43e",
"name": "test",
"username": "test"
},
"version": "new"
}
合并请求事件
在创建新的合并请求,或已有合并请求状态发生更新,或源分支有新的提交时将触发,事件包括:
创建合并请求
更新合并请求
通过合并请求
合并合并请求
关闭合并请求
重新打开合并请求
合并请求的事件(旧版)
请求头
Codeup-Event: Merge Request Hook
请求体
{
"object_kind": "merge_request",
"user": {
"name": "Codeup",
"username": "codeup",
"avatar_url": ""
},
"object_attributes": {
"id": 99,
"target_branch": "master",
"source_branch": "ms-viewport",
"source_project_id": 14,
"author_id": 51,
"assignee_id": 6,
"title": "MS-Viewport",
"created_at": "2019-09-03T17:23:00Z",
"updated_at": "2019-09-03T17:23:00Z",
"st_commits": null,
"st_diffs": null,
"milestone_id": null,
"state": "opened",
"merge_status": "unchecked",
"target_project_id": 14,
"iid": 1,
"description": "",
"source": {
"name": "demo",
"ssh_url": "git@codeup.aliyun.com:demo/demo.git",
"http_url": "https://codeup.aliyun.com/demo/demo.git",
"web_url": "https://codeup.aliyun.com/demo/demo",
"namespace": "demo",
"visibility_level": 10
},
"target": {
"name": "demo",
"ssh_url": "git@codeup.aliyun.com:demo/demo.git",
"http_url": "https://codeup.aliyun.com/demo/demo.git",
"web_url": "https://codeup.aliyun.com/demo/demo",
"namespace": "demo",
"visibility_level": 10
},
"last_commit": {
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme",
"timestamp": "2019-02-03T23:36:29+02:00",
"url": "https://codeup.aliyun.com/demo/demo/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": {
"name": "Codeup",
"email": "codeup@aliyun.com"
}
},
"work_in_progress": false,
"url": "https://codeup.aliyun.com/demo/demo/merge_requests/1",
"action": "open"
}
}
合并请求的事件(新版)
合并请求新版自2023年8月逐步开放中,新版介绍参见合并请求能力概述。
请求头
Codeup-Event: Merge Request Hook
请求体
{
"object_attributes": {
"action": "open",
"author_aliyun_pk": "297541067872644100",
"author_id": 19271,
"biz_id": "4901786402db48aaab6450b8d12480d2",
"created_at": "2023-07-10T14:25:30+08:00",
"description": "",
"is_update_by_push": false,
"last_commit": {
"author": {
"email": "test@test.com",
"name": "test"
},
"id": "b4f8b799e9698278efb7d23bfae4fff17252eba3",
"message": "更新 2.txt",
"timestamp": "2023-06-13T10:09:53+08:00",
"url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/commits/b4f8b799e9698278efb7d23bfae4fff17252eba3"
},
"local_id": 247,
"merge_status": "unchecked",
"project_id": 609042,
"source": {
"http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
"name": "test_new_crd",
"namespace": "6369b7bb801ae2503bb96f8b",
"ssh_url": "git@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
"visibility_level": 0,
"web_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr"
},
"source_branch": "test_xxx",
"source_project_id": 609042,
"source_type": "BRANCH",
"state": "opened",
"target": {
"http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
"name": "test_new_crd",
"namespace": "6369b7bb801ae2503bb96f8b",
"ssh_url": "git@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
"visibility_level": 0,
"web_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr"
},
"target_branch": "master",
"target_project_id": 609042,
"title": "更新 2.txt",
"updated_at": "2023-07-10T14:25:30+08:00",
"url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr/change/247",
"work_in_progress": false
},
"object_kind": "merge_request",
"repository": {
"git_http_url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git",
"git_ssh_url": "git@codeup.aliyun.com:6369b7bb801ae2503bb96f8b/test_new_cr.git",
"homepage": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr",
"name": "test_new_crd",
"url": "https://codeup.aliyun.com/6369b7bb801ae2503bb96f8b/test_new_cr.git"
},
"user": {
"aliyun_pk": "297541067872644100",
"avatar_url": "https://tcs-devops.aliyuncs.com/thumbnail/112n76790347a42173fdca7ace673b123a59/w/100/h/100",
"extern_uid": "6369b7b14f6a9c17e247d43e",
"name": "test",
"username": "test"
},
"version": "new"
}
Webhook 接收端例子
如果出于测试的目的,想看下 Webhook 的执行效果,可以用一个简单的 Echo 脚本
把下面脚本保存成 print_http_body.rb.
server = WEBrick::HTTPServer.new(:Port => ARGV.first)
server.mount_proc '/' do |req, res|
puts req.body
end
trap 'INT' do
server.shutdown
end
server.start
选个未被使用的端口(比如8000),并启动脚本:ruby print_http_body.rb 8000 然后在 Codeup 上配置 Webhook 的 URL http://my.host:8000/
在Codeup 上点击 Test Hook,可以在命令行里看到如下内容
{"before":"f2e2d577fab1562a6239b82721fd9827e05fdce6","after":"eb63d0277e64684236ebf8394b919230c4b8a286"}
aliyun.com - - [14/May/2019:11:11:11 EDT] "POST / HTTP/1.1" 200 0
- -> /
使用 HTTP Basic Authentication
当触发支持 HTTP Basic Authentication 的服务器时, 允许在 URL中添加用户名和密码进行访问,Webhook 形如:
http://userName:password@my.host:8080/project/test-job