Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1060 from nakatanakatana/add_workflows_methods
add: workflows.stepCompleted and workflwows.stepFailed support
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
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"` | ||
} | ||
) | ||
|
||
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, options ...WorkflowStepCompletedRequestOption) error { | ||
// More information: https://api.slack.com/methods/workflows.stepCompleted | ||
r := WorkflowStepCompletedRequest{ | ||
WorkflowStepExecuteID: workflowStepExecuteID, | ||
} | ||
for _, option := range options { | ||
option(r) | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); 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) | ||
} | ||
} |