Skip to content
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

wip generation of terraform code from application code #1855

Merged
merged 8 commits into from
Dec 17, 2024

Conversation

motatoes
Copy link
Contributor

@motatoes motatoes commented Dec 14, 2024

Summary by CodeRabbit

  • New Features

    • Introduced functionality to generate Terraform code from specific GitHub issue comments.
    • Users can trigger Terraform code generation by commenting with "digger generate".
  • Bug Fixes

    • Enhanced error handling for missing or invalid project names and generation endpoint issues.
    • Improved error management during the Terraform code generation process.

Copy link
Contributor

coderabbitai bot commented Dec 14, 2024

Warning

Rate limit exceeded

@motatoes has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 54 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between eab45d5 and 900148b.

📒 Files selected for processing (1)
  • backend/controllers/github.go (4 hunks)

Walkthrough

The pull request introduces a feature for generating Terraform code in response to specific comments on GitHub issues. When the environment variable DIGGER_GENERATION_ENABLED is set to "1", comments starting with "digger generate" trigger the processing of the project name. The implementation includes a new utility function to send application code to a generation endpoint, with robust error handling for various scenarios. This enhances the interaction between GitHub comments and Terraform code generation.

Changes

File Change Summary
backend/controllers/github.go Added logic to handle Terraform code generation for issue comments when DIGGER_GENERATION_ENABLED is set.
backend/utils/ai.go Introduced GenerateTerraformCode function to send application code to a generation endpoint and retrieve generated Terraform code with error handling.

Sequence Diagram

sequenceDiagram
    participant User
    participant GitHub
    participant Digger
    participant AIEndpoint

    User->>GitHub: Comment "digger generate ProjectName"
    GitHub->>Digger: Trigger Issue Comment Event
    Digger->>Digger: Validate Generation Settings
    Digger->>AIEndpoint: Request Terraform Code Generation
    AIEndpoint-->>Digger: Return Generated Code
    Digger->>GitHub: Post Generated Code as Comment

Poem

🐰 In the garden of code, we dig and create,
With GitHub's whispers, we open the gate.
"Digger generate," the call we embrace,
Terraform blooms in this magical space!
Errors are caught, like rabbits in flight,
Building our dreams, from day into night. 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

delve-auditor bot commented Dec 14, 2024

No security or compliance issues detected. Reviewed everything up to 5fd36ee.

Security Overview
  • 🔎 Scanned files: 2 changed file(s)
Detected Code Changes
Change Type Relevant files
Enhancement ► action.yml
    Update cache paths and key configuration
► github.go
    Add terraform code generation functionality
► ai.go
    Implement terraform code generation utilities
Refactor ► github.go
    Fix import organization
Diagnostics
  • Code Scan ID: d0233504-b47c-4427-b949-7eb5c12a6049

Reply to this PR with @delve-auditor followed by a description of what change you want and we'll auto-submit a change to this PR to implement it.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🧹 Outside diff range and nitpick comments (5)
backend/utils/ai.go (4)

57-59: Add response content validation

The function should validate that the generated Terraform code is not empty before returning.

 type GeneratorResponse struct {
     Result string `json:"result"`
 }
+
+func (r GeneratorResponse) Validate() error {
+    if r.Result == "" {
+        return fmt.Errorf("generated terraform code cannot be empty")
+    }
+    return nil
+}

And use it after unmarshaling:

 err = json.Unmarshal(body, &response)
 if err != nil {
     return "", fmt.Errorf("unable to parse generator response: %v", err)
 }
+if err := response.Validate(); err != nil {
+    return "", fmt.Errorf("invalid generator response: %v", err)
+}

Line range hint 773-777: Fix duplicate error messages

The error messages for generation endpoint configuration are duplicated and incorrect. The second error message should reflect the actual error from the GenerateTerraformCode function.

 terraformCode, err := utils.GenerateTerraformCode(appCode, generationEndpoint, webhookSecret)
 if err != nil {
-    commentReporterManager.UpdateComment(fmt.Sprintf(":x: server does not have generation endpoint configured, please verify"))
-    log.Printf("server does not have generation endpoint configured, please verify")
-    return fmt.Errorf("server does not have generation endpoint configured, please verify")
+    commentReporterManager.UpdateComment(fmt.Sprintf(":x: Failed to generate Terraform code: %v", err))
+    log.Printf("Failed to generate Terraform code: %v", err)
+    return fmt.Errorf("failed to generate Terraform code: %v", err)
 }
