-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTools.go
156 lines (131 loc) · 3.59 KB
/
getTools.go
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
package tools
import (
"codacy/cli-v2/plugins"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
func enrichToolsWithVersion(tools []Tool) ([]Tool, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}
// Create a new GET request
req, err := http.NewRequest("GET", "https://api.codacy.com/api/v3/tools", nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
// Send the request
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get tools from Codacy API: status code %d", resp.StatusCode)
}
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var response ToolsResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
// Create a map of tool UUIDs to versions
versionMap := make(map[string]string)
for _, tool := range response.Data {
versionMap[tool.Uuid] = tool.Version
}
// Enrich the input tools with versions
for i, tool := range tools {
if version, exists := versionMap[tool.Uuid]; exists {
tools[i].Version = version
}
}
return tools, nil
}
func GetRepositoryTools(codacyBase string, apiToken string, provider string, organization string, repository string) ([]Tool, error) {
client := &http.Client{
Timeout: 10 * time.Second,
}
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools",
codacyBase,
provider,
organization,
repository)
// Create a new GET request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error:", err)
return nil, err
}
// Set the headers
req.Header.Set("api-token", apiToken)
// Send the request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error:", err)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Println("Error:", url)
return nil, errors.New("failed to get repository tools from Codacy API")
}
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error:", err)
return nil, err
}
var response ToolsResponse
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Println("Error unmarshaling response:", err)
return nil, err
}
supportedTools, err := plugins.GetSupportedTools()
if err != nil {
return nil, err
}
// Filter enabled tools
var enabledTools []Tool
for _, tool := range response.Data {
if tool.Settings.Enabled {
if _, exists := supportedTools[strings.ToLower(tool.Name)]; exists {
enabledTools = append(enabledTools, tool)
}
}
}
return enrichToolsWithVersion(enabledTools)
}
type ToolsResponse struct {
Data []Tool `json:"data"`
}
type Tool struct {
Uuid string `json:"uuid"`
Name string `json:"name"`
Version string `json:"version"`
Settings struct {
Enabled bool `json:"isEnabled"`
UsesConfigFile bool `json:"hasConfigurationFile"`
} `json:"settings"`
}
// FilterToolsByConfigUsage filters out tools that use their own configuration files
// Returns only tools that need configuration to be generated for them (UsesConfigFile = false)
func FilterToolsByConfigUsage(tools []Tool) []Tool {
var filtered []Tool
for _, tool := range tools {
if !tool.Settings.UsesConfigFile {
filtered = append(filtered, tool)
} else {
fmt.Printf("Skipping config generation for %s - configured to use repo's config file\n", tool.Name)
}
}
return filtered
}