Skip to content

Commit

Permalink
refactor: move webhook into pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
abuaboud committed Mar 4, 2024
1 parent f2d45aa commit 5c86deb
Show file tree
Hide file tree
Showing 48 changed files with 395 additions and 543 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -15,6 +15,7 @@
"cli": "npx ts-node packages/cli/src/index.ts",
"create-piece": "npx ts-node packages/cli/src/index.ts pieces create",
"create-action": "npx ts-node packages/cli/src/index.ts actions create",
"create-trigger": "npx ts-node packages/cli/src/index.ts triggers create",
"sync-pieces": "npx ts-node packages/cli/src/index.ts pieces sync",
"publish-piece": "npx ts-node tools/scripts/pieces/publish-piece.ts"
},
Expand Down
Expand Up @@ -46,7 +46,6 @@ export const testExecutionContext = {
case ActionType.CODE:
case TriggerType.EMPTY:
case TriggerType.PIECE:
case TriggerType.WEBHOOK:
flowExecutionContext = flowExecutionContext.upsertStep(step.name, GenericStepOutput.create({
input: step.settings,
type: stepType,
Expand Down
1 change: 0 additions & 1 deletion packages/engine/src/lib/helper/logging-utils.ts
Expand Up @@ -21,7 +21,6 @@ async function trimStepOutput(stepOutput: StepOutput): Promise<StepOutput> {
modified.input = await applyFunctionToValues(modified.input, trim)
switch (modified.type) {
case ActionType.BRANCH:
case TriggerType.WEBHOOK:
case TriggerType.EMPTY:
case TriggerType.PIECE:
modified.output = await applyFunctionToValues(modified.output, trim)
Expand Down
2 changes: 1 addition & 1 deletion packages/pieces/community/framework/package.json
@@ -1,5 +1,5 @@
{
"name": "@activepieces/pieces-framework",
"version": "0.7.21",
"version": "0.7.22",
"type": "commonjs"
}
33 changes: 33 additions & 0 deletions packages/pieces/community/webhook/.eslintrc.json
@@ -0,0 +1,33 @@
{
"extends": [
"../../../../.eslintrc.json"
],
"ignorePatterns": [
"!**/*"
],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx"
],
"rules": {}
},
{
"files": [
"*.ts",
"*.tsx"
],
"rules": {}
},
{
"files": [
"*.js",
"*.jsx"
],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions packages/pieces/community/webhook/README.md
@@ -0,0 +1,7 @@
# pieces-webhook

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build pieces-webhook` to build the library.
4 changes: 4 additions & 0 deletions packages/pieces/community/webhook/package.json
@@ -0,0 +1,4 @@
{
"name": "@activepieces/piece-webhook",
"version": "0.0.1"
}
43 changes: 43 additions & 0 deletions packages/pieces/community/webhook/project.json
@@ -0,0 +1,43 @@
{
"name": "pieces-webhook",
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/pieces/community/webhook/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": [
"{options.outputPath}"
],
"options": {
"outputPath": "dist/packages/pieces/community/webhook",
"tsConfig": "packages/pieces/community/webhook/tsconfig.lib.json",
"packageJson": "packages/pieces/community/webhook/package.json",
"main": "packages/pieces/community/webhook/src/index.ts",
"assets": [
"packages/pieces/community/webhook/*.md"
],
"buildableProjectDepsInPackageJsonType": "dependencies",
"updateBuildableProjectDepsInPackageJson": true
}
},
"publish": {
"command": "node tools/scripts/publish.mjs pieces-webhook {args.ver} {args.tag}",
"dependsOn": [
"build"
]
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": [
"{options.outputFile}"
],
"options": {
"lintFilePatterns": [
"packages/pieces/community/webhook/**/*.ts"
]
}
}
},
"tags": []
}
13 changes: 13 additions & 0 deletions packages/pieces/community/webhook/src/index.ts
@@ -0,0 +1,13 @@

import { createPiece, PieceAuth } from "@activepieces/pieces-framework";
import { catchRequest } from "./lib/triggers/catch-hook";

export const webhook = createPiece({
displayName: "Webhook",
auth: PieceAuth.None(),
minimumSupportedRelease: '0.21.0',
logoUrl: "https://cdn.activepieces.com/pieces/webhook.svg",
authors: [],
actions: [],
triggers: [catchRequest],
});
36 changes: 36 additions & 0 deletions packages/pieces/community/webhook/src/lib/triggers/catch-hook.ts
@@ -0,0 +1,36 @@

import { createTrigger, Property, TriggerStrategy } from '@activepieces/pieces-framework';

const message = `
Copy the following URL:
**{{webhookUrl}}**
If you are expecting a reply from this webhook, append **/sync** to the URL.
In that case, you will also have to add an HTTP step with **return response** to the end of your flow.
If the flow takes more than **30 seconds**, it will give a **408 Request Timeout** response.
`;

export const catchRequest = createTrigger({
name: 'catch_request',
displayName: 'Catch Request',
description: 'Receive incoming HTTP/webhooks using any HTTP method such as GET, POST, PUT, DELETE, etc.',
props: {
markdown: Property.MarkDown({
value: message
}),
},
sampleData: null,
type: TriggerStrategy.WEBHOOK,
async onEnable() {
// ignore
},
async onDisable() {
// ignore
},
async run(context) {
return [context.payload]
}
})
19 changes: 19 additions & 0 deletions packages/pieces/community/webhook/tsconfig.json
@@ -0,0 +1,19 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
}
]
}
11 changes: 11 additions & 0 deletions packages/pieces/community/webhook/tsconfig.lib.json
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}
@@ -0,0 +1,81 @@
import { logger } from 'server-shared'
import { MigrationInterface, QueryRunner } from 'typeorm'

const FLOW_VERSION_TABLE = 'flow_version'

export class MigrateWebhook1709581196563 implements MigrationInterface {
name = 'MigrateWebhook1709581196563'

public async up(queryRunner: QueryRunner): Promise<void> {
logger.info('MigrateWebhook1709581196563, started')

let count = 0
const flowVersionsIds = await queryRunner.query('SELECT id FROM flow_version')

for (const { id } of flowVersionsIds) {
const [flowVersion] = await queryRunner.query('SELECT * FROM flow_version WHERE id = $1', [id])
const step = parseJson(flowVersion.trigger)
const isString = typeof flowVersion.trigger === 'string'
if (step.type === 'WEBHOOK') {
step.type = 'PIECE_TRIGGER'
step.settings = {
input: {},
'inputUiInfo': step.settings.inputUiInfo,
triggerName: 'catch_request',
pieceName: '@activepieces/piece-webhook',
pieceVersion: '~0.0.1',
'pieceType': 'OFFICIAL',
'packageType': 'REGISTRY',
}
count++
const result = isString ? JSON.stringify(step) : step
await queryRunner.query(
`UPDATE ${FLOW_VERSION_TABLE} SET trigger = $1 WHERE id = $2`,
[result, flowVersion.id],
)
}
}
logger.info('MigrateWebhook1709581196563, finished flows ' + count)
}

public async down(queryRunner: QueryRunner): Promise<void> {
logger.info('rolling back MigrateWebhook1709581196563, started')

let count = 0
const flowVersionsIds = await queryRunner.query('SELECT id FROM flow_version')

for (const { id } of flowVersionsIds) {
const [flowVersion] = await queryRunner.query('SELECT * FROM flow_version WHERE id = $1', [id])

const step = parseJson(flowVersion.trigger)
const isString = typeof flowVersion.trigger === 'string'
if (step.type === 'PIECE_TRIGGER') {
if (step.settings.pieceName === '@activepieces/piece-webhook') {
step.type = 'WEBHOOK'
step.settings = {
'inputUiInfo': step.settings.inputUiInfo,
}
count++
const result = isString ? JSON.stringify(step) : step
await queryRunner.query(
`UPDATE ${FLOW_VERSION_TABLE} SET trigger = $1 WHERE id = $2`,
[result, flowVersion.id],
)
}
}
}
logger.info(
'rolling back MigrateWebhook1709581196563, finished flows ' + count,
)
}
}


const parseJson = (json: string) => {
try {
return JSON.parse(json)
}
catch (e) {
return json
}
}
@@ -0,0 +1,79 @@
import { logger } from 'server-shared'
import { MigrationInterface, QueryRunner } from 'typeorm'

export class MigrateWebhookTemplate1709581196564 implements MigrationInterface {
name = 'MigrateWebhookTemplate1709581196564'

public async up(queryRunner: QueryRunner): Promise<void> {
logger.info('MigrateWebhookTemplate1709581196564, started')

let count = 0
const flowVersionsIds = await queryRunner.query('SELECT id FROM flow_template')

for (const { id } of flowVersionsIds) {
const [flowVersion] = await queryRunner.query('SELECT * FROM flow_template WHERE id = $1', [id])
const step = parseJson(flowVersion.template.trigger)
const isString = typeof flowVersion.template.trigger === 'string'
if (step.type === 'WEBHOOK') {
step.type = 'PIECE_TRIGGER'
step.settings = {
input: {},
'inputUiInfo': step.settings.inputUiInfo,
triggerName: 'catch_request',
pieceName: '@activepieces/piece-webhook',
pieceVersion: '~0.0.1',
'pieceType': 'OFFICIAL',
'packageType': 'REGISTRY',
}
count++
const result = isString ? JSON.stringify(step) : step
await queryRunner.query(
'UPDATE flow_template SET template = $1 WHERE id = $2',
[result, flowVersion.id],
)
}
}
logger.info('MigrateWebhookTemplate1709581196564, finished flows ' + count)
}

public async down(queryRunner: QueryRunner): Promise<void> {
logger.info('rolling back MigrateWebhookTemplate1709581196564, started')

let count = 0
const flowVersionsIds = await queryRunner.query('SELECT id FROM flow_template')

for (const { id } of flowVersionsIds) {
const [flowVersion] = await queryRunner.query('SELECT * FROM flow_template WHERE id = $1', [id])

const step = parseJson(flowVersion.template.trigger)
const isString = typeof flowVersion.template.trigger === 'string'
if (step.type === 'PIECE_TRIGGER') {
if (step.settings.pieceName === '@activepieces/piece-webhook') {
step.type = 'WEBHOOK'
step.settings = {
'inputUiInfo': step.settings.inputUiInfo,
}
count++
const result = isString ? JSON.stringify(step) : step
await queryRunner.query(
'UPDATE flow_template SET template = $1 WHERE id = $2',
[result, flowVersion.id],
)
}
}
}
logger.info(
'rolling back MigrateWebhookTemplate1709581196564, finished flows ' + count,
)
}
}


const parseJson = (json: string) => {
try {
return JSON.parse(json)
}
catch (e) {
return json
}
}

0 comments on commit 5c86deb

Please sign in to comment.