🧰 Tools
🪛 golangci-lint (1.62.2)

1-1: : # github.com/diggerhq/digger/backend/utils [github.com/diggerhq/digger/backend/utils.test]
utils/ai.go:7:2: "github.com/diggerhq/digger/libs/digger_config" imported and not used

(typecheck)


Line range hint 779-780: Implement TODO for writing generated code

The code currently only logs the generated Terraform code instead of writing it to the project directory.

Would you like me to help implement the code to write the generated Terraform code to the project directory? This would include:

  1. Determining the correct file path
  2. Creating necessary directories
  3. Writing the file with proper permissions
  4. Error handling for file operations
🧰 Tools
🪛 golangci-lint (1.62.2)

1-1: : # github.com/diggerhq/digger/backend/utils [github.com/diggerhq/digger/backend/utils.test]
utils/ai.go:7:2: "github.com/diggerhq/digger/libs/digger_config" imported and not used

(typecheck)


Replace hardcoded application code with project name

The hardcoded "mycode" is used as input to GenerateTerraformCode which expects an application code parameter. Since we already have the project name from the command, we should use that instead of the hardcoded value.

  • In backend/controllers/github.go, replace appCode := "mycode" with appCode := projectName
🔗 Analysis chain

Line range hint 771-771: Replace hardcoded application code

The application code is hardcoded as "mycode". This should be derived from the project's actual code.


Let me search for more specific information about the application code usage.


Let me check how the application code is used in the generation process.


Let me try a different approach to find the function implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if project directory contains application code
ast-grep --pattern 'project.Dir'

Length of output: 17227


Script:

#!/bin/bash
# Search for application code references
rg -A 2 "mycode" --type go

Length of output: 255


Script:

#!/bin/bash
# Search for GenerateTerraformCode function implementation
ast-grep --pattern 'func GenerateTerraformCode($$$) {
  $$$
}'

Length of output: 64


Script:

#!/bin/bash
# Search for GenerateTerraformCode function using ripgrep
rg -A 10 "func GenerateTerraformCode" --type go

Length of output: 583

🧰 Tools
🪛 golangci-lint (1.62.2)

1-1: : # github.com/diggerhq/digger/backend/utils [github.com/diggerhq/digger/backend/utils.test]
utils/ai.go:7:2: "github.com/diggerhq/digger/libs/digger_config" imported and not used

(typecheck)

backend/controllers/github.go (1)

757-762: Enhance error messages for better user experience.

The error messages could be more descriptive to help users understand and fix the issue.

Apply this diff to improve error messages:

-				commentReporterManager.UpdateComment(fmt.Sprintf("could not find project %v in digger.yml", projectName))
+				commentReporterManager.UpdateComment(fmt.Sprintf(":x: Project '%v' not found in digger.yml. Please verify the project name and ensure it exists in your configuration.", projectName))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 706ecf2 and f9089bf.

📒 Files selected for processing (2)
  • backend/controllers/github.go (1 hunks)
  • backend/utils/ai.go (1 hunks)
🧰 Additional context used
🪛 golangci-lint (1.62.2)
backend/utils/ai.go

1-1: : # github.com/diggerhq/digger/backend/utils [github.com/diggerhq/digger/backend/utils.test]
utils/ai.go:7:2: "github.com/diggerhq/digger/libs/digger_config" imported and not used

(typecheck)

🔇 Additional comments (1)
backend/controllers/github.go (1)

773-781: ⚠️ Potential issue

Fix error handling and implement code generation response.

The code has several issues:

  1. The error message on generation failure is incorrect (shows configuration error)
  2. The generated code is only logged, not written to the project
  3. Contains commented out code

