This repository was archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgitssh.go
77 lines (65 loc) · 2.02 KB
/
gitssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package connectors
import (
"context"
"fmt"
"os"
gitv1 "github.com/flanksource/git-operator/api/v1"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
git "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/ssh"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/go-logr/logr"
"github.com/jenkins-x/go-scm/scm"
"github.com/pkg/errors"
ssh2 "golang.org/x/crypto/ssh"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type GitSSH struct {
*scm.Client
k8sCrd client.Client
log logr.Logger
url string
auth transport.AuthMethod
}
func (g *GitSSH) OpenPullRequest(ctx context.Context, base string, head string, spec *gitv1.PullRequestTemplate) (int, error) {
return 0, fmt.Errorf("open pull request not implemented for git ssh")
}
func (g *GitSSH) ClosePullRequest(ctx context.Context, id int) error {
return fmt.Errorf("close pull request not implemented for git ssh")
}
func (g *GitSSH) Push(ctx context.Context, branch string) error {
return fmt.Errorf("push not implemented for git ssh")
}
func (g *GitSSH) Clone(ctx context.Context, branch, local string) (billy.Filesystem, *git.Worktree, error) {
// Filesystem abstraction based on memory
fs := memfs.New()
repo, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
URL: g.url,
Progress: os.Stdout,
Auth: g.auth,
})
if err != nil {
return nil, nil, err
}
work, err := repo.Worktree()
if err != nil {
return nil, nil, err
}
return fs, work, nil
}
func NewGitSSH(client client.Client, log logr.Logger, url, user string, privateKey []byte, password string) (Connector, error) {
publicKeys, err := ssh.NewPublicKeys(user, privateKey, password)
if err != nil {
return nil, errors.Wrap(err, "failed to create public keys")
}
publicKeys.HostKeyCallback = ssh2.InsecureIgnoreHostKey()
github := &GitSSH{
k8sCrd: client,
log: log.WithName("connector").WithName("GitSSH"),
url: url,
auth: publicKeys,
}
return github, nil
}