Browse Source

🐞 fix: 修复部分获取仓库信息的api无效的问题

Pchen. 3 months ago
parent
commit
41ff395483
4 changed files with 50 additions and 39 deletions
  1. 3 3
      apis/Repos/GetRepoLog.js
  2. 3 3
      apis/Repos/GetRepoStatus.js
  3. 30 24
      apis/Repos/GitCodeStats.js
  4. 14 9
      apis/Repos/GitContributors.js

+ 3 - 3
apis/Repos/GetRepoLog.js

@@ -27,15 +27,15 @@ class GetRepoLog extends API {
                 ...BaseStdResponse.ACCESS_DENIED
             })
 
-        let sql = 'SELECT url FROM repos WHERE create_user = ? AND id = ?'
+        let sql = 'SELECT state, path, url FROM repos WHERE create_user = ? AND id = ?'
         let r = await db.query(sql, [uuid, id])
-        if (r && r.length === 0)
+        if (!r || r.length === 0)
             return res.json({
                 ...BaseStdResponse.ERR,
                 msg: '未找到仓库'
             })
 
-        if (r.state !== 1 || !r[0].path === 0)
+        if (r[0].state !== 1 || !r[0].path)
             return res.json({
                 ...BaseStdResponse.ERR,
                 msg: '仓库未成功克隆!'

+ 3 - 3
apis/Repos/GetRepoStatus.js

@@ -27,15 +27,15 @@ class GetRepoStatus extends API {
                 ...BaseStdResponse.ACCESS_DENIED
             })
 
-        let sql = 'SELECT url FROM repos WHERE create_user = ? AND id = ?'
+        let sql = 'SELECT state, path, url FROM repos WHERE create_user = ? AND id = ?'
         let r = await db.query(sql, [uuid, id])
-        if (r && r.length === 0)
+        if (!r || r.length === 0)
             return res.json({
                 ...BaseStdResponse.ERR,
                 msg: '未找到仓库'
             })
 
-        if (r.state !== 1 || !r[0].path === 0)
+        if (r[0].state !== 1 || !r[0].path)
             return res.json({
                 ...BaseStdResponse.ERR,
                 msg: '仓库未成功克隆!'

+ 30 - 24
apis/Repos/GitCodeStats.js

@@ -94,45 +94,51 @@ class gitCodeStats extends API {
                 ...BaseStdResponse.ACCESS_DENIED
             })
 
-        let sql = 'SELECT path FROM repos WHERE create_user = ? AND id = ?'
+        let sql = 'SELECT state, path FROM repos WHERE create_user = ? AND id = ?'
 
         let r = await db.query(sql, [uuid, id])
-        if (r || r.length === 0)
+        if (!r || r.length === 0)
             return res.json({
                 ...BaseStdResponse.ERR,
                 msg: '未找到仓库'
             })
 
-        if (r.state !== 1 || !r[0].path === 0)
+        if (r[0].state !== 1 || !r[0].path)
             return res.json({
                 ...BaseStdResponse.ERR,
                 msg: '仓库未成功克隆!'
             })
 
-        const git = simpleGit(r[0].path)
-        const files = await git.raw(["ls-files"]) // 获取所有 Git 跟踪的文件
-        const fileList = files.split("\n").map(f => f.trim()).filter(f => f)
-
         let totalLines = 0
         let languageStats = {}
-
-        for (const file of fileList) {
-            const fullPath = path.join(r[0].path, file)
-            const ext = path.extname(file)
-            const language = Object.keys(this.languageExtensions).find(lang =>
-                this.languageExtensions[lang].includes(ext)
-            );
-
-            if (language) {
-                const lines = await countLines(fullPath)
-                totalLines += lines
-                languageStats[language] = (languageStats[language] || 0) + lines
-            } else {
-                // 如果文件不属于任何已知语言,归类为其他
-                const lines = await countLines(fullPath)
-                totalLines += lines
-                languageStats["Other"] = (languageStats["Other"] || 0) + lines
+        try {
+            const git = simpleGit(r[0].path)
+            const files = await git.raw(["ls-files"]) // 获取所有 Git 跟踪的文件
+            const fileList = files.split("\n").map(f => f.trim()).filter(f => f)
+
+            for (const file of fileList) {
+                const fullPath = path.join(r[0].path, file)
+                const ext = path.extname(file)
+                const language = Object.keys(this.languageExtensions).find(lang =>
+                    this.languageExtensions[lang].includes(ext)
+                )
+
+                if (language) {
+                    const lines = await countLines(fullPath)
+                    totalLines += lines
+                    languageStats[language] = (languageStats[language] || 0) + lines
+                } else {
+                    // 如果文件不属于任何已知语言,归类为其他
+                    const lines = await countLines(fullPath)
+                    totalLines += lines
+                    languageStats["Other"] = (languageStats["Other"] || 0) + lines
+                }
             }
+        } catch (error) {
+            return res.json({
+                ...BaseStdResponse.ERR,
+                msg: '获取仓库信息失败!'
+            })
         }
 
         res.json({

+ 14 - 9
apis/Repos/GitContributors.js

@@ -84,31 +84,36 @@ class GitContributors extends API {
                 ...BaseStdResponse.ACCESS_DENIED
             })
 
-        let sql = 'SELECT path FROM repos WHERE create_user = ? AND id = ?'
+        let sql = 'SELECT state, path FROM repos WHERE create_user = ? AND id = ?'
         let r = await db.query(sql, [uuid, id])
-        if (r && r.length === 0)
+        if (!r || r.length === 0)
             return res.json({
                 ...BaseStdResponse.ERR,
                 msg: '未找到仓库'
             })
 
-        if (r.state !== 1 || !r[0].path === 0)
+        if (r[0].state !== 1 || !r[0].path)
             return res.json({
                 ...BaseStdResponse.ERR,
                 msg: '仓库未成功克隆!'
             })
 
+        let contributors
         try {
-            const contributors = await this.analyzeGitContributors(r[0].path)
-            res.json({
-                ...BaseStdResponse.OK,
-                data: contributors
-            })
-
+            contributors = await this.analyzeGitContributors(r[0].path)
         } catch (error) {
             this.logger.error('获取仓库开发者失败!' + error.stack)
+            return res.json({
+                ...BaseStdResponse.ERR,
+                msg: '获取仓库开发者失败!'
+            })
         }
 
+        res.json({
+            ...BaseStdResponse.OK,
+            data: contributors
+        })
+
     }
 }