Skip to content

Commit fc611e8

Browse files
support use of codacy api token
1 parent 6ed461d commit fc611e8

File tree

5 files changed

+142
-8
lines changed

5 files changed

+142
-8
lines changed

.codacy/cli-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mode: local
1+
mode: remote

.codacy/codacy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ runtimes:
22
33
44
tools:
5-
- eslint@9.3.0
5+
- eslint@8.57.0
66
77
88

cmd/init.go

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import (
1818
const CodacyApiBase = "https://app.codacy.com"
1919

2020
var codacyRepositoryToken string
21+
var codacyApiToken string
2122

2223
func init() {
2324
initCmd.Flags().StringVar(&codacyRepositoryToken, "repository-token", "", "optional codacy repository token, if defined configurations will be fetched from codacy")
25+
initCmd.Flags().StringVar(&codacyApiToken, "codacy-api-token", "", "optional codacy api token, if defined configurations will be fetched from codacy")
2426
rootCmd.AddCommand(initCmd)
2527
}
2628

@@ -29,10 +31,9 @@ var initCmd = &cobra.Command{
2931
Short: "Bootstraps project configuration",
3032
Long: "Bootstraps project configuration, creates codacy configuration file",
3133
Run: func(cmd *cobra.Command, args []string) {
32-
3334
config.Config.CreateLocalCodacyDir()
3435

35-
cliLocalMode := len(codacyRepositoryToken) == 0
36+
cliLocalMode := len(codacyRepositoryToken) == 0 && len(codacyApiToken) == 0
3637

3738
if cliLocalMode {
3839
fmt.Println()
@@ -51,7 +52,15 @@ var initCmd = &cobra.Command{
5152
if err != nil {
5253
log.Fatal(err)
5354
}
54-
err = buildRepositoryConfigurationFiles(codacyRepositoryToken)
55+
56+
var token Token
57+
if len(codacyApiToken) > 0 {
58+
token = ApiToken{value: codacyApiToken}
59+
} else {
60+
token = ProjectToken{value: codacyRepositoryToken}
61+
}
62+
63+
err = buildRepositoryConfigurationFiles(token)
5564
if err != nil {
5665
log.Fatal(err)
5766
}
@@ -138,9 +147,112 @@ func cliConfigFileTemplate(cliLocalMode bool) string {
138147
return fmt.Sprintf(`mode: %s`, cliModeString)
139148
}
140149

141-
func buildRepositoryConfigurationFiles(token string) error {
150+
func buildRepositoryConfigurationFiles(token Token) error {
142151
fmt.Println("Building repository configuration files ...")
143-
fmt.Println("Fetching repository configuration from codacy ...")
152+
switch token := token.(type) {
153+
case ProjectToken:
154+
return buildRepositoryConfigurationFilesFromRepositoryToken(token)
155+
case ApiToken:
156+
return buildRepositoryConfigurationFilesFromApiToken(token)
157+
default:
158+
return fmt.Errorf("unknown token type: %T", token)
159+
}
160+
}
161+
162+
func buildRepositoryConfigurationFilesFromApiToken(token ApiToken) error {
163+
fmt.Println("Fetching repository configuration from codacy using api token ...")
164+
165+
// Ask for provider
166+
var provider string
167+
for {
168+
fmt.Print("Enter provider (gh/bb/gl): ")
169+
fmt.Scanln(&provider)
170+
if provider == "gh" || provider == "bb" || provider == "gl" {
171+
break
172+
}
173+
fmt.Println("Invalid provider. Please enter 'gh', 'bb', or 'gl'")
174+
}
175+
176+
// Ask for organization name
177+
var organizationName string
178+
fmt.Print("Enter remote organization name: ")
179+
fmt.Scanln(&organizationName)
180+
181+
// Ask for repository name
182+
var repositoryName string
183+
fmt.Print("Enter remote repository name: ")
184+
fmt.Scanln(&repositoryName)
185+
186+
var supportedTools = []string{
187+
"f8b29663-2cb2-498d-b923-a10c6a8c05cd", // ESLint
188+
"2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb", // Trivy
189+
"31677b6d-4ae0-4f56-8041-606a8d7a8e61", // Pylint
190+
"9ed24812-b6ee-4a58-9004-0ed183c45b8f", // PMD
191+
}
192+
193+
client := &http.Client{
194+
Timeout: 10 * time.Second,
195+
}
196+
197+
for _, toolUuid := range supportedTools {
198+
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns",
199+
CodacyApiBase,
200+
provider,
201+
organizationName,
202+
repositoryName,
203+
toolUuid)
204+
205+
// Create a new GET request
206+
req, err := http.NewRequest("GET", url, nil)
207+
if err != nil {
208+
fmt.Println("Error:", err)
209+
return err
210+
}
211+
212+
// Set the headers
213+
req.Header.Set("api-token", token.Value())
214+
215+
// Send the request
216+
resp, err := client.Do(req)
217+
if err != nil {
218+
fmt.Println("Error:", err)
219+
return err
220+
}
221+
defer resp.Body.Close()
222+
223+
if resp.StatusCode >= 400 {
224+
return errors.New("failed to get repository's configuration from Codacy API")
225+
}
226+
227+
// Read the response body
228+
body, err := io.ReadAll(resp.Body)
229+
if err != nil {
230+
fmt.Println("Error:", err)
231+
return err
232+
}
233+
234+
var objmap map[string]json.RawMessage
235+
err = json.Unmarshal(body, &objmap)
236+
if err != nil {
237+
fmt.Println("Error unmarshaling response:", err)
238+
return err
239+
}
240+
241+
var apiToolConfigurations []CodacyToolConfiguration
242+
err = json.Unmarshal(objmap["toolConfiguration"], &apiToolConfigurations)
243+
if err != nil {
244+
fmt.Println("Error unmarshaling tool configurations:", err)
245+
return err
246+
}
247+
248+
// TODO: Process the response and create configuration files for each tool
249+
}
250+
251+
return nil
252+
}
253+
254+
func buildRepositoryConfigurationFilesFromRepositoryToken(token ProjectToken) error {
255+
fmt.Println("Fetching repository configuration from codacy using repository token ...")
144256

145257
// API call to fetch settings
146258
url := CodacyApiBase + "/2.0/project/analysis/configuration"
@@ -157,7 +269,7 @@ func buildRepositoryConfigurationFiles(token string) error {
157269
}
158270

159271
// Set the headers
160-
req.Header.Set("project-token", token)
272+
req.Header.Set("project-token", token.Value())
161273

162274
// Send the request
163275
resp, err := client.Do(req)

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func showWelcomeMessage() {
4848
fmt.Println()
4949
cyan.Println("Initialize your project with:")
5050
fmt.Println(" codacy-cli init --repository-token YOUR_TOKEN")
51+
fmt.Println(" codacy-cli init --codacy-api-token YOUR_TOKEN")
5152
fmt.Println()
5253
fmt.Println("Or run without a token to use local configuration:")
5354
fmt.Println(" codacy-cli init")

cmd/token.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cmd
2+
3+
type Token interface {
4+
Value() string
5+
}
6+
7+
type ProjectToken struct {
8+
value string
9+
}
10+
11+
func (t ProjectToken) Value() string {
12+
return t.value
13+
}
14+
15+
type ApiToken struct {
16+
value string
17+
}
18+
19+
func (t ApiToken) Value() string {
20+
return t.value
21+
}

0 commit comments

Comments
 (0)