-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdebugger.go
More file actions
115 lines (104 loc) · 2.8 KB
/
Copy pathdebugger.go
File metadata and controls
115 lines (104 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"fmt"
"strings"
"github.com/kpenfound/greetings-api/.dagger/internal/dagger"
)
// Debug broken tests. Returns a unified diff of the test fixes
func (g *Greetings) DebugTests(
ctx context.Context,
// The model to use to debug debug tests
// +optional
// +default = "claude-sonnet-4-0"
model string,
) (string, error) {
prompt := dag.CurrentModule().Source().File("prompts/fix_tests.md")
// Check if backend is broken
if _, berr := g.Backend.CheckDirectory(ctx, g.Backend.Source()); berr != nil {
ws := dag.Workspace(
g.Source,
g.Backend.AsWorkspaceCheckable(),
".",
)
env := dag.Env().
WithWorkspaceInput("workspace", ws, "workspace to read, write, and test code").
WithWorkspaceOutput("fixed", "workspace with fixed tests")
return dag.LLM(dagger.LLMOpts{Model: model}).
WithEnv(env).
WithPromptFile(prompt).
Env().
Output("fixed").
AsWorkspace().
Diff(ctx)
}
// Check if frontend is broken
if _, ferr := g.Frontend.CheckDirectory(ctx, g.Frontend.Source()); ferr != nil {
ws := dag.Workspace(
g.Source,
g.Frontend.AsWorkspaceCheckable(),
"website",
)
env := dag.Env().
WithWorkspaceInput("workspace", ws, "workspace to read, write, and test code").
WithWorkspaceOutput("fixed", "workspace with fixed tests")
return dag.LLM(dagger.LLMOpts{Model: model}).
WithEnv(env).
WithPromptFile(prompt).
Env().
Output("fixed").
AsWorkspace().
Diff(ctx)
}
return "", fmt.Errorf("no broken tests found")
}
// Debug broken tests on a pull request and comment fix suggestions
func (g *Greetings) DebugBrokenTestsPr(
ctx context.Context,
// Github token with permissions to comment on the pull request
githubToken *dagger.Secret,
// Git commit in Github
commit string,
// The model to use to debug debug tests
// +optional
// +default = "claude-sonnet-4-0"
model string,
) error {
gh := dag.GithubIssue(dagger.GithubIssueOpts{Token: githubToken})
// Determine PR head
gitRef := dag.Git(g.Repo).Commit(commit)
gitSource := gitRef.Tree()
pr, err := gh.GetPrForCommit(ctx, g.Repo, commit)
if err != nil {
return err
}
// Set source to PR head
g = New(gitSource, g.Repo, g.Image, g.App)
// Suggest fix
suggestionDiff, err := g.DebugTests(ctx, model)
if err != nil {
return err
}
if suggestionDiff == "" {
return fmt.Errorf("no suggestions found")
}
// Convert the diff to CodeSuggestions
codeSuggestions := parseDiff(suggestionDiff)
// For each suggestion, comment on PR
for _, suggestion := range codeSuggestions {
markupSuggestion := "```suggestion\n" + strings.Join(suggestion.Suggestion, "\n") + "\n```"
err := gh.WritePullRequestCodeComment(
ctx,
g.Repo,
pr,
commit,
markupSuggestion,
suggestion.File,
"RIGHT",
suggestion.Line)
if err != nil {
return err
}
}
return nil
}