Trigger
Trigger 是一系列功能函数的组合,由产品事件发生后主动触发,比如:事项创建前、事项创建后、仓库Push、分支合并等事件。事件触发后,可通过 Trigger 找到对应插件中配置的功能函数执行,进而实现插件监听产品事件的能力。
以下场景为插件监听产品事件,当事件发生时执行对应的后续逻辑。
- 场景一:当事项创建后,给负责人发送消息通知。
- 场景二:当仓库master分支merge后,调用流水线进行构建
对接应用中心的产品,需要在产品事件触发后,调用应用中心的triggers接口触发Trigger。其中参数 event 代表事件类型,格式为:gei:proxima:item:created,规则如下:
- gei: gitee event identifier 固定字符串
- proxima: 产品名,如:pipe\code\one
- item:模块名,如:仓库名(repo)
- created:事件名,如:pushed
- 例如:gei:code:repo:created
接口说明
POST
/apps/api/v1/:applicationId/apps/triggers
Path Parameters
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
applicationId | string | 是 | 租户Key,例如osc |
Body Parameters
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
event | string | 是 | 事件类型,例如gei:proxima:item:created gei: gitee event identifier 固定字符串 proxima: 产品名,如:pipe\code\one item:模块名,如:仓库名(repo) created:事件名,如:pushed |
param1 | any | 否 | 自定义的参数名称,可以直接传到插件的payload中使用 |
... | any | 否 | 其他自定义参数 |
Response
名称 | 类型 | 说明 |
---|---|---|
code | number | 错误码,只有报错时会返回 |
message | string | 错误信息,只有报错时会返回 |
... | any | 插件返回的自定义参数 |
Example
调用示例
try {
const { data } = await axios({
method: 'POST',
url: 'http://gitee-proxima-core:1337/apps/api/v1/osc/apps/triggers',
data: {
event: 'gei:proxima:item:created',
itemId: 'itemId',
itemKey: 'itemKey',
}
})
if(data.code) {
throw new Error(data.message);
}
// item是插件返回的数据
const { item } = data;
// your code
} catch(e) {
console.error(e)
}
插件使用demo
插件的创建可以参考Init,也可以参考Example Apps
manifest.yml
app:
name: trigger-demo
key: trigger_demo
version: "0.0.1"
modules:
trigger:
- key: item-created
events:
- gei:proxima:item:created
function: item-saved-function
async: false
function:
- key: item-saved-function
handler: index.runItemSavedTrigger
index.ts
export const runItemSavedTrigger = async({{ payload }}) => {
// 从调用方获取的参数
const {itemId, itemKey} = payload;
// your code
// 返回的item可以在上面api的返回中获取
return { item }
}