Skip to content

Commit

Permalink
added webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
inlife committed Jul 8, 2024
1 parent 02fbfff commit 0e324b1
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
65 changes: 65 additions & 0 deletions packages/nexrender-action-webhook/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const fetch = global.fetch || require('node-fetch');

const replaceJobData = (settings, job, data) => {
if (typeof data === 'string') {
// find all occurences of {job.*} in the string
return data.replace(/{job\.([^}]+)}/g, (match, path) => {
const rest = path.split('.');

if (!rest.length) {
return ''
}

const value = rest.reduce((acc, key) => {
if (acc === undefined) {
return acc;
}

return acc[key];
}, job);

return value;
})
}

if (Array.isArray(data)) {
return data.map(item => replaceJobData(settings, job, item));
}

if (typeof data === 'object') {
return Object.keys(data).reduce((acc, key) => {
acc[key] = replaceJobData(settings, job, data[key]);
return acc;
}, {});
}

return data;
}

module.exports = async (job, settings, { url, method, headers, json, body }) => {
url = replaceJobData(settings, job, url);
headers = replaceJobData(settings, job, headers);

if (json) {
json = replaceJobData(settings, job, json);
}

if (body) {
body = replaceJobData(settings, job, body);
}

settings.logger.log(`[webhook] sending request to ${url}`);

const response = await fetch(url, {
method: method || 'POST',
headers,
body: json ? JSON.stringify(json) : body,
});

if (!response.ok) {
settings.logger.log(`[webhook] request failed with status code ${response.status}`);
return;
}

settings.logger.log(`[webhook] request successful with status code ${response.status}`);
}
13 changes: 13 additions & 0 deletions packages/nexrender-action-webhook/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@nexrender/action-webhook",
"homepage": "https://www.nexrender.com",
"author": "inlife",
"version": "1.51.1",
"main": "index.js",
"publishConfig": {
"access": "public"
},
"dependencies": {
"node-fetch": "^3.3.2"
}
}
106 changes: 106 additions & 0 deletions packages/nexrender-action-webhook/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
let object = null;

global.fetch = async (url, options) => {
object = { url, options };
return {
ok: true,
status: 200,
json: async () => ({"status": "ok"}),
text: async () => JSON.stringify({"status": "ok"}),
};
};


const action = require("./index.js");
const assert = require("assert");

describe("action/webhook", () => {
const settings = {
logger: { log: () => {} },
}

const job = {
uid: "123",
state: "queued",
custom: {
super: "value",
}
}

it("should send a request to a webhook", async () => {
await action(job, settings, {
url: "http://example.com",
method: "POST",
headers: {
"Content-Type": "application/json",
},
json: {
somevalue: 123,
},
});

assert.equal(object.url, "http://example.com");
assert.equal(object.options.method, "POST");
assert.equal(object.options.headers["Content-Type"], "application/json");
assert.equal(object.options.body, JSON.stringify({somevalue: 123}));
})

it("should replace job data in the webhook url", async () => {
await action(job, settings, {
url: "http://example.com/{job.uid}",
method: "POST",
headers: {
"Content-Type": "application/json",
},
json: {
somevalue: 123,
},
});

assert.equal(object.url, "http://example.com/123");
})

it("should replace job data in the webhook headers", async () => {
await action(job, settings, {
url: "http://example.com",
method: "POST",
headers: {
"Content-Type": "{job.custom.super}",
},
json: {
somevalue: 123,
},
});

assert.equal(object.options.headers["Content-Type"], "value");
})

it("should replace job data in the webhook json", async () => {
await action(job, settings, {
url: "http://example.com",
method: "POST",
headers: {
"Content-Type": "application/json",
},
json: {
somevalue: "value here {job.custom.super}:{job.uid}",
state: "{job.state}",
},
});

assert.equal(object.options.body, JSON.stringify({somevalue: "value here value:123", state: "queued"}));
})

it("should replace job data in the webhook body", async () => {
await action(job, settings, {
url: "http://example.com",
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: "somevalue: value here {job.custom.super}:{job.uid}, state: {job.state}"
});

assert.equal(object.options.body, "somevalue: value here value:123, state: queued");
})
})

0 comments on commit 0e324b1

Please sign in to comment.