Skip to content

Commit a0d5445

Browse files
pr review and fixed bug on creation of yaml file
1 parent 47057f0 commit a0d5445

File tree

2 files changed

+111
-4
lines changed

2 files changed

+111
-4
lines changed

tools/language_config.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"os"
99
"path/filepath"
10+
"slices"
1011
"strings"
1112
"time"
1213

@@ -15,7 +16,7 @@ import (
1516
"gopkg.in/yaml.v3"
1617
)
1718

18-
const CodacyApiBase = "https://app.codacy.com"
19+
var CodacyApiBase = "https://app.codacy.com"
1920

2021
// ToolLanguageInfo contains language and extension information for a tool
2122
type ToolLanguageInfo struct {
@@ -91,17 +92,26 @@ func CreateLanguagesConfigFile(apiTools []Tool, toolsConfigDir string, toolIDMap
9192

9293
// Filter languages based on repository languages
9394
var filteredLanguages []string
95+
var filteredExtensionsSet = make(map[string]struct{})
9496
for _, lang := range langInfo.Languages {
95-
// Convert both to lowercase for case-insensitive comparison
9697
lowerLang := strings.ToLower(lang)
9798
if extensions, exists := repositoryLanguages[lowerLang]; exists && len(extensions) > 0 {
9899
filteredLanguages = append(filteredLanguages, lang)
100+
for _, ext := range extensions {
101+
filteredExtensionsSet[ext] = struct{}{}
102+
}
99103
}
100104
}
105+
filteredExtensions := make([]string, 0, len(filteredExtensionsSet))
106+
for ext := range filteredExtensionsSet {
107+
filteredExtensions = append(filteredExtensions, ext)
108+
}
109+
slices.Sort(filteredExtensions)
110+
langInfo.Languages = filteredLanguages
111+
langInfo.Extensions = filteredExtensions
101112

102113
// Only add tool if it has languages that exist in the repository
103114
if len(filteredLanguages) > 0 {
104-
langInfo.Languages = filteredLanguages
105115
configTools = append(configTools, langInfo)
106116
}
107117
}
@@ -135,7 +145,6 @@ func CreateLanguagesConfigFile(apiTools []Tool, toolsConfigDir string, toolIDMap
135145
return nil
136146
}
137147

138-
// https://app.codacy.com/api/v3/organizations/gh/troubleshoot-codacy/repositories/eslint-test-examples/settings/languages
139148
func getRepositoryLanguages(apiToken string, provider string, organization string, repository string) (map[string][]string, error) {
140149
client := &http.Client{
141150
Timeout: 10 * time.Second,
@@ -212,6 +221,9 @@ func getRepositoryLanguages(apiToken string, provider string, organization strin
212221
extSlice = append(extSlice, ext)
213222
}
214223

224+
// Sort extensions for consistent ordering in the config file
225+
slices.Sort(extSlice)
226+
215227
// Add to result map with lowercase key for case-insensitive matching
216228
result[strings.ToLower(lang.Name)] = extSlice
217229
}

tools/language_config_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"io"
77
"net/http"
88
"net/http/httptest"
9+
"os"
910
"sort"
1011
"testing"
1112
"time"
1213

1314
"github.com/stretchr/testify/assert"
15+
"gopkg.in/yaml.v3"
1416
)
1517

1618
func TestGetRepositoryLanguages(t *testing.T) {
@@ -220,3 +222,96 @@ func TestGetRepositoryLanguages(t *testing.T) {
220222
})
221223
}
222224
}
225+
226+
func TestCreateLanguagesConfigFile_ExtensionsFromRepository(t *testing.T) {
227+
tempDir := t.TempDir()
228+
229+
// Mock API server for getRepositoryLanguages
230+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
231+
w.Header().Set("Content-Type", "application/json")
232+
json.NewEncoder(w).Encode(map[string]interface{}{
233+
"languages": []map[string]interface{}{
234+
{
235+
"name": "JavaScript",
236+
"codacyDefaults": []string{".js", ".jsx"},
237+
"extensions": []string{".js", ".vue"},
238+
"enabled": true,
239+
"detected": true,
240+
},
241+
{
242+
"name": "Python",
243+
"codacyDefaults": []string{".py"},
244+
"extensions": []string{".testPy"},
245+
"enabled": true,
246+
"detected": true,
247+
},
248+
{
249+
"name": "Apex",
250+
"codacyDefaults": []string{".cls"},
251+
"extensions": []string{".app", ".trigger"},
252+
"enabled": true,
253+
"detected": true,
254+
},
255+
{
256+
"name": "Scala",
257+
"codacyDefaults": []string{".scala"},
258+
"extensions": []string{},
259+
"enabled": true,
260+
"detected": true,
261+
},
262+
{
263+
"name": "Ruby",
264+
"codacyDefaults": []string{".rb"},
265+
"extensions": []string{".gemspec"},
266+
"enabled": true,
267+
"detected": true,
268+
},
269+
},
270+
})
271+
}))
272+
defer server.Close()
273+
274+
// Patch CodacyApiBase to use the test server
275+
oldBase := CodacyApiBase
276+
CodacyApiBase = server.URL
277+
defer func() { CodacyApiBase = oldBase }()
278+
279+
apiTools := []Tool{
280+
{Uuid: "eslint-uuid"},
281+
{Uuid: "pylint-uuid"},
282+
{Uuid: "pmd-uuid"},
283+
}
284+
toolIDMap := map[string]string{
285+
"eslint-uuid": "eslint",
286+
"pylint-uuid": "pylint",
287+
"pmd-uuid": "pmd",
288+
}
289+
290+
err := CreateLanguagesConfigFile(apiTools, tempDir, toolIDMap, "test-token", "gh", "org", "repo")
291+
assert.NoError(t, err)
292+
293+
// Read and unmarshal the generated YAML
294+
data, err := os.ReadFile(tempDir + "/languages-config.yaml")
295+
assert.NoError(t, err)
296+
297+
var config LanguagesConfig
298+
err = yaml.Unmarshal(data, &config)
299+
assert.NoError(t, err)
300+
301+
// Check that extensions are correct for each tool
302+
eslint := findTool(config.Tools, "eslint")
303+
assert.ElementsMatch(t, []string{".js", ".jsx", ".vue"}, eslint.Extensions)
304+
pylint := findTool(config.Tools, "pylint")
305+
assert.ElementsMatch(t, []string{".py", ".testPy"}, pylint.Extensions)
306+
pmd := findTool(config.Tools, "pmd")
307+
assert.ElementsMatch(t, []string{".cls", ".app", ".trigger", ".scala", ".rb", ".gemspec"}, pmd.Extensions)
308+
}
309+
310+
func findTool(tools []ToolLanguageInfo, name string) ToolLanguageInfo {
311+
for _, t := range tools {
312+
if t.Name == name {
313+
return t
314+
}
315+
}
316+
return ToolLanguageInfo{}
317+
}

0 commit comments

Comments
 (0)