Apply this diff to fix the issues:

 			if err != nil {
-				commentReporterManager.UpdateComment(fmt.Sprintf(":x: server does not have generation endpoint configured, please verify"))
-				log.Printf("server does not have generation endpoint configured, please verify")
-				return fmt.Errorf("server does not have generation endpoint configured, please verify")
+				commentReporterManager.UpdateComment(fmt.Sprintf(":x: Failed to generate Terraform code: %v", err))
+				log.Printf("Failed to generate Terraform code: %v", err)
+				return fmt.Errorf("failed to generate Terraform code: %v", err)
 			}
-			// comment terraform code to project dir
-			//project.Dir
-			log.Printf(terraformCode)
+			err = utils.WriteGeneratedCode(project.Dir, terraformCode)
+			if err != nil {
+				commentReporterManager.UpdateComment(fmt.Sprintf(":x: Failed to write generated code: %v", err))
+				log.Printf("Failed to write generated code: %v", err)
+				return fmt.Errorf("failed to write generated code: %v", err)
+			}
+			commentReporterManager.UpdateComment(":white_check_mark: Successfully generated and wrote Terraform code")

Run the following script to verify the implementation of WriteGeneratedCode:

Comment on lines +35 to +36
client := &http.Client{}
resp, err := client.Do(req)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add timeout to HTTP client

The HTTP client should have a reasonable timeout to prevent hanging on slow responses.

