From 5547165d166c93d2e81233e514a3d9876b437d26 Mon Sep 17 00:00:00 2001 From: tanaka Date: Mon, 2 May 2022 23:54:23 +0900 Subject: [PATCH 1/3] add: workflows.stepCompleted and workflwows.stepFailed --- workflow_step_execute.go | 74 +++++++++++++++++++++++++++++++++++ workflow_step_execute_test.go | 35 +++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 workflow_step_execute.go create mode 100644 workflow_step_execute_test.go diff --git a/workflow_step_execute.go b/workflow_step_execute.go new file mode 100644 index 000000000..de31b196f --- /dev/null +++ b/workflow_step_execute.go @@ -0,0 +1,74 @@ +package slack + +import ( + "context" + "encoding/json" +) + +type ( + WorkflowStepCompletedRequest struct { + WorkflowStepExecuteID string `json:"workflow_step_execute_id"` + Outputs *map[string]string `json:"outputs"` + } + + WorkflowStepFailedRequest struct { + WorkflowStepExecuteID string `json:"workflow_step_execute_id"` + Error struct { + Message string `json:"message"` + } `json:"error"` + } +) + +// WorkflowStepCompleted indicates step is completed +func (api *Client) WorkflowStepCompleted(workflowStepExecuteID string, outputs *map[string]string) error { + // More information: https://api.slack.com/methods/workflows.stepCompleted + r := WorkflowStepCompletedRequest{ + WorkflowStepExecuteID: workflowStepExecuteID, + } + if outputs != nil { + r.Outputs = outputs + } + + endpoint := api.endpoint + "workflows.stepCompleted" + jsonData, err := json.Marshal(r) + if err != nil { + return err + } + + response := &SlackResponse{} + if err := postJSON(context.Background(), api.httpclient, endpoint, api.token, jsonData, response, api); err != nil { + return err + } + + if !response.Ok { + return response.Err() + } + + return nil +} + +// WorkflowStepFailed indicates step is failed +func (api *Client) WorkflowStepFailed(workflowStepExecuteID string, errorMessage string) error { + // More information: https://api.slack.com/methods/workflows.stepFailed + r := WorkflowStepFailedRequest{ + WorkflowStepExecuteID: workflowStepExecuteID, + } + r.Error.Message = errorMessage + + endpoint := api.endpoint + "workflows.stepFailed" + jsonData, err := json.Marshal(r) + if err != nil { + return err + } + + response := &SlackResponse{} + if err := postJSON(context.Background(), api.httpclient, endpoint, api.token, jsonData, response, api); err != nil { + return err + } + + if !response.Ok { + return response.Err() + } + + return nil +} diff --git a/workflow_step_execute_test.go b/workflow_step_execute_test.go new file mode 100644 index 000000000..aae88f87e --- /dev/null +++ b/workflow_step_execute_test.go @@ -0,0 +1,35 @@ +package slack + +import ( + "encoding/json" + "net/http" + "testing" +) + +func workflowStepHandler(rw http.ResponseWriter, r *http.Request) { + rw.Header().Set("Content-Type", "application/json") + response, _ := json.Marshal(SlackResponse{ + Ok: true, + }) + rw.Write(response) +} + +func TestWorkflowStepCompleted(t *testing.T) { + http.HandleFunc("/workflows.stepCompleted", workflowStepHandler) + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + + if err := api.WorkflowStepCompleted("executeID", nil); err != nil { + t.Errorf("Unexpected error: %s", err) + } +} + +func TestWorkflowStepFailed(t *testing.T) { + http.HandleFunc("/workflows.stepFailed", workflowStepHandler) + once.Do(startServer) + api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) + + if err := api.WorkflowStepFailed("executeID", "error message"); err != nil { + t.Errorf("Unexpected error: %s", err) + } +} From f55b7c92818a909c9f4ebc4c74bdbc84498eb0c5 Mon Sep 17 00:00:00 2001 From: tanaka Date: Mon, 13 Jun 2022 22:31:13 +0900 Subject: [PATCH 2/3] refactor: change WorkflowStepCompleted args --- workflow_step_execute.go | 17 ++++++++++++++--- workflow_step_execute_test.go | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/workflow_step_execute.go b/workflow_step_execute.go index de31b196f..d1d77cbad 100644 --- a/workflow_step_execute.go +++ b/workflow_step_execute.go @@ -19,14 +19,25 @@ type ( } ) +type WorkflowStepCompletedRequestOption func(opt WorkflowStepCompletedRequest) error + +func WorkflowStepCompletedRequestOptionOutput(outputs map[string]string) WorkflowStepCompletedRequestOption { + return func(opt WorkflowStepCompletedRequest) error { + if len(outputs) > 0 { + opt.Outputs = &outputs + } + return nil + } +} + // WorkflowStepCompleted indicates step is completed -func (api *Client) WorkflowStepCompleted(workflowStepExecuteID string, outputs *map[string]string) error { +func (api *Client) WorkflowStepCompleted(workflowStepExecuteID string, options ...WorkflowStepCompletedRequestOption) error { // More information: https://api.slack.com/methods/workflows.stepCompleted r := WorkflowStepCompletedRequest{ WorkflowStepExecuteID: workflowStepExecuteID, } - if outputs != nil { - r.Outputs = outputs + for _, option := range options { + option(r) } endpoint := api.endpoint + "workflows.stepCompleted" diff --git a/workflow_step_execute_test.go b/workflow_step_execute_test.go index aae88f87e..60c21c8ec 100644 --- a/workflow_step_execute_test.go +++ b/workflow_step_execute_test.go @@ -19,7 +19,7 @@ func TestWorkflowStepCompleted(t *testing.T) { once.Do(startServer) api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/")) - if err := api.WorkflowStepCompleted("executeID", nil); err != nil { + if err := api.WorkflowStepCompleted("executeID"); err != nil { t.Errorf("Unexpected error: %s", err) } } From 9013791a078cb1d461d50e6646bacfe1978c9c81 Mon Sep 17 00:00:00 2001 From: tanaka Date: Fri, 22 Jul 2022 12:31:38 +0900 Subject: [PATCH 3/3] fix: change Outputs type *map[string]string to map[string]strring --- workflow_step_execute.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/workflow_step_execute.go b/workflow_step_execute.go index d1d77cbad..18516f23e 100644 --- a/workflow_step_execute.go +++ b/workflow_step_execute.go @@ -7,8 +7,8 @@ import ( type ( WorkflowStepCompletedRequest struct { - WorkflowStepExecuteID string `json:"workflow_step_execute_id"` - Outputs *map[string]string `json:"outputs"` + WorkflowStepExecuteID string `json:"workflow_step_execute_id"` + Outputs map[string]string `json:"outputs"` } WorkflowStepFailedRequest struct { @@ -24,7 +24,7 @@ type WorkflowStepCompletedRequestOption func(opt WorkflowStepCompletedRequest) e func WorkflowStepCompletedRequestOptionOutput(outputs map[string]string) WorkflowStepCompletedRequestOption { return func(opt WorkflowStepCompletedRequest) error { if len(outputs) > 0 { - opt.Outputs = &outputs + opt.Outputs = outputs } return nil }