summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/slack-go/slack/workflow_step_execute.go
blob: 18516f23ec36bbc302770395e4ef0553a88cfd0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
}