1
0
Fork 0
mirror of synced 2024-09-30 00:57:16 +13:00

Merge pull request #1815 from Budibase/fix/webhook-issue

Fixing some issues discovered with webhooks sending JSON
This commit is contained in:
Michael Drury 2021-06-23 20:45:42 +01:00 committed by GitHub
commit ef6678d683
7 changed files with 62 additions and 24 deletions

View file

@ -14,7 +14,6 @@
$: allowDeleteTrigger = !steps.length $: allowDeleteTrigger = !steps.length
function deleteStep() { function deleteStep() {
console.log("Running")
automationStore.actions.deleteAutomationBlock(block) automationStore.actions.deleteAutomationBlock(block)
} }
</script> </script>

View file

@ -16,11 +16,13 @@
$: automation = $automationStore.selectedAutomation?.automation $: automation = $automationStore.selectedAutomation?.automation
onMount(async () => { onMount(async () => {
// save the automation initially if (!automation?.definition?.trigger?.inputs.schemaUrl) {
await automationStore.actions.save({ // save the automation initially
instanceId, await automationStore.actions.save({
automation, instanceId,
}) automation,
})
}
interval = setInterval(async () => { interval = setInterval(async () => {
await automationStore.actions.fetch() await automationStore.actions.fetch()
const outputs = automation?.definition?.trigger.schema.outputs?.properties const outputs = automation?.definition?.trigger.schema.outputs?.properties

View file

@ -9,6 +9,9 @@
$: appUrl = $hostingStore.appUrl $: appUrl = $hostingStore.appUrl
function fullWebhookURL(uri) { function fullWebhookURL(uri) {
if (!uri) {
return ""
}
if (production) { if (production) {
return `${appUrl}/${uri}` return `${appUrl}/${uri}`
} else { } else {

View file

@ -4,6 +4,11 @@ module.exports = async (url, opts) => {
function json(body, status = 200) { function json(body, status = 200) {
return { return {
status, status,
headers: {
get: () => {
return ["application/json"]
},
},
json: async () => { json: async () => {
return body return body
}, },

View file

@ -88,6 +88,8 @@ async function checkForCronTriggers({ appId, oldAuto, newAuto }) {
async function checkForWebhooks({ appId, oldAuto, newAuto }) { async function checkForWebhooks({ appId, oldAuto, newAuto }) {
const oldTrigger = oldAuto ? oldAuto.definition.trigger : null const oldTrigger = oldAuto ? oldAuto.definition.trigger : null
const newTrigger = newAuto ? newAuto.definition.trigger : null const newTrigger = newAuto ? newAuto.definition.trigger : null
const triggerChanged =
oldTrigger && newTrigger && oldTrigger.id !== newTrigger.id
function isWebhookTrigger(auto) { function isWebhookTrigger(auto) {
return ( return (
auto && auto &&
@ -98,25 +100,32 @@ async function checkForWebhooks({ appId, oldAuto, newAuto }) {
// need to delete webhook // need to delete webhook
if ( if (
isWebhookTrigger(oldAuto) && isWebhookTrigger(oldAuto) &&
!isWebhookTrigger(newAuto) && (!isWebhookTrigger(newAuto) || triggerChanged) &&
oldTrigger.webhookId oldTrigger.webhookId
) { ) {
let db = new CouchDB(appId) try {
// need to get the webhook to get the rev let db = new CouchDB(appId)
const webhook = await db.get(oldTrigger.webhookId) // need to get the webhook to get the rev
const ctx = { const webhook = await db.get(oldTrigger.webhookId)
appId, const ctx = {
params: { id: webhook._id, rev: webhook._rev }, appId,
params: { id: webhook._id, rev: webhook._rev },
}
// might be updating - reset the inputs to remove the URLs
if (newTrigger) {
delete newTrigger.webhookId
newTrigger.inputs = {}
}
await webhooks.destroy(ctx)
} catch (err) {
// don't worry about not being able to delete, if it doesn't exist all good
} }
// might be updating - reset the inputs to remove the URLs
if (newTrigger) {
delete newTrigger.webhookId
newTrigger.inputs = {}
}
await webhooks.destroy(ctx)
} }
// need to create webhook // need to create webhook
else if (!isWebhookTrigger(oldAuto) && isWebhookTrigger(newAuto)) { if (
(!isWebhookTrigger(oldAuto) || triggerChanged) &&
isWebhookTrigger(newAuto)
) {
const ctx = { const ctx = {
appId, appId,
request: { request: {

View file

@ -77,14 +77,34 @@ module.exports.run = async function ({ inputs }) {
requestBody.length !== 0 && requestBody.length !== 0 &&
BODY_REQUESTS.indexOf(requestMethod) !== -1 BODY_REQUESTS.indexOf(requestMethod) !== -1
) { ) {
request.body = JSON.parse(requestBody) request.body =
typeof requestBody === "string"
? requestBody
: JSON.stringify(requestBody)
request.headers = {
"Content-Type": "application/json",
}
} }
try { try {
// do a quick JSON parse if there is a body, to generate an error if its invalid
if (request.body) {
JSON.parse(request.body)
}
const response = await fetch(url, request) const response = await fetch(url, request)
const contentType = response.headers.get("content-type")
const success = response.status === 200
let resp
if (!success) {
resp = response.statusText
} else if (contentType && contentType.indexOf("application/json") !== -1) {
resp = await response.json()
} else {
resp = await response.text()
}
return { return {
response: await response.json(), response: resp,
success: response.status === 200, success: success,
} }
} catch (err) { } catch (err) {
/* istanbul ignore next */ /* istanbul ignore next */

View file

@ -25,7 +25,7 @@ describe("test the outgoing webhook action", () => {
expect(res.success).toEqual(true) expect(res.success).toEqual(true)
expect(res.response.url).toEqual("http://www.test.com") expect(res.response.url).toEqual("http://www.test.com")
expect(res.response.method).toEqual("POST") expect(res.response.method).toEqual("POST")
expect(res.response.body.a).toEqual(1) expect(JSON.parse(res.response.body).a).toEqual(1)
}) })
it("should return an error if something goes wrong in fetch", async () => { it("should return an error if something goes wrong in fetch", async () => {