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

Commit 2f64693

Browse files
Added folder logic for push task
1 parent 54a11fa commit 2f64693

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

pkg/plugin/git_api.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (gitApi GitApi) AddFileWithContent(fileName string, fileContent string) {
110110
}
111111

112112
// CommitWorktree commits all changes in the filesystem
113-
func (gitApi GitApi) CommitWorktree(repository git.Repository) {
113+
func (gitApi GitApi) CommitWorktree(repository git.Repository, tag string) {
114114
// get worktree and commit
115115
w, err := repository.Worktree()
116116
if err != nil {
@@ -121,8 +121,7 @@ func (gitApi GitApi) CommitWorktree(repository git.Repository) {
121121
wStatus, _ := w.Status()
122122
log.DefaultLogger.Debug("worktree status" , "status", wStatus)
123123

124-
// TODO get tag from frontend
125-
_, err := w.Commit("Dashboards synced with tag " + "<tag>", &git.CommitOptions{
124+
_, err := w.Commit("Dashboards synced with tag <" + tag + ">", &git.CommitOptions{
126125
Author: (*object2.Signature)(&object.Signature{
127126
Name: "grafana-dashboard-sync-plugin",
128127
When: time.Now(),
@@ -178,7 +177,7 @@ func (gitApi GitApi) GetFileContent() map[string][]byte {
178177
fileMap := make(map[string][]byte)
179178

180179
for _, file := range files {
181-
log.DefaultLogger.Info("File name", "name", file.Name())
180+
log.DefaultLogger.Debug("File name", "name", file.Name())
182181
if file.IsDir() {
183182
continue
184183
}

pkg/plugin/grafana_api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ func (grafanaApi GrafanaApi) GetDashboardObjectByID(uid string) (sdk.Board, sdk.
3939
}
4040

4141
// UpdateDashboardObjectByID update the Dashboard with the given dashboard object
42-
func (grafanaApi GrafanaApi) UpdateDashboardObjectByID(dashboard sdk.Board) (sdk.StatusMessage, error) {
42+
func (grafanaApi GrafanaApi) UpdateDashboardObjectByID(dashboard sdk.Board, folderId int) (sdk.StatusMessage, error) {
4343
statusMessage, err := grafanaApi.grafanaClient.SetDashboard(context.Background() ,dashboard, sdk.SetDashboardParams{
4444
Overwrite: false,
45+
FolderID: folderId,
4546
})
4647
return statusMessage, err
4748
}

pkg/plugin/plugin.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,30 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
9494
}
9595
for _, dashboard := range dashboards {
9696
// get raw Json
97-
dashboardJson, _, err := grafanaApi.GetRawDashboardByID(dashboard.UID)
97+
dashboardJson, boardProperties, err := grafanaApi.GetRawDashboardByID(dashboard.UID)
9898
if err != nil {
9999
log.DefaultLogger.Error("get raw dashboard", "error", err.Error())
100100
}
101+
102+
// get folder name and id, needed for update processes and git folder structure
103+
folderName := boardProperties.FolderTitle
104+
folderId := boardProperties.FolderID
105+
101106
// get dashboard Object TODO: Verify if raw Json manipulation is faster
102107
dashboardObject, _, err := grafanaApi.GetDashboardObjectByID(dashboard.UID)
103108
if err != nil {
104109
log.DefaultLogger.Error("get dashboard", "error", err.Error())
105110
}
111+
106112
// delete Tag from dashboard
107113
dashboardWithDeletedTag := grafanaApi.DeleteTagFromDashboardObjectByID(dashboardObject, dashboardTag)
114+
108115
// update dashboard with deleted Tag
109-
_, err = grafanaApi.UpdateDashboardObjectByID(dashboardWithDeletedTag)
116+
_, err = grafanaApi.UpdateDashboardObjectByID(dashboardWithDeletedTag, folderId)
110117
if err != nil {
111118
log.DefaultLogger.Error("update dashboard", "error", err.Error())
112119
}
113-
log.DefaultLogger.Info("Dashboard preparation successfully ")
120+
log.DefaultLogger.Debug("Dashboard preparation successfully ")
114121

115122
// Todo: If dashboard found, else skip git workflow
116123
gitApi := NewGitApi(pushGitURL, privateKeyFilePath)
@@ -119,9 +126,11 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
119126
return nil, err
120127
}
121128
gitApi.FetchRepo(*repository)
122-
gitApi.AddFileWithContent(dashboardObject.Title+".json", string(dashboardJson))
123-
gitApi.CommitWorktree(*repository)
129+
gitApi.AddFileWithContent(folderName+"/"+dashboardObject.Title+".json", string(dashboardJson))
130+
gitApi.CommitWorktree(*repository, dashboardTag)
124131
gitApi.PushRepo(*repository)
132+
133+
log.DefaultLogger.Info("Dashboard pushed successfully ")
125134
}
126135
}
127136

0 commit comments

Comments
 (0)