-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
247 lines (227 loc) · 7.34 KB
/
github.go
File metadata and controls
247 lines (227 loc) · 7.34 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package native
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
const (
githubAcceptJSON = "application/vnd.github+json"
githubAcceptDiff = "application/vnd.github.diff"
githubAPIVersion = "2022-11-28"
githubUserAgent = "agentic-control-plane/agentctl (native-github-read)"
maxGitHubJSONBody = 8 << 20 // 8 MiB
maxGitHubDiffBody = 32 << 20 // 32 MiB
defaultGitHubAPIBase = "https://api.github.com"
)
func githubAPIBase() string {
u := strings.TrimSpace(os.Getenv("GITHUB_API_URL"))
if u == "" {
return defaultGitHubAPIBase
}
return strings.TrimSuffix(u, "/")
}
func githubToken() (string, error) {
t := strings.TrimSpace(os.Getenv("GITHUB_TOKEN"))
if t == "" {
return "", fmt.Errorf("native: GITHUB_TOKEN is not set (required for GitHub API operations)")
}
return t, nil
}
func githubPullRequestGet(ctx context.Context, with map[string]any) (map[string]any, error) {
owner, repo, number, err := githubRepoTriplet(with)
if err != nil {
return nil, err
}
path := fmt.Sprintf("/repos/%s/%s/pulls/%s", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(number))
b, err := githubGET(ctx, path, githubAcceptJSON, maxGitHubJSONBody)
if err != nil {
return nil, err
}
var pr map[string]any
if err := json.Unmarshal(b, &pr); err != nil {
return nil, fmt.Errorf("native: pull_request.get decode: %w", err)
}
return map[string]any{"pull_request": pr}, nil
}
func githubPullRequestDiff(ctx context.Context, with map[string]any) (map[string]any, error) {
owner, repo, number, err := githubRepoTriplet(with)
if err != nil {
return nil, err
}
path := fmt.Sprintf("/repos/%s/%s/pulls/%s", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(number))
text, err := githubGETString(ctx, path, githubAcceptDiff, maxGitHubDiffBody)
if err != nil {
return nil, err
}
return map[string]any{"diff": text}, nil
}
// githubLivePostCommentContext reports whether step inputs request a real GitHub issue comment:
// non-empty owner, repo, number (or pull_number), and body. When true, post_comment uses the REST
// API if GITHUB_TOKEN is set; otherwise the demo stays fully offline (simulated).
func githubLivePostCommentContext(with map[string]any) (owner, repo, number, body string, wantLive bool) {
owner, ok1 := tryStringFromWith(with, "owner")
repo, ok2 := tryStringFromWith(with, "repo")
num, ok3 := tryStringFromWith(with, "number", "pull_number")
bodyStr, ok4 := tryStringFromWith(with, "body")
if !ok1 || !ok2 || !ok3 || !ok4 {
return "", "", "", "", false
}
return owner, repo, num, bodyStr, true
}
func tryStringFromWith(with map[string]any, keys ...string) (string, bool) {
for _, k := range keys {
v, ok := with[k]
if !ok || v == nil {
continue
}
s, err := scalarToString(v)
if err != nil || strings.TrimSpace(s) == "" {
continue
}
return strings.TrimSpace(s), true
}
return "", false
}
func githubPOSTJSON(ctx context.Context, path string, payload any, maxResp int64) ([]byte, error) {
return githubJSONRequest(ctx, http.MethodPost, path, payload, maxResp)
}
func defaultGitHubHTTPClient() *http.Client {
return &http.Client{Timeout: 60 * time.Second}
}
func readGitHubResponseBody(resp *http.Response, maxResp int64) ([]byte, error) {
limited := io.LimitReader(resp.Body, maxResp+1)
b, err := io.ReadAll(limited)
if err != nil {
return nil, fmt.Errorf("native: github read body: %w", err)
}
if int64(len(b)) > maxResp {
return nil, fmt.Errorf("native: github response body exceeds limit (%d bytes)", maxResp)
}
return b, nil
}
func githubCheckRunsList(ctx context.Context, with map[string]any) (map[string]any, error) {
owner, err := stringFromWith(with, "owner")
if err != nil {
return nil, fmt.Errorf("native: check_runs.list %w", err)
}
repo, err := stringFromWith(with, "repo")
if err != nil {
return nil, fmt.Errorf("native: check_runs.list %w", err)
}
ref, err := stringFromWith(with, "ref", "head_sha", "sha")
if err != nil {
return nil, fmt.Errorf("native: check_runs.list requires non-empty ref, head_sha, or sha: %w", err)
}
path := fmt.Sprintf("/repos/%s/%s/commits/%s/check-runs", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(ref))
b, err := githubGET(ctx, path, githubAcceptJSON, maxGitHubJSONBody)
if err != nil {
return nil, err
}
var payload map[string]any
if err := json.Unmarshal(b, &payload); err != nil {
return nil, fmt.Errorf("native: check_runs.list decode: %w", err)
}
return payload, nil
}
func githubRepoTriplet(with map[string]any) (owner, repo, number string, err error) {
owner, err = stringFromWith(with, "owner")
if err != nil {
return "", "", "", fmt.Errorf("native: pull_request.* %w", err)
}
repo, err = stringFromWith(with, "repo")
if err != nil {
return "", "", "", fmt.Errorf("native: pull_request.* %w", err)
}
number, err = stringFromWith(with, "number", "pull_number")
if err != nil {
return "", "", "", fmt.Errorf("native: pull_request.* requires non-empty number or pull_number: %w", err)
}
return owner, repo, number, nil
}
func stringFromWith(with map[string]any, keys ...string) (string, error) {
for _, k := range keys {
v, ok := with[k]
if !ok || v == nil {
continue
}
s, err := scalarToString(v)
if err != nil {
return "", fmt.Errorf("field %q: %w", k, err)
}
if strings.TrimSpace(s) != "" {
return strings.TrimSpace(s), nil
}
}
return "", fmt.Errorf("missing or empty one of %v", keys)
}
func scalarToString(v any) (string, error) {
switch x := v.(type) {
case string:
return x, nil
case bool:
return strconv.FormatBool(x), nil
case int:
return strconv.Itoa(x), nil
case int64:
return strconv.FormatInt(x, 10), nil
case float64:
if x == float64(int64(x)) && x >= -9007199254740992 && x <= 9007199254740992 {
return strconv.FormatInt(int64(x), 10), nil
}
return strconv.FormatFloat(x, 'f', -1, 64), nil
case json.Number:
return x.String(), nil
default:
return "", fmt.Errorf("unsupported type %T", v)
}
}
func githubGET(ctx context.Context, path, accept string, maxBody int64) ([]byte, error) {
return githubRequestBody(ctx, http.MethodGet, path, accept, maxBody)
}
func githubGETString(ctx context.Context, path, accept string, maxBody int64) (string, error) {
b, err := githubRequestBody(ctx, http.MethodGet, path, accept, maxBody)
if err != nil {
return "", err
}
return string(b), nil
}
func githubRequestBody(ctx context.Context, method, path, accept string, maxBody int64) ([]byte, error) {
token, err := githubToken()
if err != nil {
return nil, err
}
fullURL := strings.TrimSuffix(githubAPIBase(), "/") + path
req, err := http.NewRequestWithContext(ctx, method, fullURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("User-Agent", githubUserAgent)
if strings.TrimSpace(accept) != "" {
req.Header.Set("Accept", accept)
}
if accept == githubAcceptJSON || accept == "" {
req.Header.Set("X-GitHub-Api-Version", githubAPIVersion)
}
cli := defaultGitHubHTTPClient()
resp, err := cli.Do(req)
if err != nil {
return nil, fmt.Errorf("native: github request: %w", err)
}
defer resp.Body.Close()
b, err := readGitHubResponseBody(resp, maxBody)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("native: github HTTP %s: %s", resp.Status, truncateRunes(string(b), 512))
}
return b, nil
}