Skip to content

system ​

本文档介绍如何在应用中订阅系统事件。当用户在 PingCode 产品中执行操作时,系统会生成 PingCode 产品事件,如创建工作项,应用可以配置订阅并处理这些事件。

配置 ​

manifest.yml 文件配置示例:

yaml
event:
  triggers:
    - key: system_trigger
      type: system
      events:
        - pce:ship:idea:created
        - pce:ship:idea:updated
      handler: 
        function: ship-idea-handler
      filter:
        ignoreSelf: true

处理函数 ​

当事件触发时,会被你定义的处理函数接收到。

示例 ​

javascript
import type { SystemEventHandler } from "@pc-nexus/event";

export const handler: SystemEventHandler = async (context, event) => {
    console.log(JSON.stringify(context));
    console.log(JSON.stringify(event));
};

参数 ​

处理函数接收 context 和 event 两个参数。

context ​

context 为 NexusAppContext 类型,包含应用运行时的上下文信息,详情请参考 app ,获取触发的事件信息可以从 context 中获取 如下:

属性描述
event.trigger.key触发器在 manifest 中定义的 key
event.trigger.type触发器类型,值为 system

event ​

event 是订阅事件所传递的数据,其类型定义如下:

typescript
export interface EventHandlerEvent {
    event_type: string;
    self_generated: boolean;
    payload: {
        data: PrincipalInfo;
        changelog?: {
            origin?: originValue;
            target?: targetValue;
            property?: PropertyInfo;
        };
        source?: SystemEventSource;
    };
}
名称描述
event_type订阅事件的类型,例如 pce:pjm:workitem:created
self_generated该事件是否为 Nexus App 的操作触发的,如调用 REST API 创建或者更新工作项;当 Manifest 中对应的 filter.ignoreSelf 为 true 且 self_generated = true 的事件不会被触发,反之会被触发。
payload订阅事件所传递的数据

payload 中包含:

名称描述
data事件主体信息,结构与获取主体信息的 REST API 返回的结构相同; 关于辨别主体的几个例子: - pce:pjm:workitem:created 事件主体为 workitem; - pce:pjm:workitem:link:added 事件主体为 workitem; - pce:pjm:sprint:created 事件主体为 sprint;
source表明了触发事件的源头,默认为空; 例如当时系统自动操作导致产生的事件时,该字段值是 system
changelog变更日志,通常在事件类型是 updated、added 和 removed 时携带此字段
changelog.origin变更时的原始值; - 当事件类型是 updated 时,该字段就是变更前的值; - 当事件类型是 removed 时,该字段就是移除的目标对象;
changelog.target变更时的目标值; - 当事件类型是 updated 时,该字段就是变更后的值; - 当事件类型是 added 时,该字段就是增加的目标对象;
changelog.property变更的属性,通常在事件类型是 updated 时携带此字段,详细结构可参考获取属性信息的 REST API

数据示例 ​

以事件 pce:pjm:workitem:created 为例,当事件触发时完整的 event 参数如下:

