11package native
22
33import (
4- "bytes"
54 "context"
65 "encoding/json"
76 "fmt"
@@ -70,24 +69,6 @@ func githubPullRequestDiff(ctx context.Context, with map[string]any) (map[string
7069 return map [string ]any {"diff" : text }, nil
7170}
7271
73- func githubPullRequestPostComment (ctx context.Context , owner , repo , number , body string ) (map [string ]any , error ) {
74- path := fmt .Sprintf ("/repos/%s/%s/issues/%s/comments" , url .PathEscape (owner ), url .PathEscape (repo ), url .PathEscape (number ))
75- payload := map [string ]string {"body" : body }
76- b , err := githubPOSTJSON (ctx , path , payload , maxGitHubJSONBody )
77- if err != nil {
78- return nil , err
79- }
80- var out map [string ]any
81- if err := json .Unmarshal (b , & out ); err != nil {
82- return nil , fmt .Errorf ("native: pull_request.post_comment decode: %w" , err )
83- }
84- if out == nil {
85- out = map [string ]any {}
86- }
87- out ["simulated" ] = false
88- return out , nil
89- }
90-
9172// githubLivePostCommentContext reports whether step inputs request a real GitHub issue comment:
9273// non-empty owner, repo, number (or pull_number), and body. When true, post_comment uses the REST
9374// API if GITHUB_TOKEN is set; otherwise the demo stays fully offline (simulated).
@@ -118,32 +99,14 @@ func tryStringFromWith(with map[string]any, keys ...string) (string, bool) {
11899}
119100
120101func githubPOSTJSON (ctx context.Context , path string , payload any , maxResp int64 ) ([]byte , error ) {
121- token , err := githubToken ()
122- if err != nil {
123- return nil , err
124- }
125- bodyBytes , err := json .Marshal (payload )
126- if err != nil {
127- return nil , fmt .Errorf ("native: github encode body: %w" , err )
128- }
129- fullURL := strings .TrimSuffix (githubAPIBase (), "/" ) + path
130- req , err := http .NewRequestWithContext (ctx , http .MethodPost , fullURL , bytes .NewReader (bodyBytes ))
131- if err != nil {
132- return nil , err
133- }
134- req .Header .Set ("Authorization" , "Bearer " + token )
135- req .Header .Set ("User-Agent" , githubUserAgent )
136- req .Header .Set ("Accept" , githubAcceptJSON )
137- req .Header .Set ("Content-Type" , githubAcceptJSON )
138- req .Header .Set ("X-GitHub-Api-Version" , githubAPIVersion )
102+ return githubJSONRequest (ctx , http .MethodPost , path , payload , maxResp )
103+ }
139104
140- cli := & http.Client {Timeout : 60 * time .Second }
141- resp , err := cli .Do (req )
142- if err != nil {
143- return nil , fmt .Errorf ("native: github request: %w" , err )
144- }
145- defer resp .Body .Close ()
105+ func defaultGitHubHTTPClient () * http.Client {
106+ return & http.Client {Timeout : 60 * time .Second }
107+ }
146108
109+ func readGitHubResponseBody (resp * http.Response , maxResp int64 ) ([]byte , error ) {
147110 limited := io .LimitReader (resp .Body , maxResp + 1 )
148111 b , err := io .ReadAll (limited )
149112 if err != nil {
@@ -152,9 +115,6 @@ func githubPOSTJSON(ctx context.Context, path string, payload any, maxResp int64
152115 if int64 (len (b )) > maxResp {
153116 return nil , fmt .Errorf ("native: github response body exceeds limit (%d bytes)" , maxResp )
154117 }
155- if resp .StatusCode < 200 || resp .StatusCode >= 300 {
156- return nil , fmt .Errorf ("native: github HTTP %s: %s" , resp .Status , truncateRunes (string (b ), 512 ))
157- }
158118 return b , nil
159119}
160120
@@ -269,20 +229,16 @@ func githubRequestBody(ctx context.Context, method, path, accept string, maxBody
269229 req .Header .Set ("X-GitHub-Api-Version" , githubAPIVersion )
270230 }
271231
272- cli := & http. Client { Timeout : 60 * time . Second }
232+ cli := defaultGitHubHTTPClient ()
273233 resp , err := cli .Do (req )
274234 if err != nil {
275235 return nil , fmt .Errorf ("native: github request: %w" , err )
276236 }
277237 defer resp .Body .Close ()
278238
279- limited := io .LimitReader (resp .Body , maxBody + 1 )
280- b , err := io .ReadAll (limited )
239+ b , err := readGitHubResponseBody (resp , maxBody )
281240 if err != nil {
282- return nil , fmt .Errorf ("native: github read body: %w" , err )
283- }
284- if int64 (len (b )) > maxBody {
285- return nil , fmt .Errorf ("native: github response body exceeds limit (%d bytes)" , maxBody )
241+ return nil , err
286242 }
287243 if resp .StatusCode < 200 || resp .StatusCode >= 300 {
288244 return nil , fmt .Errorf ("native: github HTTP %s: %s" , resp .Status , truncateRunes (string (b ), 512 ))
0 commit comments