2023-09-23 17:12:35 +02:00
|
|
|
package db_lib
|
2022-01-30 12:22:18 +01:00
|
|
|
|
|
|
|
import (
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/util"
|
2022-01-30 12:22:18 +01:00
|
|
|
"os"
|
2024-05-22 19:00:28 +02:00
|
|
|
"path"
|
2023-02-26 07:22:47 +01:00
|
|
|
|
2024-10-26 14:56:17 +02:00
|
|
|
"github.com/semaphoreui/semaphore/db"
|
|
|
|
"github.com/semaphoreui/semaphore/pkg/task_logger"
|
2022-01-30 12:22:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type GitRepositoryDirType int
|
|
|
|
|
|
|
|
const (
|
2024-05-22 19:00:28 +02:00
|
|
|
GitRepositoryTmpPath GitRepositoryDirType = iota
|
|
|
|
GitRepositoryFullPath
|
2022-01-30 12:22:18 +01:00
|
|
|
)
|
|
|
|
|
2023-02-26 07:22:47 +01:00
|
|
|
type GitClient interface {
|
|
|
|
Clone(r GitRepository) error
|
|
|
|
Pull(r GitRepository) error
|
|
|
|
Checkout(r GitRepository, target string) error
|
|
|
|
CanBePulled(r GitRepository) bool
|
|
|
|
GetLastCommitMessage(r GitRepository) (msg string, err error)
|
|
|
|
GetLastCommitHash(r GitRepository) (hash string, err error)
|
|
|
|
GetLastRemoteCommitHash(r GitRepository) (hash string, err error)
|
|
|
|
}
|
|
|
|
|
2022-01-30 12:22:18 +01:00
|
|
|
type GitRepository struct {
|
2024-05-22 19:00:28 +02:00
|
|
|
TmpDirName string
|
2022-01-30 12:22:18 +01:00
|
|
|
TemplateID int
|
|
|
|
Repository db.Repository
|
2024-04-12 12:32:54 +02:00
|
|
|
Logger task_logger.Logger
|
2023-02-26 07:22:47 +01:00
|
|
|
Client GitClient
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
2024-05-22 19:00:28 +02:00
|
|
|
func (r GitRepository) GetFullPath() string {
|
|
|
|
if r.TmpDirName != "" {
|
|
|
|
return path.Join(util.Config.TmpPath, r.TmpDirName)
|
|
|
|
}
|
|
|
|
return r.Repository.GetFullPath(r.TemplateID)
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-26 07:22:47 +01:00
|
|
|
func (r GitRepository) ValidateRepo() error {
|
|
|
|
_, err := os.Stat(r.GetFullPath())
|
|
|
|
return err
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) Clone() error {
|
2023-02-26 07:22:47 +01:00
|
|
|
return r.Client.Clone(r)
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) Pull() error {
|
2023-02-26 07:22:47 +01:00
|
|
|
return r.Client.Pull(r)
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) Checkout(target string) error {
|
2023-02-26 07:22:47 +01:00
|
|
|
return r.Client.Checkout(r, target)
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) CanBePulled() bool {
|
2023-02-26 07:22:47 +01:00
|
|
|
return r.Client.CanBePulled(r)
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) GetLastCommitMessage() (msg string, err error) {
|
2023-02-26 07:22:47 +01:00
|
|
|
return r.Client.GetLastCommitMessage(r)
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) GetLastCommitHash() (hash string, err error) {
|
2023-02-26 07:22:47 +01:00
|
|
|
return r.Client.GetLastCommitHash(r)
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
2022-01-30 18:43:15 +01:00
|
|
|
|
|
|
|
func (r GitRepository) GetLastRemoteCommitHash() (hash string, err error) {
|
2023-02-26 07:22:47 +01:00
|
|
|
return r.Client.GetLastRemoteCommitHash(r)
|
2022-01-30 18:43:15 +01:00
|
|
|
}
|