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,33 +5,34 @@ import (
5 5
 	"os"
6 6
 	"strings"
7 7
 
8
-	gogit "gopkg.in/src-d/go-git.v4"
8
+	"config"
9 9
 
10 10
 	"golang.org/x/crypto/ssh"
11
+	gogit "gopkg.in/src-d/go-git.v4"
11 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 17
 	if err != nil {
17 18
 		return
18 19
 	}
19 20
 
20
-	exists, err := dirExists(clonePath)
21
+	exists, err := dirExists(cfg.ClonePath)
21 22
 	if err != nil {
22 23
 		return
23 24
 	}
24 25
 
25 26
 	if exists {
26
-		r, err = pull(clonePath, auth)
27
+		r, err = pull(cfg.ClonePath, auth)
27 28
 	} else {
28
-		r, err = clone(repo, clonePath, auth)
29
+		r, err = clone(cfg.URL, cfg.ClonePath, auth)
29 30
 	}
30 31
 
31 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 36
 	privateKey, err := ioutil.ReadFile(privateKeyPath)
36 37
 	if err != nil {
37 38
 		return nil, err
@@ -91,8 +92,8 @@ func dirExists(path string) (bool, error) {
91 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 97
 	if err != nil {
97 98
 		return err
98 99
 	}

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

@@ -24,7 +24,7 @@ type diffVersion struct {
24 24
 // already versionned in the repo
25 25
 func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) error {
26 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 28
 	if err != nil {
29 29
 		return err
30 30
 	}
@@ -94,7 +94,7 @@ func PullGrafanaAndCommit(client *grafana.Client, cfg *config.Config) error {
94 94
 
95 95
 	// Push the changes (we don't do it in the if clause above in case there are
96 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 98
 		return err
99 99
 	}
100 100
 

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

@@ -5,6 +5,7 @@ import (
5 5
 	"strings"
6 6
 
7 7
 	"config"
8
+	"git"
8 9
 	puller "puller"
9 10
 
10 11
 	"gopkg.in/go-playground/webhooks.v3"
@@ -25,9 +26,14 @@ func SetupWebhook(cfg *config.Config) error {
25 26
 }
26 27
 
27 28
 func HandlePush(payload interface{}, header webhooks.Header) {
29
+	var err error
30
+
28 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 37
 	for _, commit := range pl.Commits {
32 38
 		// We don't want to process commits made by the puller
33 39
 		if commit.Author.Email == cfg.Git.CommitsAuthor.Email {