Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 4488c76

Browse files
created git api struct
1 parent bf3e0ba commit 4488c76

File tree

3 files changed

+83
-25
lines changed

3 files changed

+83
-25
lines changed

pkg/plugin/git_api.go

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,84 +14,131 @@ import (
1414
"time"
1515
)
1616

17-
// Workflow to fetch remote branch, add Json Content as File to memory, commit and push to remote repo
18-
func callGit(gitUrl string, privateKeyFilePath string, fileName string, fileContent string) {
17+
// GitApi access to git api
18+
type GitApi struct {
19+
gitUrl string
20+
authenticator *ssh.PublicKeys
21+
inMemoryStore memory.Storage
22+
inMemoryFileSystem billy.Filesystem
23+
}
24+
25+
// NewGitApi creates a new NewGitApi instance
26+
func NewGitApi(gitUrl string, privateKeyFilePath string) GitApi {
27+
authenticator, _ := createAuthenticator(privateKeyFilePath)
28+
inMemoryStore, inMemoryFileSystem := createInMemory()
29+
gitApi := GitApi {gitUrl,authenticator, *inMemoryStore, inMemoryFileSystem}
30+
31+
return gitApi
32+
}
33+
34+
// helper function to create the git authenticator
35+
func createAuthenticator(privateKeyFilePath string) (*ssh.PublicKeys, error) {
1936
// git authentication with ssh
2037
_, err := os.Stat(privateKeyFilePath)
2138
if err != nil {
22-
log.DefaultLogger.Warn("read file %s failed", privateKeyFilePath, err.Error())
39+
log.DefaultLogger.Error("read file %s failed", privateKeyFilePath, err.Error())
40+
return nil, err
2341
}
2442
authenticator, err:= ssh.NewPublicKeysFromFile("git", privateKeyFilePath, "")
2543
if err != nil {
26-
log.DefaultLogger.Warn("generate public keys failed", "error", err.Error())
44+
log.DefaultLogger.Error("generate public keys failed", "error", err.Error())
45+
return nil, err
2746
}
47+
2848
// TODO delete and set known hosts?
2949
authenticator.HostKeyCallback = ssh2.InsecureIgnoreHostKey()
3050

31-
// TODO develop small functions:
51+
return authenticator, err
52+
}
53+
54+
// helper function to create the in memory storage and filesystem
55+
func createInMemory() (*memory.Storage, billy.Filesystem){
3256
// prepare in memory
33-
var store *memory.Storage
34-
store = memory.NewStorage()
57+
store := memory.NewStorage()
3558
var fs billy.Filesystem
3659
fs = memfs.New()
3760

61+
return store, fs
62+
}
63+
64+
// CloneRepo clones the gitApi.gitUrls repository
65+
func (gitApi GitApi) CloneRepo() (*git.Repository, error){
3866
// git clone
39-
r, err := git.Clone(store, fs, &git.CloneOptions{
40-
URL: gitUrl,
41-
Auth: authenticator,
67+
r, err := git.Clone(&gitApi.inMemoryStore, gitApi.inMemoryFileSystem, &git.CloneOptions{
68+
URL: gitApi.gitUrl,
69+
Auth: gitApi.authenticator,
4270
})
4371
if err != nil {
4472
log.DefaultLogger.Error("clone error" , "error", err)
73+
return nil, err
4574
} else {
4675
log.DefaultLogger.Info("repo cloned")
4776
}
4877

78+
return r, err
79+
}
80+
81+
// FetchRepo fetches the given repository
82+
func (gitApi GitApi) FetchRepo(repository git.Repository) {
4983
// fetch repo
5084
log.DefaultLogger.Info("fetching repo")
51-
err = r.Fetch(&git.FetchOptions{
85+
err := repository.Fetch(&git.FetchOptions{
5286
RemoteName: "origin",
53-
Auth: authenticator,
87+
Auth: gitApi.authenticator,
5488
})
5589
if err != nil {
5690
log.DefaultLogger.Error("fetch error", "fetchMessage", err)
91+
return
5792
}
93+
}
5894

59-
// add file with content
60-
tempFile, err := fs.Create(fileName)
95+
// AddFileWithContent add the given filename and content to the in memory filesystem
96+
func (gitApi GitApi) AddFileWithContent(fileName string, fileContent string) {
97+
// add file with content to in memory filesystem
98+
tempFile, err := gitApi.inMemoryFileSystem.Create(fileName)
6199
if err != nil {
62-
log.DefaultLogger.Error("create file error" , "error", err)
100+
log.DefaultLogger.Error("create file error", "error", err)
101+
return
63102
} else {
64103
tempFile.Write([]byte(fileContent))
65104
tempFile.Close()
66105
}
106+
}
67107

108+
// CommitWorktree commits all changes in the filesystem
109+
func (gitApi GitApi) CommitWorktree(repository git.Repository) {
68110
// get worktree and commit
69-
w, err := r.Worktree()
111+
w, err := repository.Worktree()
70112
if err != nil {
71113
log.DefaultLogger.Error("worktree error" , "error", err)
114+
return
72115
} else {
73116
w.Add("./")
74117
wStatus, _ := w.Status()
75-
log.DefaultLogger.Info("worktree status" , "status", wStatus)
118+
log.DefaultLogger.Debug("worktree status" , "status", wStatus)
119+
76120
// TODO get tag from frontend
77121
_, err := w.Commit("Dashboards synced with tag " + "<tag>", &git.CommitOptions{
78122
Author: (*object2.Signature)(&object.Signature{
79-
Name: "grafana_backend",
80-
Email: "",
123+
Name: "grafana-dashboard-sync-plugin",
81124
When: time.Now(),
82125
}),
83126
})
84127
if err != nil {
85128
log.DefaultLogger.Error("worktree commit error" , "error", err.Error())
129+
return
86130
}
87131
}
132+
}
88133

134+
// PushRepo pushes the given repository
135+
func (gitApi GitApi) PushRepo(repository git.Repository) {
89136
// push repo
90-
err = r.Push(&git.PushOptions{
137+
err := repository.Push(&git.PushOptions{
91138
RemoteName: "origin",
92-
Auth: authenticator,
139+
Auth: gitApi.authenticator,
93140
})
94141
if err != nil {
95-
log.DefaultLogger.Error("push error" , "error", err.Error())
142+
log.DefaultLogger.Error("push error", "error", err.Error())
96143
}
97144
}

pkg/plugin/grafana_api.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"github.com/grafana-tools/sdk"
66
)
77

8+
// GrafanaApi access to grafana api
89
type GrafanaApi struct {
910
grafanaClient *sdk.Client
1011
}
1112

12-
func New(grafanaURL string, apiToken string) GrafanaApi {
13+
// NewGrafanaApi creates a new GrafanaApi instance
14+
func NewGrafanaApi(grafanaURL string, apiToken string) GrafanaApi {
1315
client, _ := sdk.NewClient(grafanaURL, apiToken, sdk.DefaultHTTPClient)
1416
grafanaApi := GrafanaApi {client}
1517
return grafanaApi

pkg/plugin/plugin.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
6868
privateKeyFilePath := uiSecureProperties["pkkPath"]
6969
dashboardTag := uiProperties["dashboardTag"]
7070

71-
grafanaApi := New(grafanaUrl, token)
71+
grafanaApi := NewGrafanaApi(grafanaUrl, token)
7272

7373
dashboards, err := grafanaApi.SearchDashboardsWithTag(dashboardTag)
7474
if err != nil {
@@ -93,7 +93,16 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
9393
log.DefaultLogger.Error("update dashboard", "error", err.Error())
9494
}
9595

96-
callGit(gitURL, privateKeyFilePath, dashboardObject.Title + ".json", string(dashboardJson))
96+
gitApi := NewGitApi(gitURL, privateKeyFilePath)
97+
repository, err := gitApi.CloneRepo()
98+
if err != nil {
99+
return nil, err
100+
}
101+
gitApi.FetchRepo(*repository)
102+
gitApi.AddFileWithContent(dashboardObject.Title + ".json", string(dashboardJson))
103+
gitApi.CommitWorktree(*repository)
104+
gitApi.PushRepo(*repository)
105+
97106
}
98107

99108
return &backend.CheckHealthResult{

0 commit comments

Comments
 (0)