| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | ) |
| 7 | |
| 8 | type ( |
| 9 | WorkflowStepCompletedRequest struct { |
| 10 | WorkflowStepExecuteID string `json:"workflow_step_execute_id"` |
| 11 | Outputs map[string]string `json:"outputs"` |
| 12 | } |
| 13 | |
| 14 | WorkflowStepFailedRequest struct { |
| 15 | WorkflowStepExecuteID string `json:"workflow_step_execute_id"` |
| 16 | Error struct { |
| 17 | Message string `json:"message"` |
| 18 | } `json:"error"` |
| 19 | } |
| 20 | ) |
| 21 | |
| 22 | type WorkflowStepCompletedRequestOption func(opt WorkflowStepCompletedRequest) error |
| 23 | |
| 24 | func WorkflowStepCompletedRequestOptionOutput(outputs map[string]string) WorkflowStepCompletedRequestOption { |
| 25 | return func(opt WorkflowStepCompletedRequest) error { |
| 26 | if len(outputs) > 0 { |
| 27 | opt.Outputs = outputs |
| 28 | } |
| 29 | return nil |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // WorkflowStepCompleted indicates step is completed |
| 34 | func (api *Client) WorkflowStepCompleted(workflowStepExecuteID string, options ...WorkflowStepCompletedRequestOption) error { |
| 35 | // More information: https://api.slack.com/methods/workflows.stepCompleted |
| 36 | r := WorkflowStepCompletedRequest{ |
| 37 | WorkflowStepExecuteID: workflowStepExecuteID, |
| 38 | } |
| 39 | for _, option := range options { |
| 40 | option(r) |
| 41 | } |
| 42 | |
| 43 | endpoint := api.endpoint + "workflows.stepCompleted" |
| 44 | jsonData, err := json.Marshal(r) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | |
| 49 | response := &SlackResponse{} |
| 50 | if err := postJSON(context.Background(), api.httpclient, endpoint, api.token, jsonData, response, api); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | if !response.Ok { |
| 55 | return response.Err() |
| 56 | } |
| 57 | |
| 58 | return nil |
| 59 | } |
| 60 | |
| 61 | // WorkflowStepFailed indicates step is failed |
| 62 | func (api *Client) WorkflowStepFailed(workflowStepExecuteID string, errorMessage string) error { |
| 63 | // More information: https://api.slack.com/methods/workflows.stepFailed |
| 64 | r := WorkflowStepFailedRequest{ |
| 65 | WorkflowStepExecuteID: workflowStepExecuteID, |
| 66 | } |
| 67 | r.Error.Message = errorMessage |
| 68 | |
| 69 | endpoint := api.endpoint + "workflows.stepFailed" |
| 70 | jsonData, err := json.Marshal(r) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | response := &SlackResponse{} |
| 76 | if err := postJSON(context.Background(), api.httpclient, endpoint, api.token, jsonData, response, api); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | if !response.Ok { |
| 81 | return response.Err() |
| 82 | } |
| 83 | |
| 84 | return nil |
| 85 | } |