Skip to content

Commit 0ebb864

Browse files
committed
refactor: precompile regex, extract curl helper, and replace custom contains with strings.Contains
1 parent e1ee75c commit 0ebb864

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

internal/git/common_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"os/exec"
7+
"strings"
78
"testing"
89

910
"github.com/somaz94/go-git-commit-action/internal/config"
@@ -397,26 +398,13 @@ func TestBuildTagDescription(t *testing.T) {
397398
},
398399
}
399400
desc := tm.buildTagDescription(tt.targetCommit)
400-
if !contains(desc, tt.wantContains) {
401+
if !strings.Contains(desc, tt.wantContains) {
401402
t.Errorf("buildTagDescription() = %q, want to contain %q", desc, tt.wantContains)
402403
}
403404
})
404405
}
405406
}
406407

407-
func contains(s, substr string) bool {
408-
return len(s) >= len(substr) && searchSubstring(s, substr)
409-
}
410-
411-
func searchSubstring(s, substr string) bool {
412-
for i := 0; i <= len(s)-len(substr); i++ {
413-
if s[i:i+len(substr)] == substr {
414-
return true
415-
}
416-
}
417-
return false
418-
}
419-
420408
func TestPrintChangeDetectionInfo(t *testing.T) {
421409
// This function only prints, so we just verify it doesn't panic
422410
printChangeDetectionInfo([]byte("M file.txt"), []byte("file.txt"), true, true)

internal/github/client.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const (
1919
curlMaxTimeSec = "30"
2020
)
2121

22+
var statusCodePattern = regexp.MustCompile(`^\d{3}$`)
23+
2224
// Client handles GitHub API interactions.
2325
type 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.
4749
func (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.
8593
func 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

Comments
 (0)