You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.go 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package structs
  4. import (
  5. "fmt"
  6. "path"
  7. "slices"
  8. "strings"
  9. "time"
  10. "gopkg.in/yaml.v3"
  11. )
  12. // StateType issue state type
  13. type StateType string
  14. const (
  15. // StateOpen pr is opend
  16. StateOpen StateType = "open"
  17. // StateClosed pr is closed
  18. StateClosed StateType = "closed"
  19. // StateAll is all
  20. StateAll StateType = "all"
  21. )
  22. // PullRequestMeta PR info if an issue is a PR
  23. type PullRequestMeta struct {
  24. HasMerged bool `json:"merged"`
  25. Merged *time.Time `json:"merged_at"`
  26. IsWorkInProgress bool `json:"draft"`
  27. }
  28. // RepositoryMeta basic repository information
  29. type RepositoryMeta struct {
  30. ID int64 `json:"id"`
  31. Name string `json:"name"`
  32. Owner string `json:"owner"`
  33. FullName string `json:"full_name"`
  34. }
  35. // Issue represents an issue in a repository
  36. // swagger:model
  37. type Issue struct {
  38. ID int64 `json:"id"`
  39. URL string `json:"url"`
  40. HTMLURL string `json:"html_url"`
  41. Index int64 `json:"number"`
  42. Poster *User `json:"user"`
  43. OriginalAuthor string `json:"original_author"`
  44. OriginalAuthorID int64 `json:"original_author_id"`
  45. Title string `json:"title"`
  46. Body string `json:"body"`
  47. Ref string `json:"ref"`
  48. Attachments []*Attachment `json:"assets"`
  49. Labels []*Label `json:"labels"`
  50. Milestone *Milestone `json:"milestone"`
  51. // deprecated
  52. Assignee *User `json:"assignee"`
  53. Assignees []*User `json:"assignees"`
  54. // Whether the issue is open or closed
  55. //
  56. // type: string
  57. // enum: open,closed
  58. State StateType `json:"state"`
  59. IsLocked bool `json:"is_locked"`
  60. Comments int `json:"comments"`
  61. // swagger:strfmt date-time
  62. Created time.Time `json:"created_at"`
  63. // swagger:strfmt date-time
  64. Updated time.Time `json:"updated_at"`
  65. // swagger:strfmt date-time
  66. Closed *time.Time `json:"closed_at"`
  67. // swagger:strfmt date-time
  68. Deadline *time.Time `json:"due_date"`
  69. PullRequest *PullRequestMeta `json:"pull_request"`
  70. Repo *RepositoryMeta `json:"repository"`
  71. PinOrder int `json:"pin_order"`
  72. }
  73. // CreateIssueOption options to create one issue
  74. type CreateIssueOption struct {
  75. // required:true
  76. Title string `json:"title" binding:"Required"`
  77. Body string `json:"body"`
  78. Ref string `json:"ref"`
  79. // deprecated
  80. Assignee string `json:"assignee"`
  81. Assignees []string `json:"assignees"`
  82. // swagger:strfmt date-time
  83. Deadline *time.Time `json:"due_date"`
  84. // milestone id
  85. Milestone int64 `json:"milestone"`
  86. // list of label ids
  87. Labels []int64 `json:"labels"`
  88. Closed bool `json:"closed"`
  89. }
  90. // EditIssueOption options for editing an issue
  91. type EditIssueOption struct {
  92. Title string `json:"title"`
  93. Body *string `json:"body"`
  94. Ref *string `json:"ref"`
  95. // deprecated
  96. Assignee *string `json:"assignee"`
  97. Assignees []string `json:"assignees"`
  98. Milestone *int64 `json:"milestone"`
  99. State *string `json:"state"`
  100. // swagger:strfmt date-time
  101. Deadline *time.Time `json:"due_date"`
  102. RemoveDeadline *bool `json:"unset_due_date"`
  103. }
  104. // EditDeadlineOption options for creating a deadline
  105. type EditDeadlineOption struct {
  106. // required:true
  107. // swagger:strfmt date-time
  108. Deadline *time.Time `json:"due_date"`
  109. }
  110. // IssueDeadline represents an issue deadline
  111. // swagger:model
  112. type IssueDeadline struct {
  113. // swagger:strfmt date-time
  114. Deadline *time.Time `json:"due_date"`
  115. }
  116. // IssueFormFieldType defines issue form field type, can be "markdown", "textarea", "input", "dropdown" or "checkboxes"
  117. type IssueFormFieldType string
  118. const (
  119. IssueFormFieldTypeMarkdown IssueFormFieldType = "markdown"
  120. IssueFormFieldTypeTextarea IssueFormFieldType = "textarea"
  121. IssueFormFieldTypeInput IssueFormFieldType = "input"
  122. IssueFormFieldTypeDropdown IssueFormFieldType = "dropdown"
  123. IssueFormFieldTypeCheckboxes IssueFormFieldType = "checkboxes"
  124. )
  125. // IssueFormField represents a form field
  126. // swagger:model
  127. type IssueFormField struct {
  128. Type IssueFormFieldType `json:"type" yaml:"type"`
  129. ID string `json:"id" yaml:"id"`
  130. Attributes map[string]any `json:"attributes" yaml:"attributes"`
  131. Validations map[string]any `json:"validations" yaml:"validations"`
  132. Visible []IssueFormFieldVisible `json:"visible,omitempty"`
  133. }
  134. func (iff IssueFormField) VisibleOnForm() bool {
  135. if len(iff.Visible) == 0 {
  136. return true
  137. }
  138. return slices.Contains(iff.Visible, IssueFormFieldVisibleForm)
  139. }
  140. func (iff IssueFormField) VisibleInContent() bool {
  141. if len(iff.Visible) == 0 {
  142. // we have our markdown exception
  143. return iff.Type != IssueFormFieldTypeMarkdown
  144. }
  145. return slices.Contains(iff.Visible, IssueFormFieldVisibleContent)
  146. }
  147. // IssueFormFieldVisible defines issue form field visible
  148. // swagger:model
  149. type IssueFormFieldVisible string
  150. const (
  151. IssueFormFieldVisibleForm IssueFormFieldVisible = "form"
  152. IssueFormFieldVisibleContent IssueFormFieldVisible = "content"
  153. )
  154. // IssueTemplate represents an issue template for a repository
  155. // swagger:model
  156. type IssueTemplate struct {
  157. Name string `json:"name" yaml:"name"`
  158. Title string `json:"title" yaml:"title"`
  159. About string `json:"about" yaml:"about"` // Using "description" in a template file is compatible
  160. Labels IssueTemplateLabels `json:"labels" yaml:"labels"`
  161. Ref string `json:"ref" yaml:"ref"`
  162. Content string `json:"content" yaml:"-"`
  163. Fields []*IssueFormField `json:"body" yaml:"body"`
  164. FileName string `json:"file_name" yaml:"-"`
  165. }
  166. type IssueTemplateLabels []string
  167. func (l *IssueTemplateLabels) UnmarshalYAML(value *yaml.Node) error {
  168. var labels []string
  169. if value.IsZero() {
  170. *l = labels
  171. return nil
  172. }
  173. switch value.Kind {
  174. case yaml.ScalarNode:
  175. str := ""
  176. err := value.Decode(&str)
  177. if err != nil {
  178. return err
  179. }
  180. for _, v := range strings.Split(str, ",") {
  181. if v = strings.TrimSpace(v); v == "" {
  182. continue
  183. }
  184. labels = append(labels, v)
  185. }
  186. *l = labels
  187. return nil
  188. case yaml.SequenceNode:
  189. if err := value.Decode(&labels); err != nil {
  190. return err
  191. }
  192. *l = labels
  193. return nil
  194. }
  195. return fmt.Errorf("line %d: cannot unmarshal %s into IssueTemplateLabels", value.Line, value.ShortTag())
  196. }
  197. type IssueConfigContactLink struct {
  198. Name string `json:"name" yaml:"name"`
  199. URL string `json:"url" yaml:"url"`
  200. About string `json:"about" yaml:"about"`
  201. }
  202. type IssueConfig struct {
  203. BlankIssuesEnabled bool `json:"blank_issues_enabled" yaml:"blank_issues_enabled"`
  204. ContactLinks []IssueConfigContactLink `json:"contact_links" yaml:"contact_links"`
  205. }
  206. type IssueConfigValidation struct {
  207. Valid bool `json:"valid"`
  208. Message string `json:"message"`
  209. }
  210. // IssueTemplateType defines issue template type
  211. type IssueTemplateType string
  212. const (
  213. IssueTemplateTypeMarkdown IssueTemplateType = "md"
  214. IssueTemplateTypeYaml IssueTemplateType = "yaml"
  215. )
  216. // Type returns the type of IssueTemplate, can be "md", "yaml" or empty for known
  217. func (it IssueTemplate) Type() IssueTemplateType {
  218. if base := path.Base(it.FileName); base == "config.yaml" || base == "config.yml" {
  219. // ignore config.yaml which is a special configuration file
  220. return ""
  221. }
  222. if ext := path.Ext(it.FileName); ext == ".md" {
  223. return IssueTemplateTypeMarkdown
  224. } else if ext == ".yaml" || ext == ".yml" {
  225. return IssueTemplateTypeYaml
  226. }
  227. return ""
  228. }
  229. // IssueMeta basic issue information
  230. // swagger:model
  231. type IssueMeta struct {
  232. Index int64 `json:"index"`
  233. Owner string `json:"owner"`
  234. Name string `json:"repo"`
  235. }