Browse Source

Add commits author config to config file

Brendan Abolivier 6 years ago
parent
commit
fcc253cb7b
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
5 changed files with 26 additions and 12 deletions
  1. 6
    0
      config.example.yaml
  2. 12
    4
      src/config/config.go
  3. 1
    3
      src/puller/puller.go
  4. 6
    4
      src/puller/versions.go
  5. 1
    1
      src/pusher/webhook.go

+ 6
- 0
config.example.yaml View File

22
     # directory doesn't exist, it will be created and the repository will be
22
     # directory doesn't exist, it will be created and the repository will be
23
     # cloned into it.
23
     # cloned into it.
24
     clone_path: /tmp/grafana-dashboards
24
     clone_path: /tmp/grafana-dashboards
25
+    # Author of the commit created in the puller.
26
+    commits_author:
27
+        # Author's name.
28
+        name: Grafana Dashboard Manager
29
+        # Author's email.
30
+        email: grafana-dashboard-manager@company.tld
25
 
31
 
26
 # Settings to configure the Git webhook. Currently only GitLab webhooks are
32
 # Settings to configure the Git webhook. Currently only GitLab webhooks are
27
 # supported.
33
 # supported.

+ 12
- 4
src/config/config.go View File

22
 
22
 
23
 // GitSettings contains the data required to interact with the Git repository.
23
 // GitSettings contains the data required to interact with the Git repository.
24
 type GitSettings struct {
24
 type GitSettings struct {
25
-	URL            string `yaml:"url"`
26
-	User           string `yaml:"user"`
27
-	PrivateKeyPath string `yaml:"private_key"`
28
-	ClonePath      string `yaml:"clone_path"`
25
+	URL            string              `yaml:"url"`
26
+	User           string              `yaml:"user"`
27
+	PrivateKeyPath string              `yaml:"private_key"`
28
+	ClonePath      string              `yaml:"clone_path"`
29
+	CommitsAuthor  CommitsAuthorConfig `yaml:"commits_author"`
30
+}
31
+
32
+// CommitsAuthorConfig contains the configuration (name + email address) to use
33
+// when commiting to Git.
34
+type CommitsAuthorConfig struct {
35
+	Name  string `yaml:"name"`
36
+	Email string `yaml:"email"`
29
 }
37
 }
30
 
38
 
31
 // WebhookSettings contains the data required to setup the GitLab webhook.
39
 // WebhookSettings contains the data required to setup the GitLab webhook.

+ 1
- 3
src/puller/puller.go View File

87
 
87
 
88
 	// Check if there's uncommited changes, and if that's the case, commit them.
88
 	// Check if there's uncommited changes, and if that's the case, commit them.
89
 	if !status.IsClean() {
89
 	if !status.IsClean() {
90
-		if err = commitNewVersions(
91
-			dbVersions, dv, w, cfg.Git.ClonePath,
92
-		); err != nil {
90
+		if err = commitNewVersions(dbVersions, dv, w, cfg); err != nil {
93
 			return err
91
 			return err
94
 		}
92
 		}
95
 	}
93
 	}

+ 6
- 4
src/puller/versions.go View File

7
 	"os"
7
 	"os"
8
 	"time"
8
 	"time"
9
 
9
 
10
+	"config"
11
+
10
 	gogit "gopkg.in/src-d/go-git.v4"
12
 	gogit "gopkg.in/src-d/go-git.v4"
11
 	"gopkg.in/src-d/go-git.v4/plumbing/object"
13
 	"gopkg.in/src-d/go-git.v4/plumbing/object"
12
 )
14
 )
71
 // file, adding it to the index or creating the commit.
73
 // file, adding it to the index or creating the commit.
72
 func commitNewVersions(
74
 func commitNewVersions(
73
 	versions map[string]int, dv map[string]diffVersion, worktree *gogit.Worktree,
75
 	versions map[string]int, dv map[string]diffVersion, worktree *gogit.Worktree,
74
-	clonePath string,
76
+	cfg *config.Config,
75
 ) (err error) {
77
 ) (err error) {
76
-	if err = writeVersions(versions, dv, clonePath); err != nil {
78
+	if err = writeVersions(versions, dv, cfg.Git.ClonePath); err != nil {
77
 		return err
79
 		return err
78
 	}
80
 	}
79
 
81
 
83
 
85
 
84
 	_, err = worktree.Commit(getCommitMessage(dv), &gogit.CommitOptions{
86
 	_, err = worktree.Commit(getCommitMessage(dv), &gogit.CommitOptions{
85
 		Author: &object.Signature{
87
 		Author: &object.Signature{
86
-			Name:  "Grafana Dashboard Manager",
87
-			Email: "grafana@cozycloud.cc",
88
+			Name:  cfg.Git.CommitsAuthor.Name,
89
+			Email: cfg.Git.CommitsAuthor.Email,
88
 			When:  time.Now(),
90
 			When:  time.Now(),
89
 		},
91
 		},
90
 	})
92
 	})

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

30
 	var err error
30
 	var err error
31
 	for _, commit := range pl.Commits {
31
 	for _, commit := range pl.Commits {
32
 		// We don't want to process commits made by the puller
32
 		// We don't want to process commits made by the puller
33
-		if commit.Author.Email == "grafana@cozycloud.cc" {
33
+		if commit.Author.Email == cfg.Git.CommitsAuthor.Email {
34
 			continue
34
 			continue
35
 		}
35
 		}
36
 
36