Browse Source

Use the Git user specified in the config file

Brendan Abolivier 6 years ago
parent
commit
b58b51b20b
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
3 changed files with 19 additions and 12 deletions
  1. 10
    9
      src/git/git.go
  2. 2
    2
      src/puller/puller.go
  3. 7
    1
      src/pusher/webhook.go

+ 10
- 9
src/git/git.go View File

5
 	"os"
5
 	"os"
6
 	"strings"
6
 	"strings"
7
 
7
 
8
-	gogit "gopkg.in/src-d/go-git.v4"
8
+	"config"
9
 
9
 
10
 	"golang.org/x/crypto/ssh"
10
 	"golang.org/x/crypto/ssh"
11
+	gogit "gopkg.in/src-d/go-git.v4"
11
 	gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
12
 	gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
12
 )
13
 )
13
 
14
 
14
-func Sync(repo string, clonePath string, privateKeyPath string) (r *gogit.Repository, err error) {
15
-	auth, err := getAuth(privateKeyPath)
15
+func Sync(cfg config.GitSettings) (r *gogit.Repository, err error) {
16
+	auth, err := getAuth(cfg.User, cfg.PrivateKeyPath)
16
 	if err != nil {
17
 	if err != nil {
17
 		return
18
 		return
18
 	}
19
 	}
19
 
20
 
20
-	exists, err := dirExists(clonePath)
21
+	exists, err := dirExists(cfg.ClonePath)
21
 	if err != nil {
22
 	if err != nil {
22
 		return
23
 		return
23
 	}
24
 	}
24
 
25
 
25
 	if exists {
26
 	if exists {
26
-		r, err = pull(clonePath, auth)
27
+		r, err = pull(cfg.ClonePath, auth)
27
 	} else {
28
 	} else {
28
-		r, err = clone(repo, clonePath, auth)
29
+		r, err = clone(cfg.URL, cfg.ClonePath, auth)
29
 	}
30
 	}
30
 
31
 
31
 	return
32
 	return
32
 }
33
 }
33
 
34
 
34
-func getAuth(privateKeyPath string) (*gitssh.PublicKeys, error) {
35
+func getAuth(user string, privateKeyPath string) (*gitssh.PublicKeys, error) {
35
 	privateKey, err := ioutil.ReadFile(privateKeyPath)
36
 	privateKey, err := ioutil.ReadFile(privateKeyPath)
36
 	if err != nil {
37
 	if err != nil {
37
 		return nil, err
38
 		return nil, err
91
 	return true, err
92
 	return true, err
92
 }
93
 }
93
 
94
 
94
-func Push(r *gogit.Repository, keyPath string) error {
95
-	auth, err := getAuth(keyPath)
95
+func Push(r *gogit.Repository, cfg config.GitSettings) error {
96
+	auth, err := getAuth(cfg.User, cfg.PrivateKeyPath)
96
 	if err != nil {
97
 	if err != nil {
97
 		return err
98
 		return err
98
 	}
99
 	}

+ 2
- 2
src/puller/puller.go View File

24
 // already versionned in the repo
24
 // already versionned in the repo
25
 func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) error {
25
 func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) error {
26
 	// Clone or pull the repo
26
 	// Clone or pull the repo
27
-	repo, err := git.Sync(cfg.Git.URL, cfg.Git.ClonePath, cfg.Git.PrivateKeyPath)
27
+	repo, err := git.Sync(cfg.Git)
28
 	if err != nil {
28
 	if err != nil {
29
 		return err
29
 		return err
30
 	}
30
 	}
94
 
94
 
95
 	// Push the changes (we don't do it in the if clause above in case there are
95
 	// Push the changes (we don't do it in the if clause above in case there are
96
 	// pending commits in the local repo that haven't been pushed yet).
96
 	// pending commits in the local repo that haven't been pushed yet).
97
-	if err = git.Push(repo, cfg.Git.PrivateKeyPath); err != nil {
97
+	if err = git.Push(repo, cfg.Git); err != nil {
98
 		return err
98
 		return err
99
 	}
99
 	}
100
 
100
 

+ 7
- 1
src/pusher/webhook.go View File

5
 	"strings"
5
 	"strings"
6
 
6
 
7
 	"config"
7
 	"config"
8
+	"git"
8
 	puller "puller"
9
 	puller "puller"
9
 
10
 
10
 	"gopkg.in/go-playground/webhooks.v3"
11
 	"gopkg.in/go-playground/webhooks.v3"
25
 }
26
 }
26
 
27
 
27
 func HandlePush(payload interface{}, header webhooks.Header) {
28
 func HandlePush(payload interface{}, header webhooks.Header) {
29
+	var err error
30
+
28
 	pl := payload.(gitlab.PushEventPayload)
31
 	pl := payload.(gitlab.PushEventPayload)
29
 
32
 
30
-	var err error
33
+	if _, err = git.Sync(cfg.Git); err != nil {
34
+		panic(err)
35
+	}
36
+
31
 	for _, commit := range pl.Commits {
37
 	for _, commit := range pl.Commits {
32
 		// We don't want to process commits made by the puller
38
 		// We don't want to process commits made by the puller
33
 		if commit.Author.Email == cfg.Git.CommitsAuthor.Email {
39
 		if commit.Author.Email == cfg.Git.CommitsAuthor.Email {