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

Commit 3f14fca

Browse files
Add folder logic for pull workflow
1 parent 2f64693 commit 3f14fca

File tree

2 files changed

+75
-22
lines changed

2 files changed

+75
-22
lines changed

pkg/plugin/git_api.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,34 +166,53 @@ func (gitApi GitApi) PullRepo(repository git.Repository) {
166166
}
167167

168168
// GetFileContent get the given content of a file from the in memory filesystem
169-
func (gitApi GitApi) GetFileContent() map[string][]byte {
170-
// read current in memory filesystem
171-
files, err := gitApi.inMemoryFileSystem.ReadDir("./")
169+
func (gitApi GitApi) GetFileContent() map[string]map[string][]byte {
170+
// read current in memory filesystem to get dirs
171+
filesOrDirs, err := gitApi.inMemoryFileSystem.ReadDir("./")
172172
if err != nil {
173173
log.DefaultLogger.Error("inMemoryFileSystem error" , "error", err)
174174
return nil
175175
}
176176

177-
fileMap := make(map[string][]byte)
177+
var dirMap []string
178178

179-
for _, file := range files {
180-
log.DefaultLogger.Debug("File name", "name", file.Name())
181-
if file.IsDir() {
182-
continue
179+
for _, fileOrDir := range filesOrDirs {
180+
if fileOrDir.IsDir() {
181+
dirName := fileOrDir.Name()
182+
dirMap = append(dirMap, dirName)
183183
}
184+
}
184185

185-
src, err := gitApi.inMemoryFileSystem.Open(file.Name())
186+
fileMap := make(map[string]map[string][]byte)
186187

188+
for _, dir := range dirMap {
189+
// read current in memory filesystem to get files
190+
files, err := gitApi.inMemoryFileSystem.ReadDir("./" + dir + "/")
187191
if err != nil {
188-
log.DefaultLogger.Error("inMemoryFileSystem error" , "error", err)
192+
log.DefaultLogger.Error("inMemoryFileSystem error", "error", err)
189193
return nil
190194
}
191-
byteFile, err := ioutil.ReadAll(src)
192-
if err != nil {
193-
log.DefaultLogger.Error("read error" , "error", err)
194-
} else {
195-
fileMap[file.Name()] = byteFile
196-
src.Close()
195+
196+
for _, file := range files {
197+
198+
if file.IsDir() {
199+
continue
200+
}
201+
202+
src, err := gitApi.inMemoryFileSystem.Open(file.Name())
203+
204+
if err != nil {
205+
log.DefaultLogger.Error("inMemoryFileSystem error", "error", err)
206+
return nil
207+
}
208+
byteFile, err := ioutil.ReadAll(src)
209+
if err != nil {
210+
log.DefaultLogger.Error("read error", "error", err)
211+
} else {
212+
fileMap[dir] = make(map[string][]byte)
213+
fileMap[dir][file.Name()] = byteFile
214+
src.Close()
215+
}
197216
}
198217
}
199218
return fileMap

pkg/plugin/grafana_api.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,48 @@ func (grafanaApi GrafanaApi) UpdateDashboardObjectByID(dashboard sdk.Board, fold
4747
return statusMessage, err
4848
}
4949

50+
// CreateFolder create a folder in Grafana
51+
func (grafanaApi GrafanaApi) CreateFolder(folderName string) int {
52+
folder := sdk.Folder{Title: folderName}
53+
folder, err := grafanaApi.grafanaClient.CreateFolder(context.Background(), folder)
54+
if err != nil {
55+
log.DefaultLogger.Error("get folders error", "error", err.Error())
56+
}
57+
return folder.ID
58+
}
59+
60+
// GetOrCreateFolderID returns the ID of a given folder or create it
61+
func (grafanaApi GrafanaApi) GetOrCreateFolderID(folderName string) int {
62+
folders, err := grafanaApi.grafanaClient.GetAllFolders(context.Background())
63+
if err != nil {
64+
log.DefaultLogger.Error("get folders error", "error", err.Error())
65+
}
66+
for _, folder := range folders {
67+
if folder.Title == folderName {
68+
return folder.ID
69+
}
70+
}
71+
generatedFolderID := grafanaApi.CreateFolder(folderName)
72+
return generatedFolderID
73+
}
74+
5075
// CreateDashboardObjects set a Dashboard with the given raw dashboard object
51-
func (grafanaApi GrafanaApi) CreateDashboardObjects(fileMap map[string][]byte) {
52-
for dashboardName, rawDashboard := range fileMap {
53-
_, err := grafanaApi.grafanaClient.SetRawDashboard(context.Background(), rawDashboard)
54-
if err != nil {
55-
log.DefaultLogger.Error("set dashboard error", "error", err.Error())
76+
func (grafanaApi GrafanaApi) CreateDashboardObjects(fileMap map[string]map[string][]byte) {
77+
for dashboardDir, dashboardFile := range fileMap {
78+
dirID := grafanaApi.GetOrCreateFolderID(dashboardDir)
79+
for dashboardName, rawDashboard := range dashboardFile {
80+
_, err := grafanaApi.grafanaClient.SetRawDashboardWithParam(context.Background(), sdk.RawBoardRequest{
81+
Dashboard: rawDashboard,
82+
Parameters: sdk.SetDashboardParams{
83+
Overwrite: false,
84+
FolderID: dirID,
85+
},
86+
})
87+
if err != nil {
88+
log.DefaultLogger.Error("set dashboard error", "error", err.Error())
89+
}
90+
log.DefaultLogger.Info("Dashboard created", "name", dashboardName)
5691
}
57-
log.DefaultLogger.Info("Dashboard created", "name", dashboardName)
5892
}
5993
}
6094

0 commit comments

Comments
 (0)