Webhook

WebHook 功能是Masterlab在执行某些动作和处理后,自动回调一个您设定的 http 地址。提交给http 地址的数据格式是json,具体数据 是根据具体的动作数据。

注:由于webhook支持的触发事件非常多,可能导致请求阻塞从而使整个系统性能下降,为了提高性能效率 webhook要求启用MasterlabSocket服务(建议使用swoole),详看Masterlab的安装结尾

Webhook支持的事件

Webhook 钩子的推送数据中带有丰富的 json 聚合信息,按信息的聚合单元区分,大致可分为如下几种

        $arr['事项'] = [
            Events::onIssueCreateBefore=>'创建事项之前',
            Events::onIssueCreateAfter=>'创建事项之后',
            Events::onIssueCreateChild=>'创建子任务之后',
            Events::onIssueUpdateBefore=>'更新事项之前',
            Events::onIssueUpdateAfter=>'更新事项之后',
            Events::onIssueDelete=>'删除事项',
            Events::onIssueClose=>'关闭事项',
            Events::onIssueFollow=>'关注事项',
            Events::onIssueUnFollow=>'取消关注事项',
            Events::onIssueConvertChild=>'转换子任务事项',
            Events::onIssueBatchDelete=>'批量删除事项',
            Events::onIssueBatchUpdate=>'批量更新事项',
            Events::onIssueBatchMoveProject=>'批量移动事项至新项目',
            Events::onIssueImportByExcel=>'导入事项',
            Events::onIssueRemoveChild=>'移除子任务',
            Events::onIssueUpload=>'附件上传',
            Events::onIssueMobileUpload=>'移动端附件上传',
            Events::onIssueDeleteUpload=>'删除附件',
        ];

        $arr['事项迭代'] = [
            Events::onIssueJoinSprint=>'事项加入迭代',
            Events::onIssueJoinClose=>'事项移动到关闭事项',
            Events::onIssueJoinBacklog=>'事项移动到待办事项',
        ];

        $arr['过滤器'] = [
            Events::onIssueAddAdvFilter=>'添加高级查询过滤器',
            Events::onIssueAddFilter=>'添加过滤器',
        ];

        $arr['评论'] = [
            Events::onIssueAddComment=>'添加评论',
            Events::onIssueDeleteComment=>'删除评论',
            Events::onIssueUpdateComment=>'编辑评论',
        ];

        $arr['项目'] = [
            Events::onProjectCreate=>'创建项目',
            Events::onProjectUpdate=>'编辑项目',
            Events::onProjectDelete=>'删除项目',
            Events::onProjectArchive=>'归档项目',
            Events::onProjectRecover=>'恢复项目',
        ];

        $arr['迭代管理'] = [
            Events::onSprintCreate=>'创建迭代',
            Events::onSprintUpdate=>'编辑迭代',
            Events::onSprintSetActive=>'设置迭代为进行时',
            Events::onSprintDelete=>'删除迭代',
        ];

        $arr['版本管理'] = [
            Events::onVersionCreate=>'创建版本',
            Events::onVersionUpdate=>'编辑版本',
            Events::onVersionDelete=>'删除办法',
            Events::onVersionRelease=>'发布版本',
        ];
        $arr['模块管理'] = [
            Events::onModuleCreate=>'创建模块',
            Events::onModuleUpdate=>'编辑模块',
            Events::onModuleDelete=>'删除模块',
        ];
        $arr['标签管理'] = [
            Events::onLabelCreate=>'标签模块',
            Events::onLabelUpdate=>'标签模块',
            Events::onLabelDelete=>'标签模块',
        ];
        $arr['分类管理'] = [
            Events::onCataloglCreate=>'分类模块',
            Events::onCatalogUpdate=>'分类模块',
            Events::onCatalogDelete=>'分类模块',
        ];
        $arr['项目成员'] = [
            Events::onProjectUserAdd=>'添加项目成员',
            Events::onProjectUserUpdateRoles=>'更新角色成员',
            Events::onProjectUserRemove=>'移除用户',
        ];
        $arr['项目角色'] = [
            Events::onProjectRoleAdd=>'添加项目角色',
            Events::onProjectRoleUpdate=>'编辑项目角色',
            Events::onProjectRoleRemove=>'删除项目角色',
            Events::onProjectRolePermUpdate=>'编辑角色权限',
            Events::onProjectRoleAddUser=>'角色添加用户',
            Events::onProjectRoleRemoveUser=>'角色移除用户',
        ];
        $arr['用户管理'] = [
            Events::onUserAddByAdmin=>'添加用户',
            Events::onUserUpdateByAdmin=>'编辑用户',
            Events::onUserActiveByAdmin=>'激活用户',
            Events::onUserDeleteByAdmin=>'删除用户',
            Events::onUserDisableByAdmin=>'禁用用户',
            Events::onUserBatchDisableByAdmin=>'批量禁用用户',
            Events::onUserBatchRecoveryByAdmin=>'批量恢复用户',
        ];
        $arr['用户相关'] = [
            Events::onUserRegister=>'用户注册',
            Events::onUserLogin=>'用户登录',
            Events::onUserlogout=>'用户注销',
            Events::onUserUpdateProfile=>'编辑资料',
        ];
        $arr['组织'] = [
            Events::onOrgCreate=>'创建组织',
            Events::onOrgUpdate=>'编辑组织',
            Events::onOrgDelete=>'删除组织',
        ];

