goploy/controller/RepositoryController.go

111 lines
2.9 KiB
Go
Raw Normal View History

2021-05-23 22:29:35 +08:00
package controller
import (
"github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model"
2021-08-15 23:27:22 +08:00
"github.com/zhenorzz/goploy/repository"
2021-12-21 16:49:17 +08:00
"github.com/zhenorzz/goploy/response"
"net/http"
2021-05-23 22:29:35 +08:00
"strconv"
)
// Repository struct
type Repository Controller
2021-12-21 16:49:17 +08:00
func (r Repository) Routes() []core.Route {
return []core.Route{
core.NewRoute("/repository/getCommitList", http.MethodGet, r.GetCommitList),
core.NewRoute("/repository/getBranchList", http.MethodGet, r.GetBranchList),
core.NewRoute("/repository/getTagList", http.MethodGet, r.GetTagList),
}
}
2021-05-23 22:29:35 +08:00
// GetCommitList get latest 10 commit list
2021-12-21 16:49:17 +08:00
func (Repository) GetCommitList(gp *core.Goploy) core.Response {
2022-01-08 21:35:57 +08:00
type ReqData struct {
ID int64 `schema:"id" validate:"gt=0"`
Branch string `schema:"branch" validate:"required"`
}
var reqData ReqData
if err := decodeQuery(gp.URLQuery, &reqData); err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
2022-01-08 21:35:57 +08:00
project, err := model.Project{ID: reqData.ID}.GetData()
2021-05-23 22:29:35 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
2021-08-15 23:27:22 +08:00
2021-08-19 14:40:42 +08:00
repo, err := repository.GetRepo(project.RepoType)
2021-08-15 23:27:22 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
2022-01-08 21:35:57 +08:00
list, err := repo.BranchLog(project.ID, reqData.Branch, 10)
2021-08-15 23:27:22 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-08-15 23:27:22 +08:00
}
2021-05-23 22:29:35 +08:00
2021-12-21 16:49:17 +08:00
return response.JSON{
2021-05-23 22:29:35 +08:00
Data: struct {
2021-08-15 23:27:22 +08:00
CommitList []repository.CommitInfo `json:"list"`
}{CommitList: list},
2021-05-23 22:29:35 +08:00
}
}
2021-12-21 16:49:17 +08:00
func (Repository) GetBranchList(gp *core.Goploy) core.Response {
2021-05-23 22:29:35 +08:00
id, err := strconv.ParseInt(gp.URLQuery.Get("id"), 10, 64)
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
project, err := model.Project{ID: id}.GetData()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
2021-08-19 14:40:42 +08:00
repo, err := repository.GetRepo(project.RepoType)
2021-08-16 19:45:07 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
2021-08-16 19:45:07 +08:00
branchList, err := repo.BranchList(project.ID)
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2021-05-23 22:29:35 +08:00
Data: struct {
BranchList []string `json:"list"`
}{BranchList: branchList},
}
}
2021-12-21 16:49:17 +08:00
func (Repository) GetTagList(gp *core.Goploy) core.Response {
2021-05-23 22:29:35 +08:00
id, err := strconv.ParseInt(gp.URLQuery.Get("id"), 10, 64)
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
project, err := model.Project{ID: id}.GetData()
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
2021-08-19 14:40:42 +08:00
repo, err := repository.GetRepo(project.RepoType)
2021-08-15 23:27:22 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
2021-08-16 19:45:07 +08:00
list, err := repo.TagLog(project.ID, 10)
2021-08-15 23:27:22 +08:00
if err != nil {
2021-12-21 16:49:17 +08:00
return response.JSON{Code: response.Error, Message: err.Error()}
2021-05-23 22:29:35 +08:00
}
2021-12-21 16:49:17 +08:00
return response.JSON{
2021-05-23 22:29:35 +08:00
Data: struct {
2021-08-15 23:27:22 +08:00
TagList []repository.CommitInfo `json:"list"`
}{TagList: list},
2021-05-23 22:29:35 +08:00
}
}