gitea源码

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