Webhook创建

要创建一个新的Webhook,请按照以下步骤进行。首先要确保MasterlabSocket服务已经启动了。

  1. 以管理员身份登录,在"管理"/"Webhook"页面

  2. 输入webhook名称和提交的URL地址以及触发事件,为确保安全还可输入验证token webhook-create.png

  3. 保存后返回列表,启动webhook便可进行测试 例如提交的URL地址为 当前服务器的 /webhook.php 位于 masterlab/public/webhook.php 代码


// 用于测试webhook插件
require_once '../app/globals.php';

// 根据接收的数据进行你想要的业务操作
// ....
print_r($_POST);
// 将接收到webhook提交的数据写入到一个日志文件里,
$ret = file_put_contents(STORAGE_PATH . 'log/webhook.log', print_r($_POST['event_name'], true).print_r(json_decode($_POST['json'], true), true), FILE_APPEND);
print_r($ret);

当进行一些操作如创建项目创建事项后,便可在 storage/log/webhook.log 查看收到的调用数据

onIssueCreateAfter
Array
 (
     [summary] => 数据库每日自动备份
     [creator] => 1
     [reporter] => 1
     [created] => 1612680659
     [updated] => 1612680659
     [project_id] => 1
     [issue_type] => 3
     [priority] => 4
     [status] => 1
     [resolve] => 2
     [assignee] => 1
     [description] => 
     [module] => 1
     [environment] => 
     [sprint] => 2
     [weight] => 0
     [start_date] => 
     [due_date] => 
     [progress] => 0
     [gant_sprint_weight] => 999600000
     [id] => 161
     [project_info] => Array
         (
             [id] => 1
             [org_id] => 1
             [org_path] => default
             [name] => 示例项目
             [url] => 
             [lead] => 1
             [description] => Masterlab的示例项目
             [key] => example
             [pcounter] => 
             [default_assignee] => 1
             [assignee_type] => 
             [avatar] => project/avatar/1.png
             [category] => 0
             [type] => 0
             [type_child] => 0
             [permission_scheme_id] => 0
             [workflow_scheme_id] => 1
             [create_uid] => 1
             [create_time] => 1579247230
             [un_done_count] => 14
             [done_count] => 7
             [closed_count] => 4
             [archived] => N
             [issue_update_time] => 1583220515
             [is_display_issue_catalog] => 1
             [subsystem_json] => []
             [project_view] => issue
             [issue_view] => detail
             [issue_ui_scheme_id] => 0
             [project_tpl_id] => 1
             [default_issue_type_id] => 3
             [is_remember_last_issue] => 1
             [remember_last_issue_field] => ["issue_type","module","assignee","fix_version","labels"]
             [remember_last_issue_data] => {}
         )
 )