功能开发完成

This commit is contained in:
2025-12-31 16:23:40 +08:00
parent 2b51050ca8
commit 6f0598a859
28 changed files with 5463 additions and 118 deletions

View File

@@ -5,9 +5,9 @@ import (
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/gitcodestatic/gitcodestatic/internal/logger"
"github.com/gitcodestatic/gitcodestatic/internal/service"
"github.com/go-chi/chi/v5"
)
// RepoHandler 仓库API处理器
@@ -23,6 +23,15 @@ func NewRepoHandler(repoService *service.RepoService) *RepoHandler {
}
// AddBatch 批量添加仓库
// @Summary 批量添加仓库
// @Description 批量添加多个Git仓库异步克隆到本地
// @Tags 仓库管理
// @Accept json
// @Produce json
// @Param request body service.AddReposRequest true "仓库URL列表"
// @Success 200 {object} Response{data=service.AddReposResponse}
// @Failure 400 {object} Response
// @Router /repos/batch [post]
func (h *RepoHandler) AddBatch(w http.ResponseWriter, r *http.Request) {
var req service.AddReposRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
@@ -30,8 +39,8 @@ func (h *RepoHandler) AddBatch(w http.ResponseWriter, r *http.Request) {
return
}
if len(req.URLs) == 0 {
respondError(w, http.StatusBadRequest, 40001, "urls cannot be empty")
if len(req.Repos) == 0 {
respondError(w, http.StatusBadRequest, 40001, "repos cannot be empty")
return
}
@@ -46,6 +55,16 @@ func (h *RepoHandler) AddBatch(w http.ResponseWriter, r *http.Request) {
}
// List 获取仓库列表
// @Summary 获取仓库列表
// @Description 分页查询仓库列表,支持按状态筛选
// @Tags 仓库管理
// @Accept json
// @Produce json
// @Param status query string false "状态筛选(pending/cloning/ready/failed)"
// @Param page query int false "页码" default(1)
// @Param page_size query int false "每页数量" default(20)
// @Success 200 {object} Response
// @Router /repos [get]
func (h *RepoHandler) List(w http.ResponseWriter, r *http.Request) {
status := r.URL.Query().Get("status")
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
@@ -76,6 +95,15 @@ func (h *RepoHandler) List(w http.ResponseWriter, r *http.Request) {
}
// Get 获取仓库详情
// @Summary 获取仓库详情
// @Description 根据ID获取仓库详细信息
// @Tags 仓库管理
// @Accept json
// @Produce json
// @Param id path int true "仓库ID"
// @Success 200 {object} Response{data=models.Repository}
// @Failure 404 {object} Response
// @Router /repos/{id} [get]
func (h *RepoHandler) Get(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
if err != nil {
@@ -93,6 +121,16 @@ func (h *RepoHandler) Get(w http.ResponseWriter, r *http.Request) {
}
// SwitchBranch 切换分支
// @Summary 切换仓库分支
// @Description 异步切换仓库到指定分支
// @Tags 仓库管理
// @Accept json
// @Produce json
// @Param id path int true "仓库ID"
// @Param request body object{branch=string} true "分支名称"
// @Success 200 {object} Response{data=models.Task}
// @Failure 400 {object} Response
// @Router /repos/{id}/switch-branch [post]
func (h *RepoHandler) SwitchBranch(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
if err != nil {
@@ -124,6 +162,15 @@ func (h *RepoHandler) SwitchBranch(w http.ResponseWriter, r *http.Request) {
}
// Update 更新仓库
// @Summary 更新仓库
// @Description 异步拉取仓库最新代码(git pull)
// @Tags 仓库管理
// @Accept json
// @Produce json
// @Param id path int true "仓库ID"
// @Success 200 {object} Response{data=models.Task}
// @Failure 400 {object} Response
// @Router /repos/{id}/update [post]
func (h *RepoHandler) Update(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
if err != nil {
@@ -142,6 +189,14 @@ func (h *RepoHandler) Update(w http.ResponseWriter, r *http.Request) {
}
// Reset 重置仓库
// @Summary 重置仓库
// @Description 异步重置仓库到最新状态
// @Tags 仓库管理
// @Produce json
// @Param id path int true "仓库ID"
// @Success 200 {object} Response{data=models.Task}
// @Failure 400 {object} Response
// @Router /repos/{id}/reset [post]
func (h *RepoHandler) Reset(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
if err != nil {
@@ -160,6 +215,14 @@ func (h *RepoHandler) Reset(w http.ResponseWriter, r *http.Request) {
}
// Delete 删除仓库
// @Summary 删除仓库
// @Description 删除指定仓库
// @Tags 仓库管理
// @Produce json
// @Param id path int true "仓库ID"
// @Success 200 {object} Response
// @Failure 400 {object} Response
// @Router /repos/{id} [delete]
func (h *RepoHandler) Delete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
if err != nil {
@@ -175,3 +238,35 @@ func (h *RepoHandler) Delete(w http.ResponseWriter, r *http.Request) {
respondJSON(w, http.StatusOK, 0, "repository deleted successfully", nil)
}
// GetBranches 获取仓库分支列表
// @Summary 获取仓库分支列表
// @Description 获取指定仓库的所有分支
// @Tags 仓库管理
// @Produce json
// @Param id path int true "仓库ID"
// @Success 200 {object} Response{data=object}
// @Failure 400 {object} Response
// @Failure 404 {object} Response
// @Router /repos/{id}/branches [get]
func (h *RepoHandler) GetBranches(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
if err != nil {
respondError(w, http.StatusBadRequest, 40001, "invalid repository id")
return
}
branches, err := h.repoService.GetBranches(r.Context(), id)
if err != nil {
logger.Logger.Error().Err(err).Int64("repo_id", id).Msg("failed to get branches")
respondError(w, http.StatusInternalServerError, 50000, err.Error())
return
}
data := map[string]interface{}{
"branches": branches,
"count": len(branches),
}
respondJSON(w, http.StatusOK, 0, "success", data)
}