javascript
{
    "event_type": "pce:pjm:workitem:created",
    "self_generated": false,  
    "payload": {
        "data": {
            "id": "6a06d7954cd222a3dad91d45",
            "url": "https://open.pingcode.com/v1/project/work_items/6a06d7954cd222a3dad91d45",
            "project": {
                "id": "68c24404b2ae15e123c9e60c",
                "url": "https://open.pingcode.com/v1/project/projects/68c24404b2ae15e123c9e60c",
                "name": "test-hybrid",
                "type": "hybrid",
                "identifier": "H20020",
                "is_archived": 0,
                "is_deleted": 0
            },
            "identifier": "H20020-59",
            "title": "TEST",
            "type": "story",
            "start_at": null,
            "end_at": null,
            "parent_id": null,
            "short_id": "JoP34lQM",
            "html_url": "https://xxx.pingcode.com/pjm/workitems/JoP34lQM",
            "parent": null,
            "assignee": null,
            "state": {
                "id": "62d8f23c461c1ccf22123314",
                "url": "https://open.pingcode.com/v1/project/work_item_states/62d8f23c461c1ccf22123314",
                "name": "打开",
                "type": "pending",
                "color": "#56ABFB"
            },
            "priority": null,
            "board": {
                "id": "68c24404b2ae15e123c9e63b",
                "url": "https://open.pingcode.com/v1/project/projects/68c24404b2ae15e123c9e60c/boards/68c24404b2ae15e123c9e63b",
                "name": "默认看板",
                "work_item_types": [
                    "62d8f240461c1ccf221235d7",
                    "646b58929f3ece3cfda66c8c",
                    "636ca7d543350c333e49c3d0",
                    "62d8f23c461c1ccf22123309",
                    "636ca7f643350c333e49c3e1"
                ]
            },
            "entry": {
                "id": "68c24404b2ae15e123c9e635",
                "url": "https://open.pingcode.com/v1/project/projects/68c24404b2ae15e123c9e60c/boards/68c24404b2ae15e123c9e63b/entries/68c24404b2ae15e123c9e635",
                "name": "需求池"
            },
            "swimlane": {
                "id": "68c24404b2ae15e123c9e63a",
                "url": "https://open.pingcode.com/v1/project/projects/68c24404b2ae15e123c9e60c/boards/68c24404b2ae15e123c9e63b/swimlanes/68c24404b2ae15e123c9e63a",
                "name": "默认泳道"
            },
            "version": null,
            "versions": null,
            "sprint": null,
            "phase": null,
            "story_points": null,
            "estimated_workload": null,
            "remaining_workload": null,
            "description": null,
            "completed_at": null,
            "properties": {
                "risk": null,
                "backlog_type": null,
                "backlog_from": null,
                "schedule_mode": null,
                "dangechengyuan": null,
                "duogechengyuan": null,
                "workload": {
                    "reported_total": 0,
                    "remaining": 0,
                    "estimated": {
                        "duration": 0,
                        "estimated_at": 1778833301,
                        "estimated_by": null
                    }
                },
                "operation_time": 1778833301,
                "entry_status": 1,
                "entry_position": 16384
            },
            "tags": [],
            "participants": [
                {
                    "id": "c1ecd63e5af2461e810d402767e78fc8",
                    "url": "https://open.pingcode.com/v1/participants/c1ecd63e5af2461e810d402767e78fc8?principal_type=work_item&principal_id=6a06d7954cd222a3dad91d45",
                    "type": "user",
                    "user": {
                        "id": "c1ecd63e5af2461e810d402767e78fc8",
                        "url": "https://open.pingcode.com/v1/directory/users/c1ecd63e5af2461e810d402767e78fc8",
                        "name": "xxx",
                        "display_name": "xxx",
                        "avatar": null
                    }
                }
            ],
            "created_at": 1778833301,
            "created_by": {
                "id": "c1ecd63e5af2461e810d402767e78fc8",
                "url": "https://open.pingcode.com/v1/directory/users/c1ecd63e5af2461e810d402767e78fc8",
                "name": "xxx",
                "display_name": "xxx",
                "avatar": null
            },
            "updated_at": 1778833301,
            "updated_by": {
                "id": "c1ecd63e5af2461e810d402767e78fc8",
                "url": "https://open.pingcode.com/v1/directory/users/c1ecd63e5af2461e810d402767e78fc8",
                "name": "xxx",
                "display_name": "xxx",
                "avatar": null
            },
            "is_archived": 0,
            "is_deleted": 0
        }
    }
}

事件 ​

目前 Nexus 平台支持的系统事件如下:

事件说明
全局事件定义 PingCode 中全局系统事件
产品管理定义 PingCode 中产品管理系统事件
项目管理定义 PingCode 中项目管理系统事件
知识管理定义 PingCode 中知识管理系统事件
测试管理定义 PingCode 中测试管理系统事件

作用域范围 ​

订阅事件需要在应用 manifest.yaml 中申请相应的作用域范围,不同事件需要不同的作用域。如果权限不足,应用将无法接收到事件或获取完整数据。

yaml
event:
  triggers:
    - key: system_trigger
      type: system
      events:
        - pce:ship:idea:created
        - pce:ship:idea:updated
      handler: 
        function: ship-idea-handler
permissions:
  scopes:
    - pcp:read:ship:idea

事件循环 ​

在 manifest.yml 的触发器配置中,建议设置 filter.ignoreSelf: true 。这可以确保应用自身通过 APIs 触发的操作所产生的事件不会被自己再次订阅到,从而避免无限循环。

yaml
event:
  triggers:
    - key: system_trigger
      type: system
      events:
        - pce:ship:idea:created
        - pce:ship:idea:updated
      handler: 
        function: ship-idea-handler
      filter:
        ignoreSelf: true

举报邮箱:abuse@worktile.com