-
Notifications
You must be signed in to change notification settings - Fork 10
breaking: support use of codacy api token - PLUTO-1379 #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
fc611e8
b8b9eae
ed511ef
e8fada6
26ba75c
b3932f5
b4bfe46
eff63cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| mode: local | ||
| mode: remote |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ runtimes: | |
| - [email protected] | ||
| - [email protected] | ||
| tools: | ||
| - eslint@9.3.0 | ||
| - eslint@8.57.0 | ||
| - [email protected] | ||
| - [email protected] | ||
| - [email protected] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,9 +18,11 @@ | |
| const CodacyApiBase = "https://app.codacy.com" | ||
|
|
||
| var codacyRepositoryToken string | ||
| var codacyApiToken string | ||
|
|
||
| func init() { | ||
| initCmd.Flags().StringVar(&codacyRepositoryToken, "repository-token", "", "optional codacy repository token, if defined configurations will be fetched from codacy") | ||
| initCmd.Flags().StringVar(&codacyApiToken, "codacy-api-token", "", "optional codacy api token, if defined configurations will be fetched from codacy") | ||
| rootCmd.AddCommand(initCmd) | ||
| } | ||
|
|
||
|
|
@@ -29,10 +31,9 @@ | |
| Short: "Bootstraps project configuration", | ||
| Long: "Bootstraps project configuration, creates codacy configuration file", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
|
|
||
| config.Config.CreateLocalCodacyDir() | ||
|
|
||
| cliLocalMode := len(codacyRepositoryToken) == 0 | ||
| cliLocalMode := len(codacyRepositoryToken) == 0 && len(codacyApiToken) == 0 | ||
|
|
||
| if cliLocalMode { | ||
| fmt.Println() | ||
|
|
@@ -51,7 +52,15 @@ | |
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| err = buildRepositoryConfigurationFiles(codacyRepositoryToken) | ||
|
|
||
| var token Token | ||
| if len(codacyApiToken) > 0 { | ||
| token = ApiToken{value: codacyApiToken} | ||
| } else { | ||
| token = ProjectToken{value: codacyRepositoryToken} | ||
| } | ||
|
|
||
| err = buildRepositoryConfigurationFiles(token) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
@@ -138,9 +147,112 @@ | |
| return fmt.Sprintf(`mode: %s`, cliModeString) | ||
| } | ||
|
|
||
| func buildRepositoryConfigurationFiles(token string) error { | ||
| func buildRepositoryConfigurationFiles(token Token) error { | ||
| fmt.Println("Building repository configuration files ...") | ||
| fmt.Println("Fetching repository configuration from codacy ...") | ||
| switch token := token.(type) { | ||
| case ProjectToken: | ||
| return buildRepositoryConfigurationFilesFromRepositoryToken(token) | ||
| case ApiToken: | ||
| return buildRepositoryConfigurationFilesFromApiToken(token) | ||
| default: | ||
| return fmt.Errorf("unknown token type: %T", token) | ||
| } | ||
| } | ||
|
|
||
| func buildRepositoryConfigurationFilesFromApiToken(token ApiToken) error { | ||
|
Check warning on line 162 in cmd/init.go
|
||
| fmt.Println("Fetching repository configuration from codacy using api token ...") | ||
|
|
||
| // Ask for provider | ||
| var provider string | ||
| for { | ||
| fmt.Print("Enter provider (gh/bb/gl): ") | ||
| fmt.Scanln(&provider) | ||
| if provider == "gh" || provider == "bb" || provider == "gl" { | ||
| break | ||
| } | ||
| fmt.Println("Invalid provider. Please enter 'gh', 'bb', or 'gl'") | ||
| } | ||
|
|
||
| // Ask for organization name | ||
| var organizationName string | ||
| fmt.Print("Enter remote organization name: ") | ||
| fmt.Scanln(&organizationName) | ||
|
|
||
| // Ask for repository name | ||
| var repositoryName string | ||
| fmt.Print("Enter remote repository name: ") | ||
| fmt.Scanln(&repositoryName) | ||
|
|
||
| var supportedTools = []string{ | ||
| "f8b29663-2cb2-498d-b923-a10c6a8c05cd", // ESLint | ||
| "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb", // Trivy | ||
| "31677b6d-4ae0-4f56-8041-606a8d7a8e61", // Pylint | ||
| "9ed24812-b6ee-4a58-9004-0ed183c45b8f", // PMD | ||
| } | ||
|
|
||
| client := &http.Client{ | ||
| Timeout: 10 * time.Second, | ||
| } | ||
|
|
||
| for _, toolUuid := range supportedTools { | ||
| url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns", | ||
| CodacyApiBase, | ||
| provider, | ||
| organizationName, | ||
| repositoryName, | ||
| toolUuid) | ||
|
|
||
| // Create a new GET request | ||
| req, err := http.NewRequest("GET", url, nil) | ||
| if err != nil { | ||
| fmt.Println("Error:", err) | ||
| return err | ||
| } | ||
|
|
||
| // Set the headers | ||
| req.Header.Set("api-token", token.Value()) | ||
|
|
||
| // Send the request | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| fmt.Println("Error:", err) | ||
| return err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode >= 400 { | ||
| return errors.New("failed to get repository's configuration from Codacy API") | ||
| } | ||
|
|
||
| // Read the response body | ||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| fmt.Println("Error:", err) | ||
| return err | ||
| } | ||
|
|
||
| var objmap map[string]json.RawMessage | ||
| err = json.Unmarshal(body, &objmap) | ||
| if err != nil { | ||
| fmt.Println("Error unmarshaling response:", err) | ||
| return err | ||
| } | ||
|
|
||
| var apiToolConfigurations []CodacyToolConfiguration | ||
| err = json.Unmarshal(objmap["toolConfiguration"], &apiToolConfigurations) | ||
| if err != nil { | ||
| fmt.Println("Error unmarshaling tool configurations:", err) | ||
| return err | ||
| } | ||
|
|
||
| // TODO: Process the response and create configuration files for each tool | ||
|
||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func buildRepositoryConfigurationFilesFromRepositoryToken(token ProjectToken) error { | ||
|
Check warning on line 254 in cmd/init.go
|
||
| fmt.Println("Fetching repository configuration from codacy using repository token ...") | ||
|
|
||
| // API call to fetch settings | ||
| url := CodacyApiBase + "/2.0/project/analysis/configuration" | ||
|
|
@@ -157,7 +269,7 @@ | |
| } | ||
|
|
||
| // Set the headers | ||
| req.Header.Set("project-token", token) | ||
| req.Header.Set("project-token", token.Value()) | ||
|
|
||
| // Send the request | ||
| resp, err := client.Do(req) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package cmd | ||
|
|
||
| type Token interface { | ||
| Value() string | ||
| } | ||
|
|
||
| type ProjectToken struct { | ||
| value string | ||
| } | ||
|
|
||
| func (t ProjectToken) Value() string { | ||
| return t.value | ||
| } | ||
|
|
||
| type ApiToken struct { | ||
|
Check notice on line 15 in cmd/token.go
|
||
| value string | ||
| } | ||
|
|
||
| func (t ApiToken) Value() string { | ||
| return t.value | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can simplify it