-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}) | ||
}) |