-    client := &http.Client{}
+    client := &http.Client{
+        Timeout: 30 * time.Second,
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
client := &http.Client{}
resp, err := client.Do(req)
client := &http.Client{
Timeout: 30 * time.Second,
}
resp, err := client.Do(req)

Comment on lines 771 to 772
appCode := "mycode"
terraformCode, err := utils.GenerateTerraformCode(appCode, generationEndpoint, webhookSecret)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove hard-coded value and add input validation.

The code uses a hard-coded value "mycode" for the application code, which is not production-ready.

The application code should be derived from the project configuration or user input. Consider:

  1. Extracting the application code from the project configuration
  2. Adding validation for the application code

Apply this diff:

-			appCode := "mycode"
+			appCode, err := utils.ExtractApplicationCode(project)
+			if err != nil {
+				commentReporterManager.UpdateComment(fmt.Sprintf(":x: Failed to extract application code: %v", err))
+				log.Printf("Failed to extract application code: %v", err)
+				return fmt.Errorf("failed to extract application code: %v", err)
+			}
+			if appCode == "" {
+				commentReporterManager.UpdateComment(":x: Application code is empty. Please ensure the project configuration includes the required code.")
+				return fmt.Errorf("application code is empty")
+			}

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +747 to +755
// terraform code generator
if os.Getenv("DIGGER_GENERATION_ENABLED") == "1" {
if strings.HasPrefix(*payload.Comment.Body, "digger generate") {
projectName := ci.ParseProjectName(*payload.Comment.Body)
if projectName == "" {
commentReporterManager.UpdateComment(fmt.Sprintf(":x: generate requires argument -p <project_name>: %v", err))
log.Printf("missing project in command: %v", *payload.Comment.Body)
return fmt.Errorf("generate requires argument -p <project_name>: %v", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Fix error handling and improve feature flag management.

The code has the following issues:

  1. The error message in line 752 references an undefined err variable
  2. The feature flag should be defined as a constant or in configuration for better maintainability

Apply this diff to fix the error handling:

-				commentReporterManager.UpdateComment(fmt.Sprintf(":x: generate requires argument -p <project_name>: %v", err))
+				commentReporterManager.UpdateComment(":x: generate requires argument -p <project_name>")

Consider moving the feature flag to a constant or configuration:

+const (
+    FeatureFlagGeneration = "DIGGER_GENERATION_ENABLED"
+)

-	if os.Getenv("DIGGER_GENERATION_ENABLED") == "1" {
+	if os.Getenv(FeatureFlagGeneration) == "1" {

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
backend/controllers/github.go (1)

759-764: Improve error messages and success feedback.

The success and error messages could be more descriptive to help users understand the state better.

Apply this diff to improve the messages:

-				commentReporterManager.UpdateComment(fmt.Sprintf("could not find project %v in digger.yml", projectName))
+				commentReporterManager.UpdateComment(fmt.Sprintf(":x: Project '%v' not found in digger.yml configuration", projectName))
-				commentReporterManager.UpdateComment(fmt.Sprintf(":white_check_mark: Successfully loaded project"))
+				commentReporterManager.UpdateComment(fmt.Sprintf(":white_check_mark: Successfully loaded project '%v' from digger.yml", projectName))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0c142a4 and 767e8c6.

📒 Files selected for processing (2)
  • backend/controllers/github.go (2 hunks)
  • backend/utils/ai.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • backend/utils/ai.go
🔇 Additional comments (3)
backend/controllers/github.go (3)

768-774: ⚠️ Potential issue

Improve configuration validation and error handling.

The code should validate both the generation endpoint and webhook secret configurations.

Apply this diff to add proper validation:

 			generationEndpoint := os.Getenv("DIGGER_GENERATION_ENDPOINT")
 			if generationEndpoint == "" {
-				commentReporterManager.UpdateComment(fmt.Sprintf(":x: server does not have generation endpoint configured, please verify"))
+				commentReporterManager.UpdateComment(":x: Generation endpoint not configured. Please set DIGGER_GENERATION_ENDPOINT environment variable.")
 				log.Printf("server does not have generation endpoint configured, please verify")
 				return fmt.Errorf("server does not have generation endpoint configured, please verify")
 			}
 			webhookSecret := os.Getenv("DIGGER_GENERATION_WEBHOOK_SECRET")
+			if webhookSecret == "" {
+				commentReporterManager.UpdateComment(":x: Webhook secret not configured. Please set DIGGER_GENERATION_WEBHOOK_SECRET environment variable.")
+				log.Printf("server does not have webhook secret configured")
+				return fmt.Errorf("server does not have webhook secret configured")
+			}

Likely invalid or redundant comment.


749-757: 🛠️ Refactor suggestion

⚠️ Potential issue

Fix error handling and improve feature flag management.

The code has the following issues:

  1. The error message in line 754 references an undefined err variable
  2. The feature flag should be defined as a constant for better maintainability

Apply this diff to fix the error handling:

-				commentReporterManager.UpdateComment(fmt.Sprintf(":x: generate requires argument -p <project_name>: %v", err))
+				commentReporterManager.UpdateComment(":x: generate requires argument -p <project_name>")

Consider moving the feature flag to a constant:

+const (
+    FeatureFlagGeneration = "DIGGER_GENERATION_ENABLED"
+)

-	if os.Getenv("DIGGER_GENERATION_ENABLED") == "1" {
+	if os.Getenv(FeatureFlagGeneration) == "1" {

Likely invalid or redundant comment.


816-896: 🛠️ Refactor suggestion

Improve error handling and validation in code generation flow.

The code generation and commit flow needs several improvements:

  1. Add validation for the generated Terraform code
  2. Improve error messages
  3. Add proper cleanup on failure

Add validation for the generated Terraform code:

 			terraformCode, err := utils.GenerateTerraformCode(appCode, generationEndpoint, webhookSecret)
 			if err != nil {
 				commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not generate terraform code: %v", err))
 				log.Printf("could not generate terraform code: %v", err)
 				return fmt.Errorf("could not generate terraform code: %v", err)
 			}
+			
+			// Validate the generated Terraform code
+			if !strings.Contains(terraformCode, "resource") && !strings.Contains(terraformCode, "data") {
+				commentReporterManager.UpdateComment(":x: Generated Terraform code appears to be invalid: no resource or data blocks found")
+				return fmt.Errorf("invalid terraform code generated: no resource or data blocks found")
+			}

Consider implementing a rollback mechanism for failed commits:

+			// Store the original ref for rollback
+			originalRef, _, err := ghService.Client.Git.GetRef(context.Background(), repoOwner, repoName, fmt.Sprintf("refs/heads/%s", *branch))
+			if err != nil {
+				commentReporterManager.UpdateComment(fmt.Sprintf(":x: Failed to get original ref for rollback: %v", err))
+				return fmt.Errorf("failed to get original ref: %v", err)
+			}
+
 			_, _, err = ghService.Client.Git.UpdateRef(context.Background(), repoOwner, repoName, ref, false)
 			if err != nil {
+				// Rollback to original ref
+				_, _, rollbackErr := ghService.Client.Git.UpdateRef(context.Background(), repoOwner, repoName, originalRef, true)
+				if rollbackErr != nil {
+					log.Printf("Failed to rollback ref: %v", rollbackErr)
+				}
 				commentReporterManager.UpdateComment(fmt.Sprintf(":x: Failed to update branch reference: %v", err))
 				log.Printf("Error updating branch reference: %v", err)
 				return fmt.Errorf("error updating branch reference: %v", err)
 			}

Add verification for the generated file:

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (3)
backend/controllers/github.go (3)

843-844: Remove commented code and improve logging.

The code contains a commented line and logs potentially sensitive information.

Apply this diff to clean up the code:

-			// comment terraform code to project dir
-			//project.Dir
-			log.Printf("terraform code is %v", terraformCode)
+			log.Printf("Generated terraform code for project %s, size: %d bytes", projectName, len(terraformCode))

768-774: Improve configuration validation and error messages.

The error messages for missing configuration could be more descriptive.

Apply this diff to improve the error messages:

 			generationEndpoint := os.Getenv("DIGGER_GENERATION_ENDPOINT")
 			if generationEndpoint == "" {
-				commentReporterManager.UpdateComment(fmt.Sprintf(":x: server does not have generation endpoint configured, please verify"))
-				log.Printf("server does not have generation endpoint configured, please verify")
-				return fmt.Errorf("server does not have generation endpoint configured, please verify")
+				const msg = "Generation endpoint not configured. Please set DIGGER_GENERATION_ENDPOINT environment variable."
+				commentReporterManager.UpdateComment(fmt.Sprintf(":x: %s", msg))
+				log.Printf("%s", msg)
+				return fmt.Errorf(msg)
 			}

832-838: Improve error handling in terraform generation.

The error handling could be more descriptive and provide better feedback to users.

Apply this diff to improve error handling:

-			commentReporterManager.UpdateComment(fmt.Sprintf("Generating terraform..."))
+			commentReporterManager.UpdateComment(":hourglass_flowing_sand: Generating Terraform code...")
 			terraformCode, err := utils.GenerateTerraformCode(appCode, generationEndpoint, webhookSecret)
 			if err != nil {
-				commentReporterManager.UpdateComment(fmt.Sprintf(":x: could not generate terraform code: %v", err))
-				log.Printf("could not generate terraform code: %v", err)
-				return fmt.Errorf("could not generate terraform code: %v", err)
+				msg := fmt.Sprintf("Failed to generate Terraform code: %v", err)
+				commentReporterManager.UpdateComment(fmt.Sprintf(":x: %s", msg))
+				log.Printf("%s", msg)
+				return fmt.Errorf(msg)
 			}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 767e8c6 and eab45d5.

📒 Files selected for processing (1)
  • backend/controllers/github.go (2 hunks)
🔇 Additional comments (1)
backend/controllers/github.go (1)

749-757: ⚠️ Potential issue

Improve error handling in command parsing.

The error message references an undefined err variable in the error handling block.

Apply this diff to fix the error message:

-				commentReporterManager.UpdateComment(fmt.Sprintf(":x: generate requires argument -p <project_name>: %v", err))
+				commentReporterManager.UpdateComment(":x: generate requires argument -p <project_name>")

Likely invalid or redundant comment.

Comment on lines +830 to +831
log.Printf("the app code is: %v", appCode)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove or redact sensitive logging.

Logging the entire application code could expose sensitive information and consume excessive log space.

Apply this diff to improve the logging:

-			log.Printf("the app code is: %v", appCode)
+			log.Printf("Successfully extracted application code, size: %d bytes", len(appCode))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
log.Printf("the app code is: %v", appCode)
log.Printf("Successfully extracted application code, size: %d bytes", len(appCode))

Comment on lines +856 to +860
Path: github.String(filepath.Join(project.Dir, fmt.Sprintf("generated_%v.tf", issueNumber))),
Mode: github.String("100644"),
Type: github.String("blob"),
Content: github.String(terraformCode),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve file path handling and content type safety.

The code directly joins paths and doesn't validate the generated terraform code format.

Apply this diff to improve the implementation:

 				{
-					Path:    github.String(filepath.Join(project.Dir, fmt.Sprintf("generated_%v.tf", issueNumber))),
+					Path:    github.String(filepath.ToSlash(filepath.Join(project.Dir, fmt.Sprintf("generated_%d.tf", issueNumber)))),
 					Mode:    github.String("100644"),
 					Type:    github.String("blob"),
-					Content: github.String(terraformCode),
+					Content: github.String(strings.TrimSpace(terraformCode)),
 				},
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Path: github.String(filepath.Join(project.Dir, fmt.Sprintf("generated_%v.tf", issueNumber))),
Mode: github.String("100644"),
Type: github.String("blob"),
Content: github.String(terraformCode),
},
Path: github.String(filepath.ToSlash(filepath.Join(project.Dir, fmt.Sprintf("generated_%d.tf", issueNumber)))),
Mode: github.String("100644"),
Type: github.String("blob"),
Content: github.String(strings.TrimSpace(terraformCode)),
},

Comment on lines 776 to 819
// Get all code content from the repository at a specific commit
getCodeFromCommit := func(ghService *dg_github.GithubService, repoOwner, repoName string, commitSha *string, projectDir string) (string, error) {
const MaxPatchSize = 1024 * 1024 // 1MB limit

// Get the commit's changes compared to default branch
comparison, _, err := ghService.Client.Repositories.CompareCommits(
context.Background(),
repoOwner,
repoName,
defaultBranch,
*commitSha,
nil,
)
if err != nil {
return "", fmt.Errorf("error comparing commits: %v", err)
}

var appCode strings.Builder
for _, file := range comparison.Files {
if file.Size > MaxPatchSize {
return "", fmt.Errorf("file %s exceeds maximum allowed size", *file.Filename)
}
if file.Patch == nil {
continue // Skip files without patches
}
log.Printf("Processing patch for file: %s", *file.Filename)
if *file.Additions > 0 {
lines := strings.Split(*file.Patch, "\n")
for _, line := range lines {
if strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "+++") {
appCode.WriteString(strings.TrimPrefix(line, "+"))
appCode.WriteString("\n")
}
}
}
appCode.WriteString("\n")
}

if appCode.Len() == 0 {
return "", fmt.Errorf("no code changes found in commit %s. Please ensure the PR contains added or modified code", *commitSha)
}

return appCode.String(), nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance code extraction with better memory management.

The code extraction function has several areas for improvement:

  1. The patch processing could be more memory efficient
  2. The error messages could be more descriptive

Apply this diff to improve the implementation:

 func getCodeFromCommit(ghService *dg_github.GithubService, repoOwner, repoName string, commitSha *string, projectDir string) (string, error) {
 	const MaxPatchSize = 1024 * 1024 // 1MB limit
+	var totalSize int64
 	
 	comparison, _, err := ghService.Client.Repositories.CompareCommits(
 		context.Background(),
 		repoOwner,
 		repoName,
 		defaultBranch,
 		*commitSha,
 		nil,
 	)
 	if err != nil {
-		return "", fmt.Errorf("error comparing commits: %v", err)
+		return "", fmt.Errorf("failed to compare commits between %s and %s: %v", defaultBranch, *commitSha, err)
 	}
 
 	var appCode strings.Builder
 	for _, file := range comparison.Files {
+		totalSize += file.Size
+		if totalSize > MaxPatchSize * 2 {
+			return "", fmt.Errorf("total changes exceed maximum allowed size")
+		}
 		if file.Size > MaxPatchSize {
 			return "", fmt.Errorf("file %s exceeds maximum allowed size", *file.Filename)
 		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Get all code content from the repository at a specific commit
getCodeFromCommit := func(ghService *dg_github.GithubService, repoOwner, repoName string, commitSha *string, projectDir string) (string, error) {
const MaxPatchSize = 1024 * 1024 // 1MB limit
// Get the commit's changes compared to default branch
comparison, _, err := ghService.Client.Repositories.CompareCommits(
context.Background(),
repoOwner,
repoName,
defaultBranch,
*commitSha,
nil,
)
if err != nil {
return "", fmt.Errorf("error comparing commits: %v", err)
}
var appCode strings.Builder
for _, file := range comparison.Files {
if file.Size > MaxPatchSize {
return "", fmt.Errorf("file %s exceeds maximum allowed size", *file.Filename)
}
if file.Patch == nil {
continue // Skip files without patches
}
log.Printf("Processing patch for file: %s", *file.Filename)
if *file.Additions > 0 {
lines := strings.Split(*file.Patch, "\n")
for _, line := range lines {
if strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "+++") {
appCode.WriteString(strings.TrimPrefix(line, "+"))
appCode.WriteString("\n")
}
}
}
appCode.WriteString("\n")
}
if appCode.Len() == 0 {
return "", fmt.Errorf("no code changes found in commit %s. Please ensure the PR contains added or modified code", *commitSha)
}
return appCode.String(), nil
}
// Get all code content from the repository at a specific commit
getCodeFromCommit := func(ghService *dg_github.GithubService, repoOwner, repoName string, commitSha *string, projectDir string) (string, error) {
const MaxPatchSize = 1024 * 1024 // 1MB limit
var totalSize int64
// Get the commit's changes compared to default branch
comparison, _, err := ghService.Client.Repositories.CompareCommits(
context.Background(),
repoOwner,
repoName,
defaultBranch,
*commitSha,
nil,
)
if err != nil {
return "", fmt.Errorf("failed to compare commits between %s and %s: %v", defaultBranch, *commitSha, err)
}
var appCode strings.Builder
for _, file := range comparison.Files {
totalSize += file.Size
if totalSize > MaxPatchSize * 2 {
return "", fmt.Errorf("total changes exceed maximum allowed size")
}
if file.Size > MaxPatchSize {
return "", fmt.Errorf("file %s exceeds maximum allowed size", *file.Filename)
}
if file.Patch == nil {
continue // Skip files without patches
}
log.Printf("Processing patch for file: %s", *file.Filename)
if *file.Additions > 0 {
lines := strings.Split(*file.Patch, "\n")
for _, line := range lines {
if strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "+++") {
appCode.WriteString(strings.TrimPrefix(line, "+"))
appCode.WriteString("\n")
}
}
}
appCode.WriteString("\n")
}
if appCode.Len() == 0 {
return "", fmt.Errorf("no code changes found in commit %s. Please ensure the PR contains added or modified code", *commitSha)
}
return appCode.String(), nil
}

@motatoes motatoes merged commit 5e62218 into develop Dec 17, 2024
11 checks passed
motatoes added a commit that referenced this pull request Jan 2, 2025
* add flag to ignore all external directories per project (#1851)

* add flag to ignore all external directories per project

* revert includeparentblocks flag (#1852)

* improve efficiency in terragrunt generation (#1854)

* improve efficiency in terragrunt generation

* Update action.yml (#1856)

* handle crashes in goroutine events (#1857)

* fix/recover from webhook goroutines (#1858)

* handle crashes in goroutine events

* include stacktrace in errors

* wip generation of terraform code from application code (#1855)

* terraform code generation demo


---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* fix: incorrect comment in backend/migrations (#1860)

* Update setup-opentofu to fix issues with 1.6.x downloads (#1861)

* restructure docs to have no columns (#1862)

* Create curl_bootstrap.sh

* refactor codegen parts (#1863)

* refactor codegen parts

* publish ai summaries (#1864)

* publish ai summaries

* add a header for summary comment (#1865)

* fix heading (#1866)

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Aldo <82811+aldoborrero@users.noreply.github.com>
Co-authored-by: Hugo Samayoa <htplbc@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant