|
| 1 | +package workspace |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// dirFormatter 目录格式管理器 |
| 10 | +type dirFormatter struct{} |
| 11 | + |
| 12 | +// newDirFormatter 创建目录格式管理器 |
| 13 | +func newDirFormatter() *dirFormatter { |
| 14 | + return &dirFormatter{} |
| 15 | +} |
| 16 | + |
| 17 | +// generateIssueDirName 生成Issue目录名 |
| 18 | +func (f *dirFormatter) generateIssueDirName(aiModel, repo string, issueNumber int, timestamp int64) string { |
| 19 | + return fmt.Sprintf("%s-%s-issue-%d-%d", aiModel, repo, issueNumber, timestamp) |
| 20 | +} |
| 21 | + |
| 22 | +// generatePRDirName 生成PR目录名 |
| 23 | +func (f *dirFormatter) generatePRDirName(aiModel, repo string, prNumber int, timestamp int64) string { |
| 24 | + return fmt.Sprintf("%s-%s-pr-%d-%d", aiModel, repo, prNumber, timestamp) |
| 25 | +} |
| 26 | + |
| 27 | +// generateSessionDirName 生成Session目录名 |
| 28 | +func (f *dirFormatter) generateSessionDirName(aiModel, repo string, prNumber int, timestamp int64) string { |
| 29 | + return fmt.Sprintf("%s-%s-session-%d-%d", aiModel, repo, prNumber, timestamp) |
| 30 | +} |
| 31 | + |
| 32 | +// generateSessionDirNameWithSuffix 生成Session目录名(使用suffix) |
| 33 | +func (f *dirFormatter) generateSessionDirNameWithSuffix(aiModel, repo string, prNumber int, suffix string) string { |
| 34 | + return fmt.Sprintf("%s-%s-session-%d-%s", aiModel, repo, prNumber, suffix) |
| 35 | +} |
| 36 | + |
| 37 | +// extractSuffixFromPRDir 从PR目录名中提取suffix(时间戳) |
| 38 | +func (f *dirFormatter) extractSuffixFromPRDir(aiModel, repo string, prNumber int, dirName string) string { |
| 39 | + expectedPrefix := fmt.Sprintf("%s-%s-pr-%d-", aiModel, repo, prNumber) |
| 40 | + return strings.TrimPrefix(dirName, expectedPrefix) |
| 41 | +} |
| 42 | + |
| 43 | +// extractSuffixFromIssueDir 从Issue目录名中提取suffix(时间戳) |
| 44 | +func (f *dirFormatter) extractSuffixFromIssueDir(aiModel, repo string, issueNumber int, dirName string) string { |
| 45 | + expectedPrefix := fmt.Sprintf("%s-%s-issue-%d-", aiModel, repo, issueNumber) |
| 46 | + return strings.TrimPrefix(dirName, expectedPrefix) |
| 47 | +} |
| 48 | + |
| 49 | +// createSessionPath 创建Session目录路径 |
| 50 | +func (f *dirFormatter) createSessionPath(underPath, aiModel, repo string, prNumber int, suffix string) string { |
| 51 | + dirName := f.generateSessionDirNameWithSuffix(aiModel, repo, prNumber, suffix) |
| 52 | + return fmt.Sprintf("%s/%s", underPath, dirName) |
| 53 | +} |
| 54 | + |
| 55 | +// createSessionPathWithTimestamp 创建Session目录路径(使用时间戳) |
| 56 | +func (f *dirFormatter) createSessionPathWithTimestamp(underPath, aiModel, repo string, prNumber int, timestamp int64) string { |
| 57 | + dirName := f.generateSessionDirName(aiModel, repo, prNumber, timestamp) |
| 58 | + return fmt.Sprintf("%s/%s", underPath, dirName) |
| 59 | +} |
| 60 | + |
| 61 | +// IssueDirFormat Issue目录格式 |
| 62 | +type IssueDirFormat struct { |
| 63 | + AIModel string |
| 64 | + Repo string |
| 65 | + IssueNumber int |
| 66 | + Timestamp int64 |
| 67 | +} |
| 68 | + |
| 69 | +// PRDirFormat PR目录格式 |
| 70 | +type PRDirFormat struct { |
| 71 | + AIModel string |
| 72 | + Repo string |
| 73 | + PRNumber int |
| 74 | + Timestamp int64 |
| 75 | +} |
| 76 | + |
| 77 | +// SessionDirFormat Session目录格式 |
| 78 | +type SessionDirFormat struct { |
| 79 | + AIModel string |
| 80 | + Repo string |
| 81 | + PRNumber int |
| 82 | + Timestamp int64 |
| 83 | +} |
| 84 | + |
| 85 | +// parsePRDirName 解析PR目录名 |
| 86 | +func (f *dirFormatter) parsePRDirName(dirName string) (*PRDirFormat, error) { |
| 87 | + parts := strings.Split(dirName, "-") |
| 88 | + if len(parts) < 5 { |
| 89 | + return nil, fmt.Errorf("invalid PR directory format: %s", dirName) |
| 90 | + } |
| 91 | + |
| 92 | + // 格式: {aiModel}-{repo}-pr-{prNumber}-{timestamp} |
| 93 | + // 找到 "pr" 的位置 |
| 94 | + prIndex := -1 |
| 95 | + for i, part := range parts { |
| 96 | + if part == "pr" { |
| 97 | + prIndex = i |
| 98 | + break |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if prIndex == -1 || prIndex < 2 || prIndex >= len(parts)-2 { |
| 103 | + return nil, fmt.Errorf("invalid PR directory format: %s", dirName) |
| 104 | + } |
| 105 | + |
| 106 | + // 提取AI模型和仓库名 |
| 107 | + aiModel := strings.Join(parts[:prIndex-1], "-") |
| 108 | + repo := parts[prIndex-1] |
| 109 | + |
| 110 | + // 提取PR编号 |
| 111 | + prNumber, err := strconv.Atoi(parts[prIndex+1]) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("invalid PR number: %s", parts[prIndex+1]) |
| 114 | + } |
| 115 | + |
| 116 | + // 提取时间戳 |
| 117 | + timestamp, err := strconv.ParseInt(parts[prIndex+2], 10, 64) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("invalid timestamp: %s", parts[prIndex+2]) |
| 120 | + } |
| 121 | + |
| 122 | + return &PRDirFormat{ |
| 123 | + AIModel: aiModel, |
| 124 | + Repo: repo, |
| 125 | + PRNumber: prNumber, |
| 126 | + Timestamp: timestamp, |
| 127 | + }, nil |
| 128 | +} |
0 commit comments