-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpatterns.go
More file actions
63 lines (54 loc) · 1.68 KB
/
Copy pathpatterns.go
File metadata and controls
63 lines (54 loc) · 1.68 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
package tools
import (
"codacy/cli-v2/domain"
"codacy/cli-v2/utils/httpclient"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// FetchDefaultEnabledPatterns fetches default patterns from Codacy API for a given tool UUID
func FetchDefaultEnabledPatterns(toolUUID string) ([]domain.PatternDefinition, error) {
client, err := httpclient.New(httpclient.WithTimeout(10 * time.Second))
if err != nil {
return nil, fmt.Errorf("failed to create http client: %w", err)
}
// Fetch default patterns from Codacy API
url := fmt.Sprintf("https://app.codacy.com/api/v3/tools/%s/patterns", toolUUID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to fetch default patterns: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("failed to get default patterns from Codacy API: status %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
// Get default patterns
var apiResponse struct {
Data []domain.PatternDefinition `json:"data"`
Pagination struct {
Limit int `json:"limit"`
Total int `json:"total"`
} `json:"pagination"`
}
if err := json.Unmarshal(body, &apiResponse); err != nil {
return nil, fmt.Errorf("failed to unmarshal API response: %w", err)
}
// Filter out disabled patterns
var enabledPatterns []domain.PatternDefinition
for _, pattern := range apiResponse.Data {
if pattern.Enabled {
enabledPatterns = append(enabledPatterns, pattern)
}
}
return enabledPatterns, nil
}