-
Notifications
You must be signed in to change notification settings - Fork 562
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
Conversation
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 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. 📒 Files selected for processing (1)
WalkthroughThe pull request introduces a feature for generating Terraform code in response to specific comments on GitHub issues. When the environment variable Changes
Sequence DiagramsequenceDiagram
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
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
✅ No security or compliance issues detected. Reviewed everything up to 5fd36ee. Security Overview
Detected Code Changes
Diagnostics
Reply to this PR with |
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.
Actionable comments posted: 6
🧹 Outside diff range and nitpick comments (5)
backend/utils/ai.go (4)
57-59
: Add response content validationThe 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 messagesThe 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 codeThe 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:
- Determining the correct file path
- Creating necessary directories
- Writing the file with proper permissions
- 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
, replaceappCode := "mycode"
withappCode := projectName
🔗 Analysis chain
Line range hint
771-771
: Replace hardcoded application codeThe 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 goLength 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 goLength 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
📒 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
:
Fix error handling and implement code generation response.
The code has several issues:
- The error message on generation failure is incorrect (shows configuration error)
- The generated code is only logged, not written to the project
- 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
:
client := &http.Client{} | ||
resp, err := client.Do(req) |
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.
🛠️ 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.
client := &http.Client{} | |
resp, err := client.Do(req) | |
client := &http.Client{ | |
Timeout: 30 * time.Second, | |
} | |
resp, err := client.Do(req) |
backend/controllers/github.go
Outdated
appCode := "mycode" | ||
terraformCode, err := utils.GenerateTerraformCode(appCode, generationEndpoint, webhookSecret) |
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.
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:
- Extracting the application code from the project configuration
- 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.
// 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) | ||
} |
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.
🛠️ Refactor suggestion
Fix error handling and improve feature flag management.
The code has the following issues:
- The error message in line 752 references an undefined
err
variable - 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.
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.
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
📒 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
:
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
Fix error handling and improve feature flag management.
The code has the following issues:
- The error message in line 754 references an undefined
err
variable - 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:
- Add validation for the generated Terraform code
- Improve error messages
- 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>
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.
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
📒 Files selected for processing (1)
backend/controllers/github.go
(2 hunks)
🔇 Additional comments (1)
backend/controllers/github.go (1)
749-757
:
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.
log.Printf("the app code is: %v", appCode) | ||
|
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.
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.
log.Printf("the app code is: %v", appCode) | |
log.Printf("Successfully extracted application code, size: %d bytes", len(appCode)) | |
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), | ||
}, |
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.
🛠️ 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.
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)), | |
}, |
backend/controllers/github.go
Outdated
// 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 | ||
} |
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.
🛠️ Refactor suggestion
Enhance code extraction with better memory management.
The code extraction function has several areas for improvement:
- The patch processing could be more memory efficient
- 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.
// 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 | |
} |
* 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>
Summary by CodeRabbit
New Features
Bug Fixes