web hook配置

Web hook

使用回调钩子,可以在当项目有新推送的代码或缺陷时自动触发URL。 在推送、缺陷、合并请求、评审事件等,可以配置项目回调钩子来监听事件。当事件发生时,工蜂 Git 会向项目回调钩子URL发送一个POST请求。 回调钩子可以被用来更新问题追踪,更新备份镜像、触发CI构建,甚至是在服务器上部署。

SSL验证

默认情况下,回调钩子端点的 SSL 证书将根据证书颁发机构的内部列表去进行验证,也就是说证书不能自签名。 您可以在项目设置 ->高级设置 ->回调钩子设置中关闭它。

推送事件

有新的推送事件到项目时,会触发。 如果是tags推送,则不会触发。
请求头:

X-Event: Issue Hook

请求体:

{
  "object_kind": "push",
  "before": "b96850262fabfa9a8a9d28fff9040621958379f9",
  "after": "e99db09904cfa385a129305e7660395f161950a",
  "ref": "refs/heads/master",
  "user_id": 11323,
  "user_name": "git_user1",
  "user_email": "git_user1@tencent.com",
  "project_id": 165245,
  "repository": {
    "name": "z-test",
    "url": "git@tencent.com:test/z-test.git",
    "description": "",
    "homepage": "http://tencent.com/test/z-test",
    "git_http_url":"http://tencent.com/test/z-test.git",
    "git_ssh_url":"git@tencent.com:test/z-test.git",
    "visibility_level":0
  },
  "commits": [
    {
      "id": "b96850262fabfa9a8a9d28fff9040621958379f9",
      "message": "new attention",
      "timestamp": "2015-03-13T09:52:25+0000",
      "url": "http://tencent.com/test/z-test/commit/b96850262fabfa9a8a9d28fff9040621958379f9",
      "author": {
        "name": "alex",
        "email": "alex@tencent.com"
      }
    },
    {
      "id": "1480a4610ca01dd10cb5bc30d8a1b1ea568c09a1",
      "message": "push",
      "timestamp": "2015-03-13T09:51:06+0000",
      "url": "http://tencent.com/test/z-test/commit/b96850262fabfa9a8a9d28fff9040621958379f9",
      "author": {
        "name": "Bob",
        "email": "Bob@tencent.com"
      }
    }
  ],
  "total_commits_count": 52
}

Tag 事件

项目中新增或删除tags时会触发.

请求头:

X-Event: Tag Push Hook

请求体:

{
  "object_kind": "tag_push",
  "ref": "refs/tags/v1.0.2",
  "before": "1480a4610ca01dd10cb5bc3359a1b1ea568c09a1",
  "after": "b96850262fabfa9a1d9d28fff9040621958379f9",
  "user_id": 11323,
  "user_name": "git_user1",
  "project_id": 11452,
  "repository": {
    "name": "z-413",
    "url": "ssh://git@tencent.com/z-413/tencent.git",
    "description": "",
    "homepage": "http://tencent.com/z-413/tencent",
    "git_http_url":"http://tencent.com/z-413/tencent.git",
    "git_ssh_url":"git@tencent.com:z-413/tencent.git",
    "visibility_level":0
  },
  "commits": [],
  "total_commits_count": 0
}

缺陷事件

创建一个缺陷或者更新、 关闭、重新打开缺陷时都会触发。

请求头:

X-Event: Issue Hook

请求体:

{
  "object_kind": "issue",
  "user": {
    "name": "git_user2",
    "username": "git_user2",
    "avatar_url": "http://blog.bobo.com.cn/s/blog_6e572cd60101qls0.html"
  },
  "repository": {
    "name": "issue",
    "url": "http://tencent.com/z-413/rename.git",
    "description": "only test",
    "homepage": "http://tencent.com/z-413/rename"
  },
  "object_attributes": {
    "id": 19,
    "title": "test",
    "assignee_id": 11323,
    "author_id": 11323,
    "project_id": 15,
    "created_at": "2015-03-13T09:52:25+0000",
    "updated_at": "2015-03-13T09:52:25+0000",
    "position": 0,
    "branch_name": null,
    "description": "null",
    "milestone_id": null,
    "state": "opened",
    "iid": 36,
    "url": "http://tencent.com/diaspora/issues/36",
    "action": "open"
  }
}

合并请求事件

新建、更新、合并、关闭或在源分支添加提交合并请求都会触发。

请求头:

X-Event: Merge Request Hook

请求体:

{
  "object_kind": "merge_request",
  "user": {
    "name": "git_user2",
    "username": "git_user2",
    "avatar_url": "http://blog.bobo.com.cn/s/blog_6e572cd60101qls0.html"
  },
  "object_attributes": {
    "id": 23,
    "target_branch": "master",
    "source_branch": "fenzhi01",
    "source_project_id": 2,
    "author_id": 3,
    "assignee_id": 4,
    "title": "agree",
    "created_at": "2018-03-13T09:51:06+0000",
    "updated_at": "2018-03-13T09:51:06+0000",
    "st_commits": null,
    "st_diffs": null,
    "milestone_id": null,
    "state": "opened",
    "merge_status": "unchecked",
    "target_project_id": 14,
    "iid": 1,
    "description": "",
    "source": {
      "name": "rename",
      "ssh_url": "ssh://git@tencent.com/z-413/rename.git",
      "http_url": "http://tencent.com/z-413/rename.git",
      "web_url": "http://tencent.com/z-413/rename",
      "visibility_level": 20,
      "namespace": "z-413"
    },
    "target": {
      "name": "rename",
      "ssh_url": "ssh://git@tencent.com/z-413/rename.git",
      "http_url": "http://tencent.com/z-413/rename.git",
      "web_url": "http://tencent.com/z-413/rename",
      "visibility_level": 20,
      "namespace": "z-413"
    },
    "last_commit": {
      "id": "1480a4610ca01dd10cb5bc30d83151eab98c09a1",
      "message": "fixed readme",
      "timestamp": "2012-01-03T23:36:29+02:00",
      "url": "http://tencent.com/z-413/rename/commits/da1480a4610ca01dd10cb5bc30d83151eab98c09a1",
      "author": {
        "name": "alex",
        "email": "alex@tencent.com"
      }
    },
    "url": "http://tencent.com/rename/merge_requests/1",
    "action": "open"
  }
}

评审事件

当创建评审或者评审状态改变时会触发。

请求头:

X-Event: Review Hook

请求体:

{
  "event": "create",
  "author": {
    "name": "git_user2",
    "username": "git_user2",
    "web_url": "http://localhost:8080/u/git_user2",
    "id": 549,
    "avatar_url": "http://localhost:8080/uploads/user/avatar/549",
    "state": "active"
  },
  "reviewers": [
    {
      "reviewer": {
        "name": "git_user1",
        "username": "git_user1",
        "web_url": "http://localhost:3000/u/git_user1",
        "id": 555,
        "avatar_url": "http://localhost:3000/uploads/user/avatar/555",
        "state": "active"
      },
      "id": 18,
      "review_id": 14,
      "user_id": 555,
      "project_id": 56321,
      "type": "invite",
      "state": "change_denied",
      "created_at": "2015-03-13T09:51:06+0000",
      "updated_at": "2015-03-13T09:51:06+0000"
    }
  ],
  "reviewer": {
      "reviewer": {
        "name": "git_user1",
        "username": "git_user1",
        "web_url": "http://localhost:3000/u/git_user1",
        "id": 555,
        "avatar_url": "http://localhost:3000/uploads/user/avatar/555",
        "state": "active"
      },
      "id": 18,
      "review_id": 14,
      "user_id": 555,
      "project_id": 56321,
      "type": "invite",
      "state": "change_denied",
      "created_at": "2015-03-13T09:51:06+0000",
      "updated_at": "2015-03-13T09:51:06+0000"
  },
  "id": 18,
  "project_id": 56321,
  "author_id": 555,
  "reviewable_id": 1298,
  "reviewable_type": "merge_request",
  "commit_id": null,
  "state": "change_denied",
  "restrict_type": "single_approve",
  "push_reset_enabled": true,
  "created_at": "2015-03-13T09:51:06+0000",
  "updated_at": "2015-03-13T09:51:06+0000"
}

单文件评审事件

当创建评审或者评审状态改变时会触发。

请求头:

X-Event: File Review Hook
X-Token: 添加 Hook 时配置的 Token

请求体:

{  
  "event":"create",
  "project_id":107077,
  "review_id":6641,
  "reviewer":{  
     "user":{  
        "name":"git_user1 ",
        "username":"git_user1",
        "web_url":"http://tencent.com/u/alexwentong",
        "id":7581,
        "avatar_url":"thumb_14-77465e9ce7a34ec1814defb2955a4dea.gif",
        "state":"http://tencent.com/uploads/user/avatar/7581/thumb_14-77465e9ce7a34ec1814defb2955a4dea.gif"
     },
     "state":"approved",
     "summary":"666aaaa"
  },
  "note":{  
     "note_id":742857,
     "noteable_id":66415,
     "noteable_type":"Review",
     "author_id":7581,
     "note":"666aaaa",
     "project_id":1070770,
     "start_object_id":"8d879314ed8767fd337c19cdd07ebcea1c793ead",
     "target_object_id":"e969ee7aa2e054e59b677755bc6e283ed3e90a45",
     "file_path":"test.md",
     "system":false,
     "review_state":"approved",
     "created_at":"2019-07-10T08:25:06+0000",
     "updated_at":"2019-07-10T08:25:06+0000"
  }
}

评论事件

对提交、合并请求或缺陷进行评论时会触发。注释数据将保存在对象属性中(例如 note, noteable_type)。有效负载还包括关于评论目标的信息,例如: 对一个缺陷的评论,会在"缺陷"键下包括缺陷的具体信息。

有效的目标类型:

  • 提交

  • 合并请求

  • 缺陷

对提交进行评论

请求头:

X-Event: Note Hook

请求体:

{
  "object_kind": "note",
  "user": {
    "name": "git_user2",
    "username": "git_user2",
    "avatar_url": "http://blog.bobo.com.cn/s/blog_6e572cd60101qls0.html"
  },
  "project_id": 229,
  "repository": {
    "name": "rename",
    "url": "http://localhost/z-413/rename.git",
    "description": "Aut reprehenderit ut est.",
    "homepage": "http://tencent.com/z-413/rename"
  },
  "object_attributes": {
    "id": 451,
    "note": "just test ",
    "noteable_type": "Commit",
    "author_id": 23,
    "created_at": "2015-03-13T09:51:06+0000",
    "updated_at": "22015-03-13T09:51:06+0000",
    "project_id": 229,
    "attachment":null,
    "line_code": "1480a4610ca01dd10cb5bc3359a1b1ea568c09a1_3_5",
    "commit_id": "b96850262fabfa9a1d9d28fff9040621958379f9",
    "noteable_id": null,
    "system": false,
    "st_diff": {
      "diff": "xxx",
      "new_path": "z-test",
      "old_path": "z-test",
      "a_mode": "0",
      "b_mode": "160000",
      "new_file": true,
      "renamed_file": false,
      "deleted_file": false
    },
    "url": "http://tencent.com/z-413/rename/commit/b96850262fabfa9a8a9d28fff9040621958379f9#note_451"
  },
  "commit": {
    "id": "b96850262fabfa9a8a9d28fff9040621958379f9",
    "message": "a message",
    "timestamp": "2015-03-13T09:51:06+0000",
    "url": "http://tencent.com/z-413/rename/commit/b96850262fabfa9a8a9d28fff9040621958379f9",
    "author": {
      "name": "alex",
      "email": "alex@tencent.com"
    }
  }
}

对合并请求进行评论

请求头:

X-Event: Note Hook

请求体:

