Add support for access repos with credentials
This commit is contained in:
@@ -6,17 +6,21 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"git.ohea.xyz/cursorius/server/config"
|
||||
"git.ohea.xyz/cursorius/server/database"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/pkg/stdcopy"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||
"github.com/op/go-logging"
|
||||
|
||||
"git.ohea.xyz/cursorius/server/config"
|
||||
"git.ohea.xyz/cursorius/server/database"
|
||||
)
|
||||
|
||||
var log = logging.MustGetLogger("cursorius-server")
|
||||
@@ -47,12 +51,44 @@ func ExecutePipeline(pe PipelineExecution, db database.Database, pipelineConf co
|
||||
}
|
||||
|
||||
log.Infof("Cloning source from URL %v", pe.Pipeline.Url)
|
||||
// TODO: should I use go-git here instead of shelling out to raw git?
|
||||
cloneCmd := exec.Command("git", "clone", pe.Pipeline.Url, jobFolder)
|
||||
output, err := cloneCmd.CombinedOutput()
|
||||
|
||||
var auth transport.AuthMethod
|
||||
|
||||
if pe.Pipeline.Credential != nil {
|
||||
credential, err := db.GetCredentialById(*pe.Pipeline.Credential)
|
||||
if err != nil {
|
||||
log.Errorf("could not get credenital from db: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch credential.Type {
|
||||
case "USER_PASS":
|
||||
log.Debugf("job %v configured to use credential %v", pe.Pipeline.Name, credential.Name)
|
||||
auth = transport.AuthMethod(&http.BasicAuth{
|
||||
Username: credential.Username,
|
||||
Password: credential.Secret,
|
||||
})
|
||||
case "SSH_KEY":
|
||||
publicKeys, err := ssh.NewPublicKeys(credential.Username, []byte(credential.Secret), "")
|
||||
if err != nil {
|
||||
log.Errorf("could not parse credential %v", credential.Name)
|
||||
return
|
||||
}
|
||||
auth = transport.AuthMethod(publicKeys)
|
||||
default:
|
||||
log.Errorf("unsupported credential type %v", credential.Type)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
auth = nil
|
||||
}
|
||||
|
||||
_, err = git.PlainClone(jobFolder, false, &git.CloneOptions{
|
||||
URL: pe.Pipeline.Url,
|
||||
Auth: auth,
|
||||
})
|
||||
if err != nil {
|
||||
log.Debugf("%s", output)
|
||||
log.Errorf("could not clone source: %w", err)
|
||||
log.Errorf("could not clone repo: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user