goploy/model/ProjectModel.go

451 lines
10 KiB
Go
Raw Normal View History

2020-08-04 14:28:25 +08:00
package model
import (
"fmt"
sq "github.com/Masterminds/squirrel"
)
const projectTable = "`project`"
2020-08-15 13:38:06 +08:00
// Project -
2020-08-04 14:28:25 +08:00
type Project struct {
ID int64 `json:"id"`
NamespaceID int64 `json:"namespaceId"`
UserID int64 `json:"userId,omitempty"`
Name string `json:"name"`
URL string `json:"url"`
Path string `json:"path"`
2020-09-02 17:08:11 +08:00
Environment uint8 `json:"environment"`
2020-08-04 14:28:25 +08:00
Branch string `json:"branch"`
2020-09-25 20:05:25 +08:00
SymlinkPath string `json:"symlinkPath"`
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"`
AutoDeploy uint8 `json:"autoDeploy"`
PublisherID int64 `json:"publisherId"`
PublisherName string `json:"publisherName"`
PublishExt string `json:"publishExt"`
DeployState uint8 `json:"deployState"`
LastPublishToken string `json:"lastPublishToken"`
NotifyType uint8 `json:"notifyType"`
NotifyTarget string `json:"notifyTarget"`
State uint8 `json:"state"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
}
2020-08-15 13:38:06 +08:00
// Project deploy state
2020-08-04 14:28:25 +08:00
const (
ProjectNotDeploy = 0
ProjectDeploying = 1
ProjectSuccess = 2
ProjectFail = 3
)
2020-08-15 13:38:06 +08:00
// Project deploy type
2020-08-04 14:28:25 +08:00
const (
ProjectManualDeploy = 0
ProjectWebhookDeploy = 1
)
2020-08-15 13:38:06 +08:00
// Project notify type
2020-08-04 14:28:25 +08:00
const (
2020-08-04 20:00:21 +08:00
NotifyWeiXin = 1
NotifyDingTalk = 2
NotifyFeiShu = 3
NotifyCustom = 255
2020-08-04 14:28:25 +08:00
)
2020-08-15 13:38:06 +08:00
// Projects -
2020-08-04 14:28:25 +08:00
type Projects []Project
2020-08-15 13:38:06 +08:00
// AddRow return LastInsertId
2020-08-04 14:28:25 +08:00
func (p Project) AddRow() (int64, error) {
result, err := sq.
Insert(projectTable).
2020-09-25 20:05:25 +08:00
Columns(
"namespace_id",
"name",
"url",
"path",
"environment",
"branch",
"symlink_path",
"review",
"review_url",
"after_pull_script_mode",
"after_pull_script",
"after_deploy_script_mode",
"after_deploy_script",
"rsync_option",
"notify_type",
"notify_target",
).
Values(
p.NamespaceID,
p.Name,
p.URL,
p.Path,
p.Environment,
p.Branch,
p.SymlinkPath,
p.Review,
p.ReviewURL,
p.AfterPullScriptMode,
p.AfterPullScript,
p.AfterDeployScriptMode,
p.AfterDeployScript,
p.RsyncOption,
p.NotifyType,
p.NotifyTarget,
).
2020-08-04 14:28:25 +08:00
RunWith(DB).
Exec()
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
return id, err
}
2020-08-15 13:38:06 +08:00
// EditRow -
2020-08-04 14:28:25 +08:00
func (p Project) EditRow() error {
_, err := sq.
Update(projectTable).
SetMap(sq.Eq{
"name": p.Name,
"url": p.URL,
"path": p.Path,
"environment": p.Environment,
"branch": p.Branch,
2020-09-25 20:05:25 +08:00
"symlink_path": p.SymlinkPath,
"review": p.Review,
"review_url": p.ReviewURL,
2020-08-04 14:28:25 +08:00
"after_pull_script_mode": p.AfterPullScriptMode,
"after_pull_script": p.AfterPullScript,
"after_deploy_script_mode": p.AfterDeployScriptMode,
"after_deploy_script": p.AfterDeployScript,
"rsync_option": p.RsyncOption,
"notify_type": p.NotifyType,
"notify_target": p.NotifyTarget,
}).
Where(sq.Eq{"id": p.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-20 14:16:37 +08:00
// SetAutoDeploy set auto_deploy
func (p Project) SetAutoDeploy() error {
_, err := sq.
Update(projectTable).
SetMap(sq.Eq{
"auto_deploy": p.AutoDeploy,
}).
Where(sq.Eq{"id": p.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-15 13:38:06 +08:00
// RemoveRow -
2020-08-04 14:28:25 +08:00
func (p Project) RemoveRow() error {
_, err := sq.
Update(projectTable).
SetMap(sq.Eq{
"state": Disable,
}).
Where(sq.Eq{"id": p.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-15 13:38:06 +08:00
// Publish project
2020-08-04 14:28:25 +08:00
func (p Project) Publish() error {
_, err := sq.
Update(projectTable).
SetMap(sq.Eq{
"publisher_id": p.PublisherID,
"publisher_name": p.PublisherName,
"deploy_state": p.DeployState,
"last_publish_token": p.LastPublishToken,
}).
Where(sq.Eq{"id": p.ID}).
RunWith(DB).
Exec()
return err
}
2020-11-23 17:49:38 +08:00
// ResetState set deploy_state to NotDeploy
func (p Project) ResetState() error {
_, err := sq.
Update(projectTable).
SetMap(sq.Eq{
"deploy_state": ProjectNotDeploy,
}).
Where(sq.Eq{"id": p.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-15 13:38:06 +08:00
// DeploySuccess set deploy_state to success
2020-08-04 14:28:25 +08:00
func (p Project) DeploySuccess() error {
_, err := sq.
Update(projectTable).
SetMap(sq.Eq{
"deploy_state": ProjectSuccess,
}).
Where(sq.Eq{"id": p.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-15 13:38:06 +08:00
// DeployFail set deploy_state to fail
2020-08-04 14:28:25 +08:00
func (p Project) DeployFail() error {
_, err := sq.
Update(projectTable).
SetMap(sq.Eq{
"deploy_state": ProjectFail,
}).
Where(sq.Eq{"id": p.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-15 13:38:06 +08:00
// GetList -
2020-08-04 14:28:25 +08:00
func (p Project) GetList(pagination Pagination) (Projects, error) {
builder := sq.
2020-09-25 20:05:25 +08:00
Select(`
project.id,
name,
url,
path,
environment,
branch,
symlink_path,
review,
review_url,
after_pull_script_mode,
after_pull_script,
after_deploy_script_mode,
after_deploy_script,
rsync_option,
auto_deploy,
notify_type,
notify_target,
project.insert_time,
project.update_time
`).
2020-08-04 14:28:25 +08:00
From(projectTable).
Join(projectUserTable + " ON project_user.project_id = project.id").
Where(sq.Eq{
"namespace_id": p.NamespaceID,
"user_id": p.UserID,
"state": Enable,
})
if len(p.Name) > 0 {
builder = builder.Where(sq.Like{"name": "%" + p.Name + "%"})
}
rows, err := builder.Limit(pagination.Rows).
Offset((pagination.Page - 1) * pagination.Rows).
OrderBy("id DESC").
RunWith(DB).
Query()
if err != nil {
return nil, err
}
projects := Projects{}
for rows.Next() {
var project Project
if err := rows.Scan(
&project.ID,
&project.Name,
&project.URL,
&project.Path,
&project.Environment,
&project.Branch,
2020-09-25 20:05:25 +08:00
&project.SymlinkPath,
&project.Review,
&project.ReviewURL,
2020-08-04 14:28:25 +08:00
&project.AfterPullScriptMode,
&project.AfterPullScript,
&project.AfterDeployScriptMode,
&project.AfterDeployScript,
&project.RsyncOption,
&project.AutoDeploy,
&project.NotifyType,
&project.NotifyTarget,
&project.InsertTime,
&project.UpdateTime,
); err != nil {
return nil, err
}
projects = append(projects, project)
}
return projects, nil
}
2020-08-15 13:38:06 +08:00
// GetTotal -
2020-08-04 14:28:25 +08:00
func (p Project) GetTotal() (int64, error) {
var total int64
builder := sq.
Select("COUNT(*) AS count").
From(projectTable).
Join(projectUserTable + " ON project_user.project_id = project.id").
Where(sq.Eq{
"namespace_id": p.NamespaceID,
"user_id": p.UserID,
"state": Enable,
})
if len(p.Name) > 0 {
builder = builder.Where(sq.Like{"name": "%" + p.Name + "%"})
}
err := builder.RunWith(DB).
QueryRow().
Scan(&total)
if err != nil {
return 0, err
}
return total, nil
}
2020-08-04 20:00:21 +08:00
// GetUserProjectList -
2020-08-17 18:24:15 +08:00
func (p Project) GetUserProjectList() (Projects, error) {
2021-02-14 14:22:22 +08:00
rows, err := sq.
2020-08-04 14:28:25 +08:00
Select(`
project.id,
project.name,
2020-08-14 20:03:35 +08:00
project.url,
2020-08-04 14:28:25 +08:00
project.publisher_id,
project.publisher_name,
IFNULL(publish_trace.ext, '{}'),
project.environment,
project.branch,
2020-09-25 20:05:25 +08:00
project.review,
2020-08-04 14:28:25 +08:00
project.last_publish_token,
project.deploy_state,
project.update_time`).
From(projectUserTable).
LeftJoin(projectTable + " ON project_user.project_id = project.id").
LeftJoin(fmt.Sprintf("%[1]s ON %[1]s.token = %s.last_publish_token and type = %d", publishTraceTable, projectTable, Pull)).
Where(sq.Eq{
"project.namespace_id": p.NamespaceID,
2020-08-17 18:24:15 +08:00
"project_user.user_id": p.UserID,
2020-08-04 14:28:25 +08:00
"project.state": Enable,
}).
2021-02-14 14:22:22 +08:00
OrderBy("project.id DESC").
RunWith(DB).
Query()
2020-08-04 14:28:25 +08:00
if err != nil {
return nil, err
}
projects := Projects{}
for rows.Next() {
var project Project
if err := rows.Scan(
&project.ID,
&project.Name,
2020-08-14 20:03:35 +08:00
&project.URL,
2020-08-04 14:28:25 +08:00
&project.PublisherID,
&project.PublisherName,
&project.PublishExt,
&project.Environment,
&project.Branch,
2020-09-25 20:05:25 +08:00
&project.Review,
2020-08-04 14:28:25 +08:00
&project.LastPublishToken,
&project.DeployState,
&project.UpdateTime); err != nil {
return projects, err
}
projects = append(projects, project)
}
return projects, nil
}
2020-08-15 13:38:06 +08:00
// GetData -
2020-08-04 14:28:25 +08:00
func (p Project) GetData() (Project, error) {
var project Project
err := sq.
2021-02-12 23:05:06 +08:00
Select("id, namespace_id, name, url, path, environment, branch, symlink_path, review, review_url, after_pull_script_mode, after_pull_script, after_deploy_script_mode, after_deploy_script, rsync_option, auto_deploy, deploy_state, notify_type, notify_target, project.last_publish_token, insert_time, update_time").
2020-08-04 14:28:25 +08:00
From(projectTable).
Where(sq.Eq{"id": p.ID}).
RunWith(DB).
QueryRow().
Scan(
&project.ID,
&project.NamespaceID,
&project.Name,
&project.URL,
&project.Path,
&project.Environment,
&project.Branch,
2020-09-25 20:05:25 +08:00
&project.SymlinkPath,
&project.Review,
&project.ReviewURL,
2020-08-04 14:28:25 +08:00
&project.AfterPullScriptMode,
&project.AfterPullScript,
&project.AfterDeployScriptMode,
&project.AfterDeployScript,
&project.RsyncOption,
&project.AutoDeploy,
&project.DeployState,
&project.NotifyType,
&project.NotifyTarget,
2021-02-12 23:05:06 +08:00
&project.LastPublishToken,
2020-08-04 14:28:25 +08:00
&project.InsertTime,
&project.UpdateTime)
if err != nil {
return project, err
}
return project, nil
}
2020-08-15 13:38:06 +08:00
// GetUserProjectData -
2020-08-19 10:22:43 +08:00
func (p Project) GetUserProjectData() (Project, error) {
2020-08-04 14:28:25 +08:00
var project Project
err := sq.
Select("project_id, project.name, publisher_id, publisher_name, project.environment, project.branch, project.last_publish_token, project.deploy_state").
From(projectUserTable).
LeftJoin(projectTable+" ON project_user.project_id = project.id").
Where(sq.Eq{
"project_user.project_id": p.ID,
2020-08-19 10:22:43 +08:00
"project_user.user_id": p.UserID,
2020-08-04 14:28:25 +08:00
"project.state": Enable,
}).
RunWith(DB).
QueryRow().
Scan(&project.ID,
&project.Name,
&project.PublisherID,
&project.PublisherName,
&project.Environment,
&project.Branch,
&project.LastPublishToken,
&project.DeployState)
if err != nil {
return project, err
}
return project, nil
}