{
  "object_kind": "note",
  "user": {
    "name": "git_user2",
    "username": "git_user2",
    "avatar_url": "http://blog.bobo.com.cn/s/blog_6e572cd60101qls0.html"
  },
  "project_id": 229,
  "repository": {
    "name": "rename",
    "url": "http://tencent.com/z-413/rename.git",
    "description": "xx",
    "homepage": "http://tencent.com/z-413/rename"
  },
  "object_attributes": {
    "id": 294,
    "note": "back",
    "noteable_type": "MergeRequest",
    "author_id": 29,
    "created_at": "2015-03-13T09:52:25+0000",
    "updated_at": "2015-03-13T09:52:25+0000",
    "project_id": 14,
    "attachment": null,
    "line_code": null,
    "commit_id": "",
    "noteable_id": 33,
    "system": false,
    "st_diff": null,
    "url": "http://tencent.com/z-413/rename/merge_requests/6#note_33"
  },
  "merge_request": {
    "id": 2,
    "target_branch": "fenzhi01",
    "source_branch": "master",
    "source_project_id":14,
    "author_id": 11322,
    "assignee_id": 33,
    "title": "xx",
    "created_at": "2015-03-13T09:52:25+0000",
    "updated_at": "2015-03-13T09:52:25+0000",
    "milestone_id": 23,
    "state": "opened",
    "merge_status": "go",
    "target_project_id": 14,
    "iid": 3,
    "description": "Revision, not careful.",
    "position": 0,
    "locked_at": null,
    "source": {
      "name": "rename",
      "ssh_url": "git@tencent.com:z-413/rename.git",
      "http_url": "http://tencent.com:z-413/rename.git",
      "web_url": "http://tencent.com:z-413/rename",
      "namespace": "z-413",
      "visibility_level": 10
    },
    "target": {
      "name": "rename",
      "ssh_url": "git@tencent.com:z-413/rename.git",
      "http_url": "http://tencent.com:z-413/rename.git",
      "web_url": "http://tencent.com:z-413/rename",
      "namespace": "z-413",
      "visibility_level": 10
    },
    "last_commit": {
      "id": "1480a4610ca01dd10cb5bc30d8a1b1eab98c09a1",
      "message": "xxx\n",
      "timestamp": "2015-03-15T09:51:06+0000",
      "url": "http://tencent.com:z-413/rename/commit/1480a4610ca01dd10cb5bc30d8a1b1eab98c09a1",
      "author": {
        "name": "git_user2",
        "email": "git_user2@tencent.com"
      }
    }
  }
}

对缺陷进行评论

请求头:

X-Event: Note Hook

请求体:

{
  "object_kind": "note",
  "user": {
    "name": "git_user2",
    "username": "git_user2",
    "avatar_url": "http://blog.bobo.com.cn/s/blog_6e572cd60101qls0.html"
  },
  "project_id": 229,
  "repository": {
    "name": "rename",
    "url": "http://tencent.com:z-413/rename.git",
    "description": "is usually",
    "homepage": "http://tencent.com:z-413/rename.git"
  },
  "object_attributes": {
    "id": 1239,
    "note": "bug",
    "noteable_type": "Issue",
    "author_id": 23,
    "created_at": "2018-03-13T09:51:06+0000",
    "updated_at": "2018-03-13T09:51:06+0000",
    "project_id": 229,
    "attachment": null,
    "line_code": null,
    "commit_id": "",
    "noteable_id": 23,
    "system": false,
    "st_diff": null,
    "url": "http://tencent.com:z-413/rename/issues/23#note_229"
  },
  "issue": {
    "id": 8,
    "title": "test",
    "assignee_id": null,
    "author_id": 11323,
    "project_id": 229,
    "created_at": "2018-03-13T09:51:06+0000",
    "updated_at": "2018-03-13T09:51:06+0000",
    "position": 0,
    "branch_name": null,
    "description": "test",
    "milestone_id": null,
    "state": "closed",
    "iid": 66
  }
}

web hook 接收者举例

可以在控制台会话中运行的简单的echo脚本进行测试,然后将文件保存为"print_http_body.rb"。

require 'webrick'

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. 然后在工蜂git 中添加服务器http://my.host:8000/作为webhook接收器。

在 工蜂Git 中点击"Test Hook"时,正常情况下,在控制台中看到类似下面的内容。

{"before":"077a85dd266e6f3573ef7e9ef8ce3343ad659c4e","after":"95cd4a99e93bc4bbabacfa2cd10e6725b1403c60",<SNIP>}
example.com - - [14/May/2014:07:45:26 EDT] "POST / HTTP/1.1" 200 0