- Add options/seed/error_patterns.json with default upload error patterns - Add models/ai/seed.go with SeedErrorPatternsIfEmpty function - Call seed on startup after models.Init if error_pattern table is empty - Seeds 10 common error patterns for uploads, .NET, etc. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
// Copyright 2026 MarketAlly. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package ai
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/options"
|
|
)
|
|
|
|
// SeedData represents the structure of the seed JSON file
|
|
type SeedData struct {
|
|
Patterns []struct {
|
|
Pattern string `json:"pattern"`
|
|
RunnerType string `json:"runner_type"`
|
|
ProjectType string `json:"project_type"`
|
|
Framework string `json:"framework"`
|
|
ErrorMessage string `json:"error_message"`
|
|
Diagnosis string `json:"diagnosis"`
|
|
Solution string `json:"solution"`
|
|
} `json:"patterns"`
|
|
}
|
|
|
|
// SeedErrorPatternsIfEmpty seeds error patterns from the seed file if the table is empty
|
|
func SeedErrorPatternsIfEmpty(ctx context.Context) error {
|
|
count, err := db.GetEngine(ctx).Count(&ErrorPattern{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if count > 0 {
|
|
log.Debug("Error patterns table already has %d entries, skipping seed", count)
|
|
return nil
|
|
}
|
|
|
|
log.Info("Seeding error patterns from seed file...")
|
|
|
|
// Load seed data from embedded options
|
|
data, err := options.AssetFS().ReadFile("seed/error_patterns.json")
|
|
if err != nil {
|
|
log.Warn("Could not read error patterns seed file: %v", err)
|
|
return nil // Not an error, just no seed file
|
|
}
|
|
|
|
var seedData SeedData
|
|
if err := json.Unmarshal(data, &seedData); err != nil {
|
|
log.Error("Failed to parse error patterns seed file: %v", err)
|
|
return err
|
|
}
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
for _, p := range seedData.Patterns {
|
|
ep := &ErrorPattern{
|
|
Pattern: p.Pattern,
|
|
RunnerType: p.RunnerType,
|
|
ProjectType: p.ProjectType,
|
|
Framework: p.Framework,
|
|
ErrorMessage: p.ErrorMessage,
|
|
Diagnosis: p.Diagnosis,
|
|
Solution: p.Solution,
|
|
}
|
|
if err := db.Insert(ctx, ep); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
log.Info("Seeded %d error patterns", len(seedData.Patterns))
|
|
return nil
|
|
})
|
|
}
|