#218: Fixed webhook and app sdk execution bugs

This commit is contained in:
frikky
2020-12-11 04:22:22 +01:00
parent 035e9d144f
commit dc62016493
6 changed files with 112 additions and 30 deletions
+16 -12
View File
@@ -1039,19 +1039,20 @@ class AppBase:
print("PARAM: %s" % parameter)
try:
values = parameter["value_replace"]
added = 0
for val in values:
newparams.append({
"name": val["key"],
"value": val["value"],
"variant": "STATIC_VALUE",
"id": "body_replacement",
})
if values != None:
added = 0
for val in values:
newparams.append({
"name": val["key"],
"value": val["value"],
"variant": "STATIC_VALUE",
"id": "body_replacement",
})
print("Added param %s for body" % val["key"])
added += 1
print("Added param %s for body" % val["key"])
added += 1
print("ADDED %d parameters for body" % added)
print("ADDED %d parameters for body" % added)
except KeyError as e:
print("KeyError body OpenAPI: %s" % e)
pass
@@ -1427,7 +1428,10 @@ class AppBase:
print("Running with params (1): %s" % baseparams)
ret = await func(**baseparams)
print("Return from execution: %s" % ret)
if isinstance(ret, dict) or isinstance(ret, list):
if ret == None:
results.append("")
json_object = False
elif isinstance(ret, dict) or isinstance(ret, list):
results.append(ret)
json_object = True
else:
+26 -6
View File
@@ -3415,6 +3415,12 @@ func handleWebhookCallback(resp http.ResponseWriter, request *http.Request) {
log.Printf("This should trigger in the cloud. Duplicate action allowed onprem.")
}
type ExecutionStruct struct {
Start string `json:"start"`
ExecutionSource string `json:"execution_source"`
ExecutionArgument string `json:"execution_argument"`
}
for _, item := range hook.Workflows {
log.Printf("Running webhook for workflow %s with startnode %s", item, hook.Start)
workflow := Workflow{
@@ -3430,22 +3436,36 @@ func handleWebhookCallback(resp http.ResponseWriter, request *http.Request) {
}
parsedBody := string(body)
parsedBody = strings.Replace(parsedBody, "\"", "\\\"", -1)
//parsedBody = strings.Replace(parsedBody, "\"", "\\\"", -1)
if len(parsedBody) > 0 {
if string(parsedBody[0]) == `"` && string(parsedBody[len(parsedBody)-1]) == "\"" {
parsedBody = parsedBody[1 : len(parsedBody)-1]
}
}
bodyWrapper := fmt.Sprintf(`{"start": "%s", "execution_source": "webhook", "execution_argument": "%s"}`, hook.Start, string(parsedBody))
if len(hook.Start) == 0 {
log.Printf("No start node for hook %s - running with workflow default.", hook.Id)
bodyWrapper = string(parsedBody)
newBody := ExecutionStruct{
Start: hook.Start,
ExecutionSource: "webhook",
ExecutionArgument: string(parsedBody),
}
b, err := json.Marshal(newBody)
if err != nil {
log.Printf("Failed newBody marshaling: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}
//bodyWrapper := fmt.Sprintf(`{"start": "%s", "execution_source": "webhook", "execution_argument": "%s"}`, hook.Start, string(parsedBody))
//if len(hook.Start) == 0 {
// log.Printf("No start node for hook %s - running with workflow default.", hook.Id)
// bodyWrapper = string(parsedBody)
//}
newRequest := &http.Request{
Method: "POST",
Body: ioutil.NopCloser(strings.NewReader(bodyWrapper)),
Body: ioutil.NopCloser(bytes.NewReader(b)),
}
// OrgId: activeOrgs[0].Id,
+16 -4
View File
@@ -4023,15 +4023,18 @@ func deleteWorkflowApp(resp http.ResponseWriter, request *http.Request) {
resp.Write([]byte(`{"success": false}`))
return
} else {
log.Printf("App to be deleted is private")
private = true
}
q := datastore.NewQuery("workflow").Filter("org_id = ", user.ActiveOrg.Id)
// FIXME: Make workflows track themself INSIDE apps, or with a reference
q := datastore.NewQuery("workflow").Filter("org_id = ", user.ActiveOrg.Id).Limit(30)
var workflows []Workflow
_, err = dbclient.GetAll(ctx, q, &workflows)
if err != nil {
log.Printf("Failed getting related workflows for the app: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false, "reason": "}`))
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "%s"}`, err)))
return
}
@@ -5963,11 +5966,20 @@ func getAllSchedules(ctx context.Context, orgId string) ([]ScheduleOld, error) {
func getAllWorkflowApps(ctx context.Context) ([]WorkflowApp, error) {
var allworkflowapps []WorkflowApp
q := datastore.NewQuery("workflowapp").Limit(50).Order("-edited")
q := datastore.NewQuery("workflowapp").Order("-edited").Limit(50)
//Activated bool `json:"activated" yaml:"activated" required:false datastore:"activated"`
_, err := dbclient.GetAll(ctx, q, &allworkflowapps)
if err != nil {
return []WorkflowApp{}, err
if strings.Contains(fmt.Sprintf("%s", err), "ResourceExhausted") {
q := datastore.NewQuery("workflowapp").Limit(30).Order("-edited")
_, err := dbclient.GetAll(ctx, q, &allworkflowapps)
if err != nil {
return []WorkflowApp{}, err
}
} else {
return []WorkflowApp{}, err
}
}
return allworkflowapps, nil
+50 -4
View File
@@ -405,7 +405,6 @@ const AppCreator = (props) => {
newitem = newitem[tmpparam]
}
console.log("PARAM: ", newitem)
return newitem
//console.log("Should get ", parameter["$ref"])
@@ -422,7 +421,6 @@ const AppCreator = (props) => {
setBasedata(data)
if (data.info !== null && data.info !== undefined) {
console.log("DATA: ", data)
setName(data.info.title)
setDescription(data.info.description)
document.title = "Apps - "+data.info.title
@@ -500,6 +498,55 @@ const AppCreator = (props) => {
"example_response": "",
}
if (methodvalue["requestBody"] !== undefined) {
//console.log("Handle requestbody: ", methodvalue["requestBody"])
if (methodvalue["requestBody"]["content"] !== undefined) {
if (methodvalue["requestBody"]["content"]["application/json"] !== undefined) {
if (methodvalue["requestBody"]["content"]["application/json"]["schema"] !== undefined) {
if (methodvalue["requestBody"]["content"]["application/json"]["schema"]["properties"] !== undefined) {
var tmpobject = {}
for (let [prop, propvalue] of Object.entries(methodvalue["requestBody"]["content"]["application/json"]["schema"]["properties"])) {
tmpobject[prop] = `\$\{${prop}\}`
}
for (var subkey in methodvalue["requestBody"]["content"]["application/json"]["schema"]["required"]) {
const tmpitem = methodvalue["requestBody"]["content"]["application/json"]["schema"]["required"][subkey]
tmpobject[tmpitem] = `\$\{${tmpitem}\}`
}
newaction["body"] = JSON.stringify(tmpobject, null, 2)
}
}
} else if (methodvalue["requestBody"]["content"]["application/xml"] !== undefined) {
console.log("METHOD XML: ", methodvalue)
if (methodvalue["requestBody"]["content"]["application/xml"]["schema"] !== undefined) {
if (methodvalue["requestBody"]["content"]["application/xml"]["schema"]["properties"] !== undefined) {
var tmpobject = {}
for (let [prop, propvalue] of Object.entries(methodvalue["requestBody"]["content"]["application/xml"]["schema"]["properties"])) {
tmpobject[prop] = `\$\{${prop}\}`
}
for (var subkey in methodvalue["requestBody"]["content"]["application/xml"]["schema"]["required"]) {
const tmpitem = methodvalue["requestBody"]["content"]["application/xml"]["schema"]["required"][subkey]
tmpobject[tmpitem] = `\$\{${tmpitem}\}`
}
//console.log("OBJ XML: ", tmpobject)
//newaction["body"] = XML.stringify(tmpobject, null, 2)
}
}
} else {
if (methodvalue["requestBody"]["content"]["example"] !== undefined) {
if (methodvalue["requestBody"]["content"]["example"]["example"] !== undefined) {
newaction["body"] = methodvalue["requestBody"]["content"]["example"]["example"]
//JSON.stringify(tmpobject, null, 2)
}
}
console.log("NOT APPLICATION/JSON: ", methodvalue["requestBody"]["content"])
}
}
}
// HAHAHA wtf is this.
if (methodvalue.responses !== undefined) {
if (methodvalue.responses.default !== undefined) {
@@ -1317,7 +1364,6 @@ const AppCreator = (props) => {
const findBodyParams = (body) => {
const regex = /\${(\w+)}/g
const found = body.match(regex)
console.log("FOUND: ", found)
if (found === null) {
setExtraBodyFields([])
} else {
@@ -1521,7 +1567,7 @@ const AppCreator = (props) => {
const queries = values[1]
if (currentAction.paths !== paths && urlPath.length > 0) {
console.log("IN PATHS SETTER: !", paths)
//console.log("IN PATHS SETTER: !", paths)
setActionField("paths", paths)
}
+1 -1
View File
@@ -907,7 +907,7 @@ const Apps = (props) => {
<div style={{marginTop: 15}}>
{apps.length > 0 ?
filteredApps.length > 0 ?
<div style={{height: "75vh", overflowY: "scroll"}}>
<div style={{height: "75vh", overflowY: "auto"}}>
{filteredApps.map(app => {
return (
appPaper(app)
+3 -3
View File
@@ -1383,20 +1383,20 @@ func handleExecution(client *http.Client, req *http.Request, workflowExecution W
// FIXME - clean up stopped (remove) containers with this execution id
newresp, err := client.Do(req)
if err != nil {
log.Printf("Failed making request: %s", err)
log.Printf("[ERROR] Failed making request: %s", err)
time.Sleep(time.Duration(sleepTime) * time.Second)
continue
}
body, err := ioutil.ReadAll(newresp.Body)
if err != nil {
log.Printf("Failed reading body: %s", err)
log.Printf("[ERROR] Failed reading body: %s", err)
time.Sleep(time.Duration(sleepTime) * time.Second)
continue
}
if newresp.StatusCode != 200 {
log.Printf("Err: %s\nStatusCode: %d", string(body), newresp.StatusCode)
log.Printf("[ERROR] Bad statuscode: %s\nStatusCode: %d", string(body), newresp.StatusCode)
time.Sleep(time.Duration(sleepTime) * time.Second)
continue
}