2020-09-04 09:36:23 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/zhenorzz/goploy/core"
|
|
|
|
"github.com/zhenorzz/goploy/model"
|
|
|
|
"github.com/zhenorzz/goploy/utils"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Repository -
|
|
|
|
type Repository struct {
|
|
|
|
ProjectID int64
|
|
|
|
}
|
|
|
|
|
2020-09-04 17:19:53 +08:00
|
|
|
// Create -
|
2020-09-04 09:36:23 +08:00
|
|
|
func (repository Repository) Create() error {
|
2020-09-04 17:19:53 +08:00
|
|
|
srcPath := core.GetProjectPath(repository.ProjectID)
|
|
|
|
if _, err := os.Stat(srcPath); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-04 09:36:23 +08:00
|
|
|
project, err := model.Project{ID: repository.ProjectID}.GetData()
|
|
|
|
if err != nil {
|
|
|
|
core.Log(core.TRACE, "The project does not exist, projectID:"+strconv.FormatInt(repository.ProjectID, 10))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(srcPath); err != nil {
|
|
|
|
core.Log(core.TRACE, "The project fail to remove, projectID:"+strconv.FormatInt(project.ID, 10)+" ,error: "+err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
git := utils.GIT{}
|
2020-09-25 20:05:25 +08:00
|
|
|
if err := git.Clone(project.URL, srcPath); err != nil {
|
2020-09-04 09:36:23 +08:00
|
|
|
core.Log(core.ERROR, "The project fail to initialize, projectID:"+strconv.FormatInt(project.ID, 10)+" ,error: "+err.Error()+", detail: "+git.Err.String())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if project.Branch != "master" {
|
2021-01-28 15:37:34 +08:00
|
|
|
git.Dir = srcPath
|
|
|
|
if err := git.Checkout("-b", project.Branch, "origin/"+project.Branch); err != nil {
|
2020-09-04 09:36:23 +08:00
|
|
|
core.Log(core.ERROR, "The project fail to switch branch, projectID:"+strconv.FormatInt(project.ID, 10)+" ,error: "+err.Error()+", detail: "+git.Err.String())
|
|
|
|
os.RemoveAll(srcPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
core.Log(core.TRACE, "The project success to initialize, projectID:"+strconv.FormatInt(project.ID, 10))
|
|
|
|
return nil
|
|
|
|
}
|