Made webhooks able to connect to any node

This commit is contained in:
frikky
2020-06-26 08:02:27 +02:00
parent 99ec5739e6
commit d02bffa789
4 changed files with 53 additions and 35 deletions
+24 -28
View File
@@ -362,6 +362,7 @@ type HookAction struct {
type Hook struct {
Id string `json:"id" datastore:"id"`
Start string `json:"start" datastore:"start"`
Info Info `json:"info" datastore:"info"`
Actions []HookAction `json:"actions" datastore:"actions"`
Type string `json:"type" datastore:"type"`
@@ -3064,7 +3065,7 @@ func handleWebhookCallback(resp http.ResponseWriter, request *http.Request) {
hookId = hookId[8:len(hookId)]
log.Printf("HookID: %s", hookId)
//log.Printf("HookID: %s", hookId)
hook, err := getHook(ctx, hookId)
if err != nil {
log.Printf("Failed getting hook: %s", err)
@@ -3097,6 +3098,24 @@ func handleWebhookCallback(resp http.ResponseWriter, request *http.Request) {
ID: "",
}
body, err := ioutil.ReadAll(request.Body)
if err != nil {
log.Printf("Body data error: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}
bodyWrapper := fmt.Sprintf(`{"start": "%s", "execution_argument": "%s"}`, hook.Start, string(body))
if len(hook.Start) == 0 {
bodyWrapper = string(body)
}
request := &http.Request{
Method: "POST",
Body: ioutil.NopCloser(strings.NewReader(bodyWrapper)),
}
workflowExecution, executionResp, err := handleExecution(item, workflow, request)
if err == nil {
@@ -3136,6 +3155,7 @@ func handleNewHook(resp http.ResponseWriter, request *http.Request) {
Id string `json:"id"`
Name string `json:"name"`
Workflow string `json:"workflow"`
Start string `json:"start"`
}
body, err := ioutil.ReadAll(request.Body)
@@ -3146,7 +3166,7 @@ func handleNewHook(resp http.ResponseWriter, request *http.Request) {
return
}
log.Println("Data: %s", string(body))
log.Printf("Data: %s", string(body))
ctx := context.Background()
var requestdata requestData
@@ -3196,6 +3216,7 @@ func handleNewHook(resp http.ResponseWriter, request *http.Request) {
hook := Hook{
Id: newId,
Start: requestdata.Start,
Workflows: []string{requestdata.Workflow},
Info: Info{
Name: requestdata.Name,
@@ -3216,31 +3237,6 @@ func handleNewHook(resp http.ResponseWriter, request *http.Request) {
Running: false,
}
// FIXME: Add cloud function execution?
//b, err := json.Marshal(hook)
//if err != nil {
// log.Printf("Failed marshalling: %s", err)
// resp.WriteHeader(401)
// resp.Write([]byte(`{"success": false}`))
// return
//}
//environmentVariables := map[string]string{
// "FUNCTION_APIKEY": user.ApiKey,
// "CALLBACKURL": "https://shuffler.io",
// "HOOKID": hook.Id,
//}
//applocation := fmt.Sprintf("gs://%s/triggers/webhook.zip", bucketName)
//hookname := fmt.Sprintf("webhook_%s", hook.Id)
//err = deployWebhookFunction(ctx, hookname, defaultLocation, applocation, environmentVariables)
//if err != nil {
// log.Printf("Error deploying hook: %s", err)
// resp.WriteHeader(401)
// resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Issue with starting hook. Please wait a second and try again"}`)))
// return
//}
hook.Status = "running"
hook.Running = true
err = setHook(ctx, hook)
@@ -3256,7 +3252,7 @@ func handleNewHook(resp http.ResponseWriter, request *http.Request) {
log.Printf("Failed to increase total workflows: %s", err)
}
log.Println("Generating new hook")
log.Println("Set up a new hook")
resp.WriteHeader(200)
resp.Write([]byte(`{"success": true}`))
}
+10 -1
View File
@@ -2015,7 +2015,7 @@ func handleExecution(id string, workflow Workflow, request *http.Request) (Workf
}
} else if len(execution.Start) > 0 {
log.Printf("START ACTION %s IS WRONG ID LENGTH %d!", len(execution.Start))
log.Printf("START ACTION %s IS WRONG ID LENGTH %d!", execution.Start, len(execution.Start))
return WorkflowExecution{}, fmt.Sprintf("Startnode %s was not found in actions", execution.Start), errors.New(fmt.Sprintf("Startnode %s was not found in actions", execution.Start))
}
@@ -2166,11 +2166,15 @@ func handleExecution(id string, workflow Workflow, request *http.Request) (Workf
childNodes := findChildNodes(workflowExecution, workflowExecution.Start)
topic := "workflows"
startFound := false
// FIXME - remove this?
newActions := []Action{}
defaultResults := []ActionResult{}
for _, action := range workflowExecution.Workflow.Actions {
action.LargeImage = ""
if action.ID == workflowExecution.Start {
startFound = true
}
//log.Println(action.Environment)
if action.Environment == "" {
@@ -2210,6 +2214,11 @@ func handleExecution(id string, workflow Workflow, request *http.Request) (Workf
}
}
if !startFound {
log.Printf("Startnode %s doesn't exist!", workflowExecution.Start)
return WorkflowExecution{}, fmt.Sprintf("Workflow action %s doesn't exist in workflow", workflowExecution.Start), errors.New(fmt.Sprintf("Workflow start node %s doesn't exist. Exiting!", workflowExecution.Start))
}
// Verification for execution environments
workflowExecution.Results = defaultResults
workflowExecution.Workflow.Actions = newActions
+3 -1
View File
@@ -1 +1,3 @@
curl -H "Content-Type: application/json" localhost:5001/api/v1/triggers/9e845679-5843-4959-a76c-a6d664e9df35 -H "Authorization: Bearer 377469e8-dd5d-4521-8d9e-416d8d2f6fd4"
# curl -H "Content-Type: application/json" localhost:5001/api/v1/triggers/9e845679-5843-4959-a76c-a6d664e9df35 -H "Authorization: Bearer 377469e8-dd5d-4521-8d9e-416d8d2f6fd4"
curl -XPOST http://localhost:5001/api/v1/hooks/webhook_1b968d49-2d78-4132-bf91-0f3a1f6a79f3 -d '{}'
+16 -5
View File
@@ -1840,9 +1840,9 @@ const AngularWorkflow = (props) => {
position: newposition,
}
if (data.trigger_type === "WEBHOOK") {
newAppData.status = "running"
}
//if (data.trigger_type === "WEBHOOK") {
// newAppData.status = "running"
//}
// Can all the data be in here? hmm
const nodeToBeAdded = {
@@ -1888,8 +1888,8 @@ const AngularWorkflow = (props) => {
setWorkflow(workflow)
if (data.trigger_type === "WEBHOOK") {
newWebhook(newAppData)
saveWorkflow(workflow)
//newWebhook(newAppData)
//saveWorkflow(workflow)
}
}
}
@@ -3837,13 +3837,24 @@ const AngularWorkflow = (props) => {
return
}
// Check the node it's connected to
var startNode = workflow.start
const branch = workflow.branches.find(branch => branch.source_id === trigger.id)
if (branch === undefined && (workflow.start === undefined || workflow.start === null || workflow.start.length === 0)) {
alert.error("No webhook node defined")
}
alert.info("Starting webhook")
if (branch !== undefined) {
startNode = branch.destination_id
}
const data = {
"name": hookname,
"type": "webhook",
"id": trigger.id,
"workflow": workflow.id,
"start": startNode,
}
fetch(globalUrl+"/api/v1/hooks/new", {