Browse Source

✨ feat: 增加提交详情获取接口

Pchen. 3 months ago
parent
commit
0c441fe18a
3 changed files with 103 additions and 1 deletions
  1. 2 1
      apis/GitActions/ChangeBranch.js
  2. 97 0
      apis/Repos/GetCommitDetail.js
  3. 4 0
      apis/Repos/GetRepoStatus.js

+ 2 - 1
apis/GitActions/ChangeBranch.js

@@ -45,7 +45,8 @@ class ChangeBranch extends API {
             await git.cwd(r[0].path)
 
             const branches = await git.branch()
-            if(!branches.all.includes(branch))
+            const remoteBranch = 'remotes/origin/' + branch
+            if(!branches.all.includes(branch) && !branches.all.includes(remoteBranch))
                 return res.json({
                     ...BaseStdResponse.ERR,
                     msg: '分支不存在!'

+ 97 - 0
apis/Repos/GetCommitDetail.js

@@ -0,0 +1,97 @@
+const API = require("../../lib/API")
+const AccessControl = require("../../lib/AccessControl")
+const { BaseStdResponse } = require("../../BaseStdResponse")
+const db = require("../../plugin/DataBase/db")
+const simpleGit = require('simple-git')
+
+class GetCommitDetail extends API {
+    constructor() {
+        super()
+
+        this.setMethod("GET")
+        this.setPath("/Repos/GetCommitDetail")
+    }
+
+    async onRequest(req, res) {
+        let { uuid, session, id, hash } = req.query
+
+        if ([uuid, session, id, hash].some(value => value === '' || value === null || value === undefined))
+            return res.json({
+                ...BaseStdResponse.MISSING_PARAMETER
+            })
+
+        // 检查 session
+        if (!await AccessControl.checkSession(uuid, session))
+            return res.status(401).json({
+                ...BaseStdResponse.ACCESS_DENIED
+            })
+
+        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)
+            return res.json({
+                ...BaseStdResponse.ERR,
+                msg: '未找到仓库'
+            })
+
+        if (r[0].state !== 1 || !r[0].path)
+            return res.json({
+                ...BaseStdResponse.ERR,
+                msg: '仓库未成功克隆!'
+            })
+
+        try {
+            const git = simpleGit()
+            await git.cwd(r[0].path)
+
+            git.show([hash], (err, result) => {
+                if (err)
+                    return res.json({
+                        ...BaseStdResponse.ERR,
+                        msg: '获取提交详情失败!'
+                    })
+
+                const commitRegex = /^commit (\w+)\nAuthor: (.+)\nDate:\s+(.+)\n\n\s*(.+)/
+                const match = result.match(commitRegex)
+                if (!match) 
+                    return res.json({
+                        ...BaseStdResponse.ERR,
+                        msg: '解析提交详情失败!'
+                    })
+
+                const commitInfo = {
+                    hash: match[1],
+                    author: match[2],
+                    date: match[3],
+                    message: match[4],
+                    files: [],
+                    diffs: []
+                }
+
+                // 解析 diff 信息
+                const diffRegex = /diff --git a\/(.+?) b\/.+?\nindex .+\n--- a\/.+?\n\+\+\+ b\/.+?\n([\s\S]+?)(?=diff --git a\/|\Z)/g
+                let diffMatch
+                while ((diffMatch = diffRegex.exec(result)) !== null) {
+                    commitInfo.files.push(diffMatch[1])
+                    commitInfo.diffs.push({
+                        file: diffMatch[1],
+                        changes: diffMatch[2].trim().split('\n')
+                    })
+                }
+                
+                res.json({
+                    ...BaseStdResponse.OK,
+                    data: commitInfo
+                })
+            })
+        } catch (error) {
+            this.logger.error('获取提交详情失败!' + error.stack)
+            return res.json({
+                ...BaseStdResponse.ERR,
+                msg: '获取提交详情失败!'
+            })
+        }
+    }
+}
+
+module.exports.GetCommitDetail = GetCommitDetail

+ 4 - 0
apis/Repos/GetRepoStatus.js

@@ -74,6 +74,10 @@ class GetRepoStatus extends API {
 
         } catch (error) {
             this.logger.error('获取仓库状态失败!' + error.stack)
+            return res.json({
+                ...BaseStdResponse.ERR,
+                msg: '获取仓库状态失败!'
+            })
         }
 
     }