|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { exec } from "child_process"; |
| 3 | +import { promisify } from "util"; |
| 4 | +import * as fs from "fs"; |
| 5 | +import * as path from "path"; |
| 6 | +import * as os from "os"; |
| 7 | +import { WorkItem, Repository } from "../types/weeklyReport"; |
| 8 | +import { SCMFactory } from "../scm/SCMProvider"; |
| 9 | + |
| 10 | +const execAsync = promisify(exec); |
| 11 | +const readFileAsync = promisify(fs.readFile); |
| 12 | +const readdirAsync = promisify(fs.readdir); |
| 13 | + |
| 14 | +export class WeeklyReportService { |
| 15 | + private readonly WORK_DAYS = 5; // 固定为5个工作日 |
| 16 | + private readonly HOURS_PER_DAY = 8; // 每天8小时 |
| 17 | + private allLogs: string[] = []; |
| 18 | + |
| 19 | + constructor() {} |
| 20 | + |
| 21 | + async generate(): Promise<WorkItem[]> { |
| 22 | + const scmProvider = await SCMFactory.detectSCM(); |
| 23 | + if (!scmProvider) { |
| 24 | + throw new Error("No SCM provider detected"); |
| 25 | + } |
| 26 | + |
| 27 | + // 获取作者信息 |
| 28 | + const author = await this.getAuthor(scmProvider.type); |
| 29 | + if (!author) { |
| 30 | + throw new Error("Unable to detect author information"); |
| 31 | + } |
| 32 | + |
| 33 | + const repositories = await this.findRepositories(); |
| 34 | + await this.collectLogs(repositories, author); |
| 35 | + return this.processLogs(); |
| 36 | + } |
| 37 | + |
| 38 | + private async getSvnAuthor(): Promise<string | undefined> { |
| 39 | + try { |
| 40 | + const svnAuthPath = path.join( |
| 41 | + os.homedir(), |
| 42 | + ".subversion", |
| 43 | + "auth", |
| 44 | + "svn.simple" |
| 45 | + ); |
| 46 | + const files = await readdirAsync(svnAuthPath); |
| 47 | + |
| 48 | + // 读取第一个认证文件 |
| 49 | + if (files.length > 0) { |
| 50 | + const authFile = path.join(svnAuthPath, files[0]); |
| 51 | + const content = await readFileAsync(authFile, "utf-8"); |
| 52 | + |
| 53 | + // 使用正则表达式匹配用户名 |
| 54 | + const usernameMatch = content.match(/username="([^"]+)"/); |
| 55 | + if (usernameMatch && usernameMatch[1]) { |
| 56 | + return usernameMatch[1]; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // 如果无法从配置文件获取,尝试从 svn info 获取 |
| 61 | + const { stdout } = await execAsync("svn info --show-item author"); |
| 62 | + return stdout.trim(); |
| 63 | + } catch (error) { |
| 64 | + console.error(`Error getting SVN author: ${error}`); |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private async getAuthor(type: "git" | "svn"): Promise<string | undefined> { |
| 70 | + try { |
| 71 | + if (type === "git") { |
| 72 | + const { stdout } = await execAsync("git config user.name"); |
| 73 | + return stdout.trim(); |
| 74 | + } else { |
| 75 | + return await this.getSvnAuthor(); |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + console.error(`Error getting author: ${error}`); |
| 79 | + return undefined; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private async collectLogs(repositories: Repository[], author: string) { |
| 84 | + for (const repo of repositories) { |
| 85 | + if (repo.type === "git") { |
| 86 | + await this.collectGitLogs(repo.path, author); |
| 87 | + } else { |
| 88 | + await this.collectSvnLogs(repo.path, author); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private getLastWeekDates(): { start: Date; end: Date } { |
| 94 | + const today = new Date(); |
| 95 | + const currentDay = today.getDay(); |
| 96 | + |
| 97 | + // 计算上周一的日期 |
| 98 | + const lastMonday = new Date(today); |
| 99 | + lastMonday.setDate(today.getDate() - currentDay - 7 + 1); |
| 100 | + lastMonday.setHours(0, 0, 0, 0); |
| 101 | + |
| 102 | + // 计算上周五的日期 |
| 103 | + const lastFriday = new Date(lastMonday); |
| 104 | + lastFriday.setDate(lastMonday.getDate() + 4); |
| 105 | + lastFriday.setHours(23, 59, 59, 999); |
| 106 | + |
| 107 | + return { start: lastMonday, end: lastFriday }; |
| 108 | + } |
| 109 | + |
| 110 | + private async collectGitLogs(repoPath: string, author: string) { |
| 111 | + const { start, end } = this.getLastWeekDates(); |
| 112 | + const startDate = start.toISOString(); |
| 113 | + const endDate = end.toISOString(); |
| 114 | + |
| 115 | + const command = `git log --after="${startDate}" --before="${endDate}" --author="${author}" --pretty=format:"%s"`; |
| 116 | + try { |
| 117 | + const { stdout } = await execAsync(command, { cwd: repoPath }); |
| 118 | + if (stdout.trim()) { |
| 119 | + this.allLogs = this.allLogs.concat(stdout.trim().split("\n")); |
| 120 | + } |
| 121 | + } catch (error) { |
| 122 | + console.error(`Error collecting Git logs: ${error}`); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private async findRepositories(): Promise<Repository[]> { |
| 127 | + const repositories: Repository[] = []; |
| 128 | + const workspaceFolders = vscode.workspace.workspaceFolders; |
| 129 | + |
| 130 | + if (!workspaceFolders) { |
| 131 | + return repositories; |
| 132 | + } |
| 133 | + |
| 134 | + for (const folder of workspaceFolders) { |
| 135 | + try { |
| 136 | + // 检查是否是 Git 仓库 |
| 137 | + const { stdout: gitOutput } = await execAsync( |
| 138 | + "git rev-parse --git-dir", |
| 139 | + { |
| 140 | + cwd: folder.uri.fsPath, |
| 141 | + } |
| 142 | + ); |
| 143 | + if (gitOutput) { |
| 144 | + repositories.push({ |
| 145 | + type: "git", |
| 146 | + path: folder.uri.fsPath, |
| 147 | + }); |
| 148 | + continue; |
| 149 | + } |
| 150 | + } catch {} |
| 151 | + |
| 152 | + try { |
| 153 | + // 检查是否是 SVN 仓库 |
| 154 | + const { stdout: svnOutput } = await execAsync("svn info", { |
| 155 | + cwd: folder.uri.fsPath, |
| 156 | + }); |
| 157 | + if (svnOutput) { |
| 158 | + repositories.push({ |
| 159 | + type: "svn", |
| 160 | + path: folder.uri.fsPath, |
| 161 | + }); |
| 162 | + } |
| 163 | + } catch {} |
| 164 | + } |
| 165 | + |
| 166 | + return repositories; |
| 167 | + } |
| 168 | + |
| 169 | + private async collectSvnLogs(repoPath: string, author: string) { |
| 170 | + const { start, end } = this.getLastWeekDates(); |
| 171 | + try { |
| 172 | + const command = `svn log -r {${start.toISOString()}}:{${end.toISOString()}} --search="${author}" --xml`; |
| 173 | + const { stdout } = await execAsync(command, { cwd: repoPath }); |
| 174 | + const matches = stdout.matchAll(/<msg>([\s\S]*?)<\/msg>/g); |
| 175 | + for (const match of matches) { |
| 176 | + if (match[1] && match[1].trim()) { |
| 177 | + this.allLogs.push(match[1].trim()); |
| 178 | + } |
| 179 | + } |
| 180 | + } catch (error) { |
| 181 | + console.error(`Error collecting SVN logs: ${error}`); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private processLogs(): WorkItem[] { |
| 186 | + const uniqueLogs = [...new Set(this.allLogs)]; |
| 187 | + const workItems: WorkItem[] = []; |
| 188 | + const totalHours = this.WORK_DAYS * this.HOURS_PER_DAY; |
| 189 | + const hoursPerLog = totalHours / uniqueLogs.length; |
| 190 | + |
| 191 | + uniqueLogs.forEach((log, index) => { |
| 192 | + let timeSpent = hoursPerLog; |
| 193 | + if (index === uniqueLogs.length - 1) { |
| 194 | + const totalAllocated = workItems.reduce( |
| 195 | + (sum, item) => sum + parseFloat(item.time), |
| 196 | + 0 |
| 197 | + ); |
| 198 | + const remaining = totalHours - totalAllocated; |
| 199 | + if (remaining > 0) { |
| 200 | + timeSpent = remaining; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + workItems.push({ |
| 205 | + content: log, |
| 206 | + time: `${timeSpent.toFixed(1)}h`, |
| 207 | + description: this.generateDescription(log), |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + return workItems; |
| 212 | + } |
| 213 | + |
| 214 | + private generateDescription(log: string): string { |
| 215 | + // 移除常见的提交前缀,如 feat:, fix: 等 |
| 216 | + const cleanLog = log.replace( |
| 217 | + /^(feat|fix|docs|style|refactor|test|chore|perf):\s*/i, |
| 218 | + "" |
| 219 | + ); |
| 220 | + |
| 221 | + // 移除 emoji |
| 222 | + const noEmoji = cleanLog |
| 223 | + .replace(/:[a-z_]+:|�[\u{1F300}-\u{1F6FF}]/gu, "") |
| 224 | + .trim(); |
| 225 | + |
| 226 | + // 如果内容过短,添加更多描述 |
| 227 | + if (noEmoji.length < 20) { |
| 228 | + return `完成${noEmoji}相关功能的开发和调试工作`; |
| 229 | + } |
| 230 | + |
| 231 | + return noEmoji; |
| 232 | + } |
| 233 | +} |
0 commit comments