-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_executor.go
More file actions
220 lines (198 loc) · 6.58 KB
/
github_executor.go
File metadata and controls
220 lines (198 loc) · 6.58 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package plugin
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"time"
"github.com/argoproj/argo-workflows/v3/pkg/plugins/executor"
"github.com/google/go-github/github"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
type GitHubExecutor struct {
client *GitHubClient
agentToken string
}
func NewGitHubExecutor(client *GitHubClient, agentToken string) GitHubExecutor {
return GitHubExecutor{client: client, agentToken: agentToken}
}
func (e *GitHubExecutor) Authorize(req *http.Request) error {
auth := req.Header.Get("Authorization")
if auth != "Bearer "+e.agentToken {
return fmt.Errorf("invalid agent token")
}
return nil
}
func (e *GitHubExecutor) Execute(args executor.ExecuteTemplateArgs) executor.ExecuteTemplateReply {
pluginJSON, err := args.Template.Plugin.MarshalJSON()
if err != nil {
err = fmt.Errorf("failed to marshal plugin to JSON from workflow spec: %w", err)
log.Println(err.Error())
return errorResponse(err)
}
plugin := &PluginSpec{}
err = json.Unmarshal(pluginJSON, plugin)
if err != nil {
err = fmt.Errorf("failed to unmarshal plugin JSON to plugin struct: %w", err)
log.Println(err.Error())
return errorResponse(err)
}
if plugin.GitHub == nil {
return executor.ExecuteTemplateReply{} // unsupported plugin
}
output, err := e.runAction(plugin)
if err != nil {
return failedResponse(wfv1.Progress(fmt.Sprintf("0/1")), fmt.Errorf("action failed: %w", err))
}
outPtr := &output
return executor.ExecuteTemplateReply{
Node: &wfv1.NodeResult{
Phase: wfv1.NodeSucceeded,
Message: "Action completed",
Progress: "1/1",
Outputs: &wfv1.Outputs{
Result: outPtr,
},
},
}
}
func (e *GitHubExecutor) runAction(plugin *PluginSpec) (string, error) {
ctx, cancel, err := durationStringToContext(plugin.GitHub.Timeout)
if err != nil {
return "", fmt.Errorf("failed to parse timeout: %w", err)
}
defer cancel()
var response *github.Response
var expectedResponseCode int
if plugin.GitHub.Issue != nil {
response, expectedResponseCode, err = e.runIssueAction(ctx, plugin.GitHub.Issue)
} else if plugin.GitHub.Check != nil {
response, expectedResponseCode, err = e.runCheckAction(ctx, plugin.GitHub.Check)
} else {
return "", fmt.Errorf("unsupported action")
}
if err != nil {
return "", fmt.Errorf("failed to run action: %w", err)
}
if response.StatusCode != expectedResponseCode {
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
return "", fmt.Errorf("expected response code %d but got %d: %s", expectedResponseCode, response.StatusCode, string(responseBody))
}
return "", nil
}
func (e *GitHubExecutor) runIssueAction(ctx context.Context, issueAction *IssueActionSpec) (*github.Response, int, error) {
if err := validateIssueAction(issueAction); err != nil {
return nil, 0, fmt.Errorf("failed to validate issue action: %w", err)
}
if issueAction.Comment != nil {
body, owner, repo, number, err := validateIssueCreateCommentAction(issueAction.Comment)
if err != nil {
return nil, 0, fmt.Errorf("invalid issue comment action: %w", err)
}
_, response, err := e.client.Issues.CreateComment(ctx, owner, repo, number, &github.IssueComment{
Body: &body,
})
return response, 201, err
} else if issueAction.Create != nil {
if err := validateIssueCreateAction(issueAction.Create); err != nil {
return nil, 0, fmt.Errorf("invalid issue create action: %w", err)
}
_, response, err := e.client.Issues.Create(ctx, issueAction.Create.Owner, issueAction.Create.Repo, issueAction.Create.Request)
return response, 201, err
}
return nil, 0, fmt.Errorf("unsupported issue action")
}
func (e *GitHubExecutor) runCheckAction(ctx context.Context, checkAction *CheckActionSpec) (*github.Response, int, error) {
if err := validateCheckAction(checkAction); err != nil {
return nil, 0, fmt.Errorf("failed to validate check action: %w", err)
}
_, response, err := e.client.Checks.CreateCheckRun(ctx, checkAction.Create.Owner, checkAction.Create.Repo, checkAction.Create.Request)
return response, 201, err
}
func validateCheckAction(action *CheckActionSpec) error {
if action.Create == nil {
return err
}
if action.Create.Repo == "" {
return err
}
if action.Create.
}
func validateIssueAction(action *IssueActionSpec) error {
if action.Comment == nil && action.Create == nil {
return fmt.Errorf("the only available issue actions are 'comment' and 'create")
}
if action.Comment != nil && action.Create != nil {
return fmt.Errorf("only one issue action can be specified")
}
return nil
}
func validateIssueCreateCommentAction(action *IssueCommentAction) (body, owner, repo string, number int, err error) {
if action.Body == "" {
return "", "", "", -1, fmt.Errorf("the issue comment body is required")
}
if action.Owner == "" {
return "", "", "", -1, fmt.Errorf("the issue owner is required")
}
if action.Repo == "" {
return "", "", "", -1, fmt.Errorf("the issue repo is required")
}
if action.Number == "" {
return "", "", "", -1, fmt.Errorf("the issue number is required")
}
number, err = strconv.Atoi(action.Number)
if err != nil {
return "", "", "", -1, fmt.Errorf("the issue number must be an integer")
}
if number < 0 {
return "", "", "", -1, fmt.Errorf("the issue number must be greater than or equal to 0")
}
return action.Body, action.Owner, action.Repo, number, nil
}
func validateIssueCreateAction(action *IssueCreateAction) error {
if action.Owner == "" {
return fmt.Errorf("the issue owner is required")
}
if action.Repo == "" {
return fmt.Errorf("the issue repo is required")
}
return nil
}
// durationStringToContext parses a duration string and returns a context and cancel function. If timeout is empty, the
// context is context.Background().
func durationStringToContext(timeout string) (ctx context.Context, cancel func(), err error) {
ctx = context.Background()
cancel = func() {}
if timeout != "" {
duration, err := time.ParseDuration(timeout)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse timeout: %w", err)
}
ctx, cancel = context.WithTimeout(ctx, duration)
}
return ctx, cancel, nil
}
func errorResponse(err error) executor.ExecuteTemplateReply {
return executor.ExecuteTemplateReply{
Node: &wfv1.NodeResult{
Phase: wfv1.NodeError,
Message: err.Error(),
Progress: wfv1.ProgressZero,
},
}
}
func failedResponse(progress wfv1.Progress, err error) executor.ExecuteTemplateReply {
return executor.ExecuteTemplateReply{
Node: &wfv1.NodeResult{
Phase: wfv1.NodeFailed,
Message: err.Error(),
Progress: progress,
},
}
}