issue.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Data and interface definitions for bugs
  2. //
  3. // TODO strip last newline from [issues.Issue] [issues.Field] and [issues.VariadicField]
  4. package issues
  5. import (
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. )
  10. // Issue Definitions ------------------------------------------------------------
  11. // ----------------------------------------------------------------------------
  12. type Issue struct {
  13. Title string // The title of the bug in human readable format
  14. Description Field // The description of the bug
  15. Status Field // The status of the bug
  16. Tags VariadicField // A slice of VariadicFields
  17. Blockedby VariadicField // A slice of VariadicFields
  18. Path string // The path to the bug
  19. // machineTitle string // The machine parseable bug title
  20. }
  21. // Constrcutor for Issues
  22. func (i Issue) New(title string, status Field, tags VariadicField, blockedby VariadicField, path string) (issue Issue, err error) {
  23. return Issue{
  24. Title: title,
  25. Status: status,
  26. Tags: tags,
  27. Blockedby: blockedby,
  28. Path: path,
  29. }, err
  30. }
  31. // Constrcutor for Issues that loads relevant data from disk
  32. func (i Issue) NewFromPath(path string) (bug Issue, err error) {
  33. // Required Fields
  34. description := &Field{Path: "/description"}
  35. status := &Field{Path: "/status"}
  36. requiredFields := []*Field{description, status}
  37. for _, field := range requiredFields {
  38. data, err := readPath(path + field.Path)
  39. if err != nil {
  40. return Issue{}, err
  41. }
  42. field.Data = data
  43. }
  44. // Variadic Fields
  45. tags := VariadicField{Path: "/tags"}
  46. blockers := VariadicField{Path: "/blockedby"}
  47. tags, _ = VariadicField.NewFromPath(tags, path)
  48. blockers, _ = VariadicField.NewFromPath(blockers, path)
  49. // we can ignore the errors, as loadVariadicField already gracefully handles
  50. //them.
  51. // title from path
  52. title := parsePathToHuman(path)
  53. return Issue{
  54. Title: title,
  55. Description: *description,
  56. Status: *status,
  57. Tags: tags,
  58. Blockedby: blockers,
  59. Path: path,
  60. }, err
  61. }
  62. // Field Definitions ----------------------------------------------------------
  63. // ----------------------------------------------------------------------------
  64. // A struct representing data that is tied to data on disk
  65. type Field struct {
  66. Path string
  67. Data string
  68. }
  69. // Constructor for Fields
  70. func (f Field) New(data string, path string) Field { return Field{Data: data, Path: path} }
  71. // VariadicField Definitions --------------------------------------------------
  72. // ----------------------------------------------------------------------------
  73. // VariadicFields hold lists of Field objects.
  74. type VariadicField struct {
  75. Path string // The associated path on disk of Fields represented by Variadic Field
  76. Fields []Field // The underlying slice of Field objects
  77. }
  78. // Constructor for VariadicFields
  79. func (vf VariadicField) New(fields []Field, path string) VariadicField {
  80. return VariadicField{Fields: fields, Path: path}
  81. }
  82. // Constructor for VariadicFields that loads relevant data from disk
  83. func (vf VariadicField) NewFromPath(pathOnDisk string) (v VariadicField, err error) {
  84. rootPath := pathOnDisk + "/" + vf.Path
  85. files, err := os.ReadDir(rootPath)
  86. if err != nil {
  87. return vf, err
  88. }
  89. for _, file := range files {
  90. data, err := readPath(rootPath + "/" + file.Name())
  91. if err != nil {
  92. return vf, err
  93. }
  94. if file.Name()[0:1] == "." {
  95. continue
  96. }
  97. vf.Fields = append(vf.Fields, Field{Data: data, Path: file.Name()})
  98. }
  99. return vf, err
  100. }
  101. // Util Definitions -----------------------------------------------------------
  102. // ----------------------------------------------------------------------------
  103. // Parses human readable strings as path strings
  104. func parseHumanToPath(humanReadable string) string {
  105. var out string
  106. out = strings.ReplaceAll(humanReadable, "-", "\\replace/")
  107. out = strings.ReplaceAll(out, " ", "-")
  108. out = strings.ReplaceAll(out, "\\replace/", "--")
  109. return out
  110. }
  111. // Parses machine parseable paths as human readable strings
  112. func parsePathToHuman(path string) string {
  113. _, last := filepath.Split(filepath.Clean(path))
  114. last = strings.ReplaceAll(last, "--", "\\replace/")
  115. last = strings.ReplaceAll(last, "-", " ")
  116. last = strings.ReplaceAll(last, "\\replace/", "-")
  117. return last
  118. }