@@ -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}
0 commit comments