goploy/controller/ProjectController.go

730 lines
22 KiB
Go
Raw Normal View History

2020-08-04 14:28:25 +08:00
package controller
import (
"bytes"
2021-01-06 17:29:25 +08:00
"fmt"
2020-08-04 14:28:25 +08:00
"github.com/zhenorzz/goploy/core"
"github.com/zhenorzz/goploy/model"
"github.com/zhenorzz/goploy/utils"
2021-01-06 17:29:25 +08:00
"io/ioutil"
2020-08-04 14:28:25 +08:00
"os"
"os/exec"
2021-01-06 17:29:25 +08:00
"path"
2020-08-04 14:28:25 +08:00
"strconv"
"strings"
)
// Project struct
type Project Controller
2020-08-15 13:38:06 +08:00
// GetList -
2020-09-26 10:45:28 +08:00
func (Project) GetList(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
pagination, err := model.PaginationFrom(gp.URLQuery)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectName := gp.URLQuery.Get("projectName")
projectList, err := model.Project{NamespaceID: gp.Namespace.ID, UserID: gp.UserInfo.ID, Name: projectName}.GetList(pagination)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
Projects model.Projects `json:"list"`
}{Projects: projectList},
}
2020-08-04 14:28:25 +08:00
}
2020-08-15 13:38:06 +08:00
// GetTotal -
2020-09-26 10:45:28 +08:00
func (Project) GetTotal(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
var total int64
var err error
projectName := gp.URLQuery.Get("projectName")
total, err = model.Project{NamespaceID: gp.Namespace.ID, UserID: gp.UserInfo.ID, Name: projectName}.GetTotal()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
Total int64 `json:"total"`
}{Total: total},
}
2020-08-04 14:28:25 +08:00
}
2020-08-15 13:38:06 +08:00
// GetRemoteBranchList -
2020-09-26 10:45:28 +08:00
func (Project) GetRemoteBranchList(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
url := gp.URLQuery.Get("url")
2021-01-07 18:32:17 +08:00
if strings.Contains(url, "git@") {
host := strings.Split(url, "git@")[1]
host = strings.Split(host, ":")[0]
2021-01-28 15:37:34 +08:00
homeDir, err := os.UserHomeDir()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
knownHostsPath := homeDir + "/.ssh/known_hosts"
2021-01-07 18:32:17 +08:00
var cmdOutbuf, cmdErrbuf bytes.Buffer
cmd := exec.Command("ssh-keygen", "-F", host, "-f", knownHostsPath)
cmd.Stdout = &cmdOutbuf
cmd.Stderr = &cmdErrbuf
if err := cmd.Run(); err != nil {
cmdOutbuf.Reset()
cmdErrbuf.Reset()
cmd := exec.Command("ssh-keyscan", host)
cmd.Stdout = &cmdOutbuf
cmd.Stderr = &cmdErrbuf
if err := cmd.Run(); err != nil {
return &core.Response{Code: core.Error, Message: cmdErrbuf.String()}
}
f, err := os.OpenFile(knownHostsPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
defer f.Close()
if _, err := f.Write(cmdOutbuf.Bytes()); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
}
}
2020-08-04 14:28:25 +08:00
cmd := exec.Command("git", "ls-remote", "-h", url)
var cmdOutbuf, cmdErrbuf bytes.Buffer
cmd.Stdout = &cmdOutbuf
cmd.Stderr = &cmdErrbuf
if err := cmd.Run(); err != nil {
return &core.Response{Code: core.Error, Message: cmdErrbuf.String()}
}
var branch []string
for _, branchWithSha := range strings.Split(cmdOutbuf.String(), "\n") {
if len(branchWithSha) != 0 {
branchWithShaSlice := strings.Fields(branchWithSha)
branchWithHead := branchWithShaSlice[len(branchWithShaSlice)-1]
branchWithHeadSlice := strings.Split(branchWithHead, "/")
branch = append(branch, branchWithHeadSlice[len(branchWithHeadSlice)-1])
}
}
2020-09-26 11:45:44 +08:00
return &core.Response{Data: struct {
Branch []string `json:"branch"`
}{Branch: branch}}
2020-08-04 14:28:25 +08:00
}
2020-08-15 13:38:06 +08:00
// GetBindServerList -
2020-09-26 10:45:28 +08:00
func (Project) GetBindServerList(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
id, err := strconv.ParseInt(gp.URLQuery.Get("id"), 10, 64)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectServers, err := model.ProjectServer{ProjectID: id}.GetBindServerListByProjectID()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
ProjectServers model.ProjectServers `json:"list"`
}{ProjectServers: projectServers},
}
2020-08-04 14:28:25 +08:00
}
2020-08-15 13:38:06 +08:00
// GetBindUserList -
2020-09-26 10:45:28 +08:00
func (Project) GetBindUserList(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
id, err := strconv.ParseInt(gp.URLQuery.Get("id"), 10, 64)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectUsers, err := model.ProjectUser{ProjectID: id, NamespaceID: gp.Namespace.ID}.GetBindUserListByProjectID()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
ProjectUsers model.ProjectUsers `json:"list"`
}{ProjectUsers: projectUsers},
}
2020-08-04 14:28:25 +08:00
}
2021-01-06 17:29:25 +08:00
// GetProjectFileList -
func (Project) GetProjectFileList(gp *core.Goploy) *core.Response {
id, err := strconv.ParseInt(gp.URLQuery.Get("id"), 10, 64)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectFiles, err := model.ProjectFile{ProjectID: id}.GetListByProjectID()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{
Data: struct {
ProjectFiles model.ProjectFiles `json:"list"`
}{ProjectFiles: projectFiles},
}
}
// GetProjectFileContent -
func (Project) GetProjectFileContent(gp *core.Goploy) *core.Response {
id, err := strconv.ParseInt(gp.URLQuery.Get("id"), 10, 64)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectFileData, err := model.ProjectFile{ID: id}.GetData()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
fileBytes, err := ioutil.ReadFile(path.Join(core.GetProjectFilePath(projectFileData.ProjectID), projectFileData.Filename))
if err != nil {
fmt.Println("read fail", err)
}
return &core.Response{
Data: struct {
Content string `json:"content"`
}{Content: string(fileBytes)},
}
}
2020-08-15 13:38:06 +08:00
// Add project
2020-09-26 10:45:28 +08:00
func (Project) Add(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
2020-08-19 10:22:43 +08:00
Name string `json:"name" validate:"required"`
URL string `json:"url" validate:"required"`
Path string `json:"path" validate:"required"`
2020-09-02 17:08:11 +08:00
Environment uint8 `json:"Environment" validate:"required"`
2020-08-19 10:22:43 +08:00
Branch string `json:"branch" validate:"required"`
2020-08-04 14:28:25 +08:00
SymlinkPath string `json:"symlinkPath"`
2020-09-25 20:05:25 +08:00
Review uint8 `json:"review"`
ReviewURL string `json:"reviewURL"`
2020-08-04 14:28:25 +08:00
AfterPullScriptMode string `json:"afterPullScriptMode"`
AfterPullScript string `json:"afterPullScript"`
AfterDeployScriptMode string `json:"afterDeployScriptMode"`
AfterDeployScript string `json:"afterDeployScript"`
RsyncOption string `json:"rsyncOption"`
ServerIDs []int64 `json:"serverIds"`
UserIDs []int64 `json:"userIds"`
NotifyType uint8 `json:"notifyType"`
NotifyTarget string `json:"notifyTarget"`
}
var reqData ReqData
2020-08-19 10:22:43 +08:00
if err := verify(gp.Body, &reqData); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
if _, err := utils.ParseCommandLine(reqData.RsyncOption); err != nil {
return &core.Response{Code: core.Error, Message: "Invalid rsync option format"}
}
projectID, err := model.Project{
NamespaceID: gp.Namespace.ID,
Name: reqData.Name,
URL: reqData.URL,
Path: reqData.Path,
Environment: reqData.Environment,
Branch: reqData.Branch,
2020-09-25 20:05:25 +08:00
SymlinkPath: reqData.SymlinkPath,
Review: reqData.Review,
ReviewURL: reqData.ReviewURL,
2020-08-04 14:28:25 +08:00
AfterPullScriptMode: reqData.AfterPullScriptMode,
AfterPullScript: reqData.AfterPullScript,
AfterDeployScriptMode: reqData.AfterDeployScriptMode,
AfterDeployScript: reqData.AfterDeployScript,
RsyncOption: reqData.RsyncOption,
NotifyType: reqData.NotifyType,
NotifyTarget: reqData.NotifyTarget,
}.AddRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectServersModel := model.ProjectServers{}
for _, serverID := range reqData.ServerIDs {
projectServerModel := model.ProjectServer{
ProjectID: projectID,
ServerID: serverID,
}
projectServersModel = append(projectServersModel, projectServerModel)
}
if err := projectServersModel.AddMany(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectUsersModel := model.ProjectUsers{}
for _, userID := range reqData.UserIDs {
projectUserModel := model.ProjectUser{
ProjectID: projectID,
UserID: userID,
}
projectUsersModel = append(projectUsersModel, projectUserModel)
}
namespaceUsers, err := model.NamespaceUser{NamespaceID: gp.Namespace.ID}.GetAllGteManagerByNamespaceID()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
for _, namespaceUser := range namespaceUsers {
projectUserModel := model.ProjectUser{
ProjectID: projectID,
UserID: namespaceUser.UserID,
}
projectUsersModel = append(projectUsersModel, projectUserModel)
}
if err := projectUsersModel.AddMany(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}
2020-08-15 13:38:06 +08:00
// Edit project
2020-09-26 10:45:28 +08:00
func (Project) Edit(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
2020-08-19 10:22:43 +08:00
ID int64 `json:"id" validate:"gt=0"`
2020-08-04 14:28:25 +08:00
Name string `json:"name"`
URL string `json:"url"`
Path string `json:"path"`
SymlinkPath string `json:"symlinkPath"`
2020-09-25 20:05:25 +08:00
Review uint8 `json:"review"`
ReviewURL string `json:"reviewURL"`
2020-09-02 17:08:11 +08:00
Environment uint8 `json:"Environment"`
2020-08-04 14:28:25 +08:00
Branch string `json:"branch"`
AfterPullScriptMode string `json:"afterPullScriptMode"`
AfterPullScript string `json:"afterPullScript"`
AfterDeployScriptMode string `json:"afterDeployScriptMode"`
AfterDeployScript string `json:"afterDeployScript"`
RsyncOption string `json:"rsyncOption"`
NotifyType uint8 `json:"notifyType"`
NotifyTarget string `json:"notifyTarget"`
}
var reqData ReqData
2020-08-19 10:22:43 +08:00
if err := verify(gp.Body, &reqData); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
if _, err := utils.ParseCommandLine(reqData.RsyncOption); err != nil {
return &core.Response{Code: core.Error, Message: "Invalid rsync option format"}
}
projectData, err := model.Project{ID: reqData.ID}.GetData()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
err = model.Project{
ID: reqData.ID,
Name: reqData.Name,
URL: reqData.URL,
Path: reqData.Path,
Environment: reqData.Environment,
Branch: reqData.Branch,
2020-09-25 20:05:25 +08:00
SymlinkPath: reqData.SymlinkPath,
Review: reqData.Review,
ReviewURL: reqData.ReviewURL,
2020-08-04 14:28:25 +08:00
AfterPullScriptMode: reqData.AfterPullScriptMode,
AfterPullScript: reqData.AfterPullScript,
AfterDeployScriptMode: reqData.AfterDeployScriptMode,
AfterDeployScript: reqData.AfterDeployScript,
RsyncOption: reqData.RsyncOption,
NotifyType: reqData.NotifyType,
NotifyTarget: reqData.NotifyTarget,
}.EditRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
if reqData.URL != projectData.URL {
2020-09-04 17:19:53 +08:00
srcPath := core.GetProjectPath(projectData.ID)
2020-08-04 14:28:25 +08:00
_, err := os.Stat(srcPath)
if err == nil || os.IsNotExist(err) == false {
repo := reqData.URL
cmd := exec.Command("git", "remote", "set-url", "origin", repo)
cmd.Dir = srcPath
if err := cmd.Run(); err != nil {
return &core.Response{Code: core.Error, Message: "Project change url fail, you can do it manually, reason: " + err.Error()}
}
}
}
return &core.Response{}
}
2020-08-20 14:16:37 +08:00
// SetAutoDeploy -
2020-09-26 10:45:28 +08:00
func (Project) SetAutoDeploy(gp *core.Goploy) *core.Response {
2020-08-20 14:16:37 +08:00
type ReqData struct {
ID int64 `json:"id" validate:"gt=0"`
AutoDeploy uint8 `json:"autoDeploy" validate:"gte=0"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
err := model.Project{
ID: reqData.ID,
AutoDeploy: reqData.AutoDeploy,
}.SetAutoDeploy()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}
2020-08-15 13:38:06 +08:00
// RemoveRow Project
2020-09-26 10:45:28 +08:00
func (Project) Remove(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
2020-08-19 10:22:43 +08:00
ID int64 `json:"id" validate:"gt=0"`
2020-08-04 14:28:25 +08:00
}
var reqData ReqData
2020-08-19 10:22:43 +08:00
if err := verify(gp.Body, &reqData); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectData, err := model.Project{ID: reqData.ID}.GetData()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2021-01-06 17:29:25 +08:00
srcPath := core.GetProjectPath(projectData.ID)
if err := os.RemoveAll(srcPath); err != nil {
return &core.Response{Code: core.Error, Message: "Delete folder fail, Detail: " + err.Error()}
}
2020-08-19 10:22:43 +08:00
if err := (model.Project{ID: reqData.ID}).RemoveRow(); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
2021-01-06 17:29:25 +08:00
return &core.Response{}
}
// UploadFile -
func (Project) UploadFile(gp *core.Goploy) *core.Response {
file, _, err := gp.Request.FormFile("file")
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
defer file.Close()
id, err := strconv.ParseInt(gp.URLQuery.Get("projectFileId"), 10, 64)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectID, err := strconv.ParseInt(gp.URLQuery.Get("projectId"), 10, 64)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
filename := gp.URLQuery.Get("filename")
filePath := path.Join(core.GetProjectFilePath(projectID), filename)
if _, err := os.Stat(path.Dir(filePath)); err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(path.Dir(filePath), 0755)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
} else {
return &core.Response{Code: core.Error, Message: err.Error()}
}
}
// read all of the contents of our uploaded file into a
// byte array
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
if err := ioutil.WriteFile(filePath, fileBytes, 0755); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
if id == 0 {
id, err = model.ProjectFile{
Filename: filename,
ProjectID: projectID,
}.AddRow()
} else {
err = model.ProjectFile{
ID: id,
Filename: filename,
ProjectID: projectID,
}.EditRow()
}
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{
Data: struct {
ID int64 `json:"id"`
}{ID: id},
}
}
// AddFile to project
func (Project) AddFile(gp *core.Goploy) *core.Response {
type ReqData struct {
ProjectID int64 `json:"projectId" validate:"gt=0"`
Content string `json:"content" validate:"required"`
Filename string `json:"filename" validate:"required"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
file, err := os.Create(path.Join(core.GetProjectFilePath(reqData.ProjectID), reqData.Filename))
if err != nil {
panic(err)
}
defer file.Close()
file.WriteString(reqData.Content)
id, err := model.ProjectFile{
ProjectID: reqData.ProjectID,
Filename: reqData.Filename,
}.AddRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{
Data: struct {
ID int64 `json:"id"`
}{ID: id},
}
}
// EditFile to project
func (Project) EditFile(gp *core.Goploy) *core.Response {
type ReqData struct {
ID int64 `json:"id" validate:"gt=0"`
Content string `json:"content" validate:"required"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectFileData, err := model.ProjectFile{ID: reqData.ID}.GetData()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
file, err := os.Create(path.Join(core.GetProjectFilePath(projectFileData.ProjectID), projectFileData.Filename))
if err != nil {
panic(err)
}
defer file.Close()
file.WriteString(reqData.Content)
return &core.Response{}
}
// RemoveFile from Project
func (Project) RemoveFile(gp *core.Goploy) *core.Response {
type ReqData struct {
ProjectFileID int64 `json:"projectFileId" validate:"gt=0"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectFileData, err := model.ProjectFile{ID: reqData.ProjectFileID}.GetData()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
if err := os.Remove(path.Join(core.GetProjectFilePath(projectFileData.ProjectID), projectFileData.Filename)); err != nil {
return &core.Response{Code: core.Error, Message: "Delete file fail, Detail: " + err.Error()}
}
if err := (model.ProjectFile{ID: reqData.ProjectFileID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
2020-08-04 14:28:25 +08:00
}
return &core.Response{}
}
2020-08-15 13:38:06 +08:00
// AddServer to project
2020-09-26 10:45:28 +08:00
func (Project) AddServer(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
ProjectID int64 `json:"projectId" validate:"gt=0"`
ServerIDs []int64 `json:"serverIds" validate:"required"`
}
var reqData ReqData
2020-08-19 10:22:43 +08:00
if err := verify(gp.Body, &reqData); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectID := reqData.ProjectID
projectServersModel := model.ProjectServers{}
for _, serverID := range reqData.ServerIDs {
projectServerModel := model.ProjectServer{
ProjectID: projectID,
ServerID: serverID,
}
projectServersModel = append(projectServersModel, projectServerModel)
}
if err := projectServersModel.AddMany(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}
2021-01-06 17:29:25 +08:00
// RemoveServer from Project
func (Project) RemoveServer(gp *core.Goploy) *core.Response {
type ReqData struct {
ProjectServerID int64 `json:"projectServerId" validate:"gt=0"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
if err := (model.ProjectServer{ID: reqData.ProjectServerID}).DeleteRow(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}
2020-08-15 13:38:06 +08:00
// AddUser to project
2020-09-26 10:45:28 +08:00
func (Project) AddUser(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
ProjectID int64 `json:"projectId" validate:"gt=0"`
UserIDs []int64 `json:"userIds" validate:"required"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectID := reqData.ProjectID
projectUsersModel := model.ProjectUsers{}
for _, userID := range reqData.UserIDs {
projectUserModel := model.ProjectUser{
ProjectID: projectID,
UserID: userID,
}
projectUsersModel = append(projectUsersModel, projectUserModel)
}
if err := projectUsersModel.AddMany(); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}
2020-08-15 13:38:06 +08:00
// RemoveUser from Project
2020-09-26 10:45:28 +08:00
func (Project) RemoveUser(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
ProjectUserID int64 `json:"projectUserId" validate:"gt=0"`
}
var reqData ReqData
2020-08-19 10:22:43 +08:00
if err := verify(gp.Body, &reqData); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-08-19 10:22:43 +08:00
if err := (model.ProjectUser{ID: reqData.ProjectUserID}).DeleteRow(); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}
2020-08-15 13:38:06 +08:00
// GetTaskList -
2020-09-26 10:45:28 +08:00
func (Project) GetTaskList(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
pagination, err := model.PaginationFrom(gp.URLQuery)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
id, err := strconv.ParseInt(gp.URLQuery.Get("id"), 10, 64)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
projectTaskList, pagination, err := model.ProjectTask{ProjectID: id}.GetListByProjectID(pagination)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
ProjectTasks model.ProjectTasks `json:"list"`
Pagination model.Pagination `json:"pagination"`
}{ProjectTasks: projectTaskList, Pagination: pagination},
}
2020-09-25 20:05:25 +08:00
}
// GetReviewList -
2020-09-26 10:45:28 +08:00
func (Project) GetReviewList(gp *core.Goploy) *core.Response {
2020-09-25 20:05:25 +08:00
pagination, err := model.PaginationFrom(gp.URLQuery)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
id, err := strconv.ParseInt(gp.URLQuery.Get("id"), 10, 64)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
ProjectReviews, pagination, err := model.ProjectReview{ProjectID: id}.GetListByProjectID(pagination)
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
ProjectReviews model.ProjectReviews `json:"list"`
Pagination model.Pagination `json:"pagination"`
}{ProjectReviews: ProjectReviews, Pagination: pagination},
}
2020-08-04 14:28:25 +08:00
}
2020-08-15 13:38:06 +08:00
// AddTask to project
2020-09-26 10:45:28 +08:00
func (Project) AddTask(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
ProjectID int64 `json:"projectId" validate:"gt=0"`
2020-12-03 17:05:03 +08:00
Branch string `json:"branch" validate:"required"`
2021-04-07 18:31:31 +08:00
CommitID string `json:"commit" validate:"len=40"`
2020-08-04 14:28:25 +08:00
Date string `json:"date" validate:"required"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
id, err := model.ProjectTask{
ProjectID: reqData.ProjectID,
CommitID: reqData.CommitID,
2020-12-03 17:05:03 +08:00
Branch: reqData.Branch,
2020-08-04 14:28:25 +08:00
Date: reqData.Date,
Creator: gp.UserInfo.Name,
CreatorID: gp.UserInfo.ID,
}.AddRow()
if err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-09-26 11:45:44 +08:00
return &core.Response{
Data: struct {
ID int64 `json:"id"`
}{ID: id},
2020-08-04 14:28:25 +08:00
}
}
2020-08-15 13:38:06 +08:00
// RemoveTask from project
2020-09-26 10:45:28 +08:00
func (Project) RemoveTask(gp *core.Goploy) *core.Response {
2020-08-04 14:28:25 +08:00
type ReqData struct {
ID int64 `json:"id" validate:"gt=0"`
}
var reqData ReqData
if err := verify(gp.Body, &reqData); err != nil {
return &core.Response{Code: core.Error, Message: err.Error()}
}
2020-08-19 10:22:43 +08:00
if err := (model.ProjectTask{ID: reqData.ID}).RemoveRow(); err != nil {
2020-08-04 14:28:25 +08:00
return &core.Response{Code: core.Error, Message: err.Error()}
}
return &core.Response{}
}