2022-01-30 12:22:18 +01:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GitRepositoryDirType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
GitRepositoryTmpDir GitRepositoryDirType = iota
|
|
|
|
GitRepositoryRepoDir
|
|
|
|
)
|
|
|
|
|
|
|
|
type GitRepository struct {
|
|
|
|
TemplateID int
|
|
|
|
Repository db.Repository
|
|
|
|
Logger Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) makeCmd(targetDir GitRepositoryDirType, args ...string) *exec.Cmd {
|
|
|
|
cmd := exec.Command("git") //nolint: gas
|
|
|
|
|
|
|
|
cmd.Env = os.Environ()
|
2022-02-06 13:17:08 +01:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintln("GIT_TERMINAL_PROMPT=0"))
|
2022-01-30 12:22:18 +01:00
|
|
|
if r.Repository.SSHKey.Type == db.AccessKeySSH {
|
2022-02-05 18:34:34 +01:00
|
|
|
sshCmd := "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i " + r.Repository.SSHKey.GetPath()
|
|
|
|
if util.Config.SshConfigPath != "" {
|
|
|
|
sshCmd += " -F " + util.Config.SshConfigPath
|
|
|
|
}
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("GIT_SSH_COMMAND=%s", sshCmd))
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch targetDir {
|
|
|
|
case GitRepositoryTmpDir:
|
|
|
|
cmd.Dir = util.Config.TmpPath
|
|
|
|
case GitRepositoryRepoDir:
|
|
|
|
cmd.Dir = r.GetFullPath()
|
|
|
|
default:
|
|
|
|
panic("unknown Repository directory type")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Args = append(cmd.Args, args...)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) run(targetDir GitRepositoryDirType, args ...string) error {
|
2022-02-06 16:35:58 +01:00
|
|
|
err := r.Repository.SSHKey.Install(db.AccessKeyRoleGit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-10 06:29:55 +01:00
|
|
|
defer r.Repository.SSHKey.Destroy() //nolint: errcheck
|
2022-02-06 16:35:58 +01:00
|
|
|
|
2022-01-30 12:22:18 +01:00
|
|
|
cmd := r.makeCmd(targetDir, args...)
|
|
|
|
|
|
|
|
r.Logger.LogCmd(cmd)
|
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2022-01-30 18:43:15 +01:00
|
|
|
func (r GitRepository) output(targetDir GitRepositoryDirType, args ...string) (out string, err error) {
|
2022-02-07 07:18:07 +01:00
|
|
|
err = r.Repository.SSHKey.Install(db.AccessKeyRoleGit)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-10 06:29:55 +01:00
|
|
|
defer r.Repository.SSHKey.Destroy() //nolint: errcheck
|
2022-02-07 07:18:07 +01:00
|
|
|
|
2022-01-30 18:43:15 +01:00
|
|
|
bytes, err := r.makeCmd(targetDir, args...).Output()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out = strings.Trim(string(bytes), " \n")
|
|
|
|
return
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) Clone() error {
|
|
|
|
r.Logger.Log("Cloning Repository " + r.Repository.GitURL)
|
|
|
|
|
|
|
|
return r.run(GitRepositoryTmpDir,
|
|
|
|
"clone",
|
|
|
|
"--recursive",
|
|
|
|
"--branch",
|
|
|
|
r.Repository.GitBranch,
|
|
|
|
r.Repository.GetGitURL(),
|
|
|
|
r.Repository.GetDirName(r.TemplateID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) Pull() error {
|
|
|
|
r.Logger.Log("Updating Repository " + r.Repository.GitURL)
|
|
|
|
|
|
|
|
return r.run(GitRepositoryRepoDir, "pull", "origin", r.Repository.GitBranch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) Checkout(target string) error {
|
|
|
|
r.Logger.Log("Checkout repository to " + target)
|
|
|
|
|
|
|
|
return r.run(GitRepositoryRepoDir, "checkout", target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) CanBePulled() bool {
|
|
|
|
err := r.run(GitRepositoryRepoDir, "fetch")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.run(GitRepositoryRepoDir,
|
|
|
|
"merge-base", "--is-ancestor", "HEAD", "origin/"+r.Repository.GitBranch)
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) GetLastCommitMessage() (msg string, err error) {
|
|
|
|
r.Logger.Log("Get current commit message")
|
|
|
|
|
2022-01-30 18:43:15 +01:00
|
|
|
msg, err = r.output(GitRepositoryRepoDir, "show-branch", "--no-name", "HEAD")
|
2022-01-30 12:22:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(msg) > 100 {
|
|
|
|
msg = msg[0:100]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) GetLastCommitHash() (hash string, err error) {
|
|
|
|
r.Logger.Log("Get current commit hash")
|
2022-01-30 18:43:15 +01:00
|
|
|
hash, err = r.output(GitRepositoryRepoDir, "rev-parse", "HEAD")
|
2022-01-30 12:22:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) ValidateRepo() error {
|
|
|
|
_, err := os.Stat(r.GetFullPath())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r GitRepository) GetFullPath() (path string) {
|
|
|
|
path = r.Repository.GetFullPath(r.TemplateID)
|
|
|
|
return
|
|
|
|
}
|
2022-01-30 18:43:15 +01:00
|
|
|
|
|
|
|
func (r GitRepository) GetLastRemoteCommitHash() (hash string, err error) {
|
2022-02-06 12:05:40 +01:00
|
|
|
out, err := r.output(GitRepositoryTmpDir, "ls-remote", r.Repository.GetGitURL(), r.Repository.GitBranch)
|
2022-01-30 18:43:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-02-06 12:05:40 +01:00
|
|
|
|
2022-01-31 05:22:08 +01:00
|
|
|
firstSpaceIndex := strings.IndexAny(out, "\t ")
|
2022-01-30 18:43:15 +01:00
|
|
|
if firstSpaceIndex == -1 {
|
|
|
|
err = fmt.Errorf("can't retreave remote commit hash")
|
|
|
|
}
|
2022-02-06 12:05:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-30 18:43:15 +01:00
|
|
|
hash = out[0:firstSpaceIndex]
|
|
|
|
return
|
|
|
|
}
|