@@ -19,6 +19,8 @@ const (
1919 curlMaxTimeSec = "30"
2020)
2121
22+ var statusCodePattern = regexp .MustCompile (`^\d{3}$` )
23+
2224// Client handles GitHub API interactions.
2325type Client struct {
2426 token string
@@ -46,15 +48,9 @@ func (c *Client) Patch(endpoint string, data interface{}) (map[string]interface{
4648// GetArray sends a GET request to the GitHub API and returns an array response.
4749func (c * Client ) GetArray (endpoint string ) ([]map [string ]interface {}, error ) {
4850 url := fmt .Sprintf ("%s/repos/%s%s" , apiBaseURL , c .repo , endpoint )
51+ args := c .buildBaseArgs (url )
4952
50- cmd := exec .Command ("curl" , "-s" , "--max-time" , curlMaxTimeSec ,
51- "-w" , "\n %{http_code}" ,
52- "-H" , fmt .Sprintf ("Authorization: Bearer %s" , c .token ),
53- "-H" , fmt .Sprintf ("Accept: %s" , acceptHeader ),
54- "-H" , fmt .Sprintf ("X-GitHub-Api-Version: %s" , apiVersion ),
55- url )
56-
57- output , err := cmd .CombinedOutput ()
53+ output , err := exec .Command ("curl" , args ... ).CombinedOutput ()
5854 if err != nil {
5955 return nil , errors .New ("GitHub API GET" , err )
6056 }
@@ -81,6 +77,18 @@ func (c *Client) Repo() string {
8177 return c .repo
8278}
8379
80+ // buildBaseArgs returns common curl arguments with authentication and API headers.
81+ func (c * Client ) buildBaseArgs (url string ) []string {
82+ return []string {
83+ "-s" , "--max-time" , curlMaxTimeSec ,
84+ "-w" , "\n %{http_code}" ,
85+ "-H" , fmt .Sprintf ("Authorization: Bearer %s" , c .token ),
86+ "-H" , fmt .Sprintf ("Accept: %s" , acceptHeader ),
87+ "-H" , fmt .Sprintf ("X-GitHub-Api-Version: %s" , apiVersion ),
88+ url ,
89+ }
90+ }
91+
8492// parseHTTPResponse splits curl output (with -w "\n%{http_code}") into body and status code.
8593func parseHTTPResponse (output []byte ) ([]byte , int , error ) {
8694 raw := strings .TrimSpace (string (output ))
@@ -98,7 +106,7 @@ func parseHTTPResponse(output []byte) ([]byte, int, error) {
98106 codeStr := strings .TrimSpace (raw [lastNewline + 1 :])
99107
100108 // Validate status code is numeric (3 digits)
101- if ! regexp . MustCompile ( `^\d{3}$` ) .MatchString (codeStr ) {
109+ if ! statusCodePattern .MatchString (codeStr ) {
102110 return []byte (body ), 0 , errors .New ("parse HTTP status code" , fmt .Errorf ("invalid status code: %q" , codeStr ))
103111 }
104112
@@ -114,17 +122,10 @@ func (c *Client) request(method, endpoint string, data interface{}) (map[string]
114122 }
115123
116124 url := fmt .Sprintf ("%s/repos/%s%s" , apiBaseURL , c .repo , endpoint )
125+ args := append ([]string {"-X" , method , "-H" , "Content-Type: application/json" }, c .buildBaseArgs (url )... )
126+ args = append (args , "-d" , string (jsonData ))
117127
118- cmd := exec .Command ("curl" , "-s" , "--max-time" , curlMaxTimeSec ,
119- "-w" , "\n %{http_code}" ,
120- "-X" , method ,
121- "-H" , fmt .Sprintf ("Authorization: Bearer %s" , c .token ),
122- "-H" , fmt .Sprintf ("Accept: %s" , acceptHeader ),
123- "-H" , "Content-Type: application/json" ,
124- url ,
125- "-d" , string (jsonData ))
126-
127- output , err := cmd .CombinedOutput ()
128+ output , err := exec .Command ("curl" , args ... ).CombinedOutput ()
128129 if err != nil {
129130 return nil , errors .New ("GitHub API " + method , err )
130131 }
0 commit comments