Tool to help you manage your Grafana dashboards using Git.

versions.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "time"
  8. "config"
  9. gogit "gopkg.in/src-d/go-git.v4"
  10. "gopkg.in/src-d/go-git.v4/plumbing/object"
  11. )
  12. // getDashboardsVersions reads the "versions.json" file at the root of the git
  13. // repository and returns its content as a map.
  14. // If the file doesn't exist, returns an empty map.
  15. // Return an error if there was an issue looking for the file (except when the
  16. // file doesn't exist), reading it or formatting its content into a map.
  17. func getDashboardsVersions(clonePath string) (versions map[string]int, err error) {
  18. versions = make(map[string]int)
  19. filename := clonePath + "/versions.json"
  20. _, err = os.Stat(filename)
  21. if os.IsNotExist(err) {
  22. return versions, nil
  23. }
  24. data, err := ioutil.ReadFile(filename)
  25. if err != nil {
  26. return
  27. }
  28. err = json.Unmarshal(data, &versions)
  29. return
  30. }
  31. // writeVersions updates or creates the "versions.json" file at the root of the
  32. // git repository. It takes as parameter a map of versions computed by
  33. // getDashboardsVersions and a map linking a dashboard slug to an instance of
  34. // diffVersion instance, and uses them both to compute an updated map of
  35. // versions that it will convert to JSON, indent and write down into the
  36. // "versions.json" file.
  37. // Returns an error if there was an issue when conerting to JSON, indenting or
  38. // writing on disk.
  39. func writeVersions(
  40. versions map[string]int, dv map[string]diffVersion, clonePath string,
  41. ) (err error) {
  42. for slug, diff := range dv {
  43. versions[slug] = diff.newVersion
  44. }
  45. rawJSON, err := json.Marshal(versions)
  46. if err != nil {
  47. return
  48. }
  49. indentedJSON, err := indent(rawJSON)
  50. if err != nil {
  51. return
  52. }
  53. filename := clonePath + "/versions.json"
  54. return rewriteFile(filename, indentedJSON)
  55. }
  56. // commitNewVersions creates a git commit from updated dashboard files (that
  57. // have previously been added to the git index) and an updated "versions.json"
  58. // file that it creates (with writeVersions) and add to the index.
  59. // Returns an error if there was an issue when creating the "versions.json"
  60. // file, adding it to the index or creating the commit.
  61. func commitNewVersions(
  62. versions map[string]int, dv map[string]diffVersion, worktree *gogit.Worktree,
  63. cfg *config.Config,
  64. ) (err error) {
  65. if err = writeVersions(versions, dv, cfg.Git.ClonePath); err != nil {
  66. return err
  67. }
  68. if _, err = worktree.Add("versions.json"); err != nil {
  69. return err
  70. }
  71. _, err = worktree.Commit(getCommitMessage(dv), &gogit.CommitOptions{
  72. Author: &object.Signature{
  73. Name: cfg.Git.CommitsAuthor.Name,
  74. Email: cfg.Git.CommitsAuthor.Email,
  75. When: time.Now(),
  76. },
  77. })
  78. return
  79. }
  80. // getCommitMessage creates a commit message that summarises the version updates
  81. // included in the commit.
  82. func getCommitMessage(dv map[string]diffVersion) string {
  83. message := "Updated dashboards\n"
  84. for slug, diff := range dv {
  85. message += fmt.Sprintf(
  86. "%s: %d => %d\n", slug, diff.oldVersion, diff.newVersion,
  87. )
  88. }
  89. return message
  90. }