Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_label.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "html/template"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "github.com/go-xorm/xorm"
  12. api "code.gitea.io/sdk/gitea"
  13. )
  14. var labelColorPattern = regexp.MustCompile("#([a-fA-F0-9]{6})")
  15. // GetLabelTemplateFile loads the label template file by given name,
  16. // then parses and returns a list of name-color pairs.
  17. func GetLabelTemplateFile(name string) ([][2]string, error) {
  18. data, err := getRepoInitFile("label", name)
  19. if err != nil {
  20. return nil, fmt.Errorf("getRepoInitFile: %v", err)
  21. }
  22. lines := strings.Split(string(data), "\n")
  23. list := make([][2]string, 0, len(lines))
  24. for i := 0; i < len(lines); i++ {
  25. line := strings.TrimSpace(lines[i])
  26. if len(line) == 0 {
  27. continue
  28. }
  29. fields := strings.SplitN(line, " ", 2)
  30. if len(fields) != 2 {
  31. return nil, fmt.Errorf("line is malformed: %s", line)
  32. }
  33. if !labelColorPattern.MatchString(fields[0]) {
  34. return nil, fmt.Errorf("bad HTML color code in line: %s", line)
  35. }
  36. fields[1] = strings.TrimSpace(fields[1])
  37. list = append(list, [2]string{fields[1], fields[0]})
  38. }
  39. return list, nil
  40. }
  41. // Label represents a label of repository for issues.
  42. type Label struct {
  43. ID int64 `xorm:"pk autoincr"`
  44. RepoID int64 `xorm:"INDEX"`
  45. Name string
  46. Color string `xorm:"VARCHAR(7)"`
  47. NumIssues int
  48. NumClosedIssues int
  49. NumOpenIssues int `xorm:"-"`
  50. IsChecked bool `xorm:"-"`
  51. }
  52. // APIFormat converts a Label to the api.Label format
  53. func (label *Label) APIFormat() *api.Label {
  54. return &api.Label{
  55. ID: label.ID,
  56. Name: label.Name,
  57. Color: strings.TrimLeft(label.Color, "#"),
  58. }
  59. }
  60. // CalOpenIssues calculates the open issues of label.
  61. func (label *Label) CalOpenIssues() {
  62. label.NumOpenIssues = label.NumIssues - label.NumClosedIssues
  63. }
  64. // ForegroundColor calculates the text color for labels based
  65. // on their background color.
  66. func (label *Label) ForegroundColor() template.CSS {
  67. if strings.HasPrefix(label.Color, "#") {
  68. if color, err := strconv.ParseUint(label.Color[1:], 16, 64); err == nil {
  69. r := float32(0xFF & (color >> 16))
  70. g := float32(0xFF & (color >> 8))
  71. b := float32(0xFF & color)
  72. luminance := (0.2126*r + 0.7152*g + 0.0722*b) / 255
  73. if luminance < 0.66 {
  74. return template.CSS("#fff")
  75. }
  76. }
  77. }
  78. // default to black
  79. return template.CSS("#000")
  80. }
  81. func newLabel(e Engine, label *Label) error {
  82. _, err := e.Insert(label)
  83. return err
  84. }
  85. // NewLabel creates a new label for a repository
  86. func NewLabel(label *Label) error {
  87. return newLabel(x, label)
  88. }
  89. // NewLabels creates new labels for a repository.
  90. func NewLabels(labels ...*Label) error {
  91. sess := x.NewSession()
  92. defer sess.Close()
  93. if err := sess.Begin(); err != nil {
  94. return err
  95. }
  96. for _, label := range labels {
  97. if err := newLabel(sess, label); err != nil {
  98. return err
  99. }
  100. }
  101. return sess.Commit()
  102. }
  103. // getLabelInRepoByName returns a label by Name in given repository.
  104. // If pass repoID as 0, then ORM will ignore limitation of repository
  105. // and can return arbitrary label with any valid ID.
  106. func getLabelInRepoByName(e Engine, repoID int64, labelName string) (*Label, error) {
  107. if len(labelName) <= 0 {
  108. return nil, ErrLabelNotExist{0, repoID}
  109. }
  110. l := &Label{
  111. Name: labelName,
  112. RepoID: repoID,
  113. }
  114. has, err := e.Get(l)
  115. if err != nil {
  116. return nil, err
  117. } else if !has {
  118. return nil, ErrLabelNotExist{0, l.RepoID}
  119. }
  120. return l, nil
  121. }
  122. // getLabelInRepoByID returns a label by ID in given repository.
  123. // If pass repoID as 0, then ORM will ignore limitation of repository
  124. // and can return arbitrary label with any valid ID.
  125. func getLabelInRepoByID(e Engine, repoID, labelID int64) (*Label, error) {
  126. if labelID <= 0 {
  127. return nil, ErrLabelNotExist{labelID, repoID}
  128. }
  129. l := &Label{
  130. ID: labelID,
  131. RepoID: repoID,
  132. }
  133. has, err := e.Get(l)
  134. if err != nil {
  135. return nil, err
  136. } else if !has {
  137. return nil, ErrLabelNotExist{l.ID, l.RepoID}
  138. }
  139. return l, nil
  140. }
  141. // GetLabelByID returns a label by given ID.
  142. func GetLabelByID(id int64) (*Label, error) {
  143. return getLabelInRepoByID(x, 0, id)
  144. }
  145. // GetLabelInRepoByName returns a label by name in given repository.
  146. func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
  147. return getLabelInRepoByName(x, repoID, labelName)
  148. }
  149. // GetLabelInRepoByID returns a label by ID in given repository.
  150. func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
  151. return getLabelInRepoByID(x, repoID, labelID)
  152. }
  153. // GetLabelsInRepoByIDs returns a list of labels by IDs in given repository,
  154. // it silently ignores label IDs that are not belong to the repository.
  155. func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
  156. labels := make([]*Label, 0, len(labelIDs))
  157. return labels, x.
  158. Where("repo_id = ?", repoID).
  159. In("id", labelIDs).
  160. Asc("name").
  161. Find(&labels)
  162. }
  163. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  164. func GetLabelsByRepoID(repoID int64, sortType string) ([]*Label, error) {
  165. labels := make([]*Label, 0, 10)
  166. sess := x.Where("repo_id = ?", repoID)
  167. switch sortType {
  168. case "reversealphabetically":
  169. sess.Desc("name")
  170. case "leastissues":
  171. sess.Asc("num_issues")
  172. case "mostissues":
  173. sess.Desc("num_issues")
  174. default:
  175. sess.Asc("name")
  176. }
  177. return labels, sess.Find(&labels)
  178. }
  179. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  180. var labels []*Label
  181. return labels, e.Where("issue_label.issue_id = ?", issueID).
  182. Join("LEFT", "issue_label", "issue_label.label_id = label.id").
  183. Asc("label.name").
  184. Find(&labels)
  185. }
  186. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  187. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  188. return getLabelsByIssueID(x, issueID)
  189. }
  190. func updateLabel(e Engine, l *Label) error {
  191. _, err := e.ID(l.ID).AllCols().Update(l)
  192. return err
  193. }
  194. // UpdateLabel updates label information.
  195. func UpdateLabel(l *Label) error {
  196. return updateLabel(x, l)
  197. }
  198. // DeleteLabel delete a label of given repository.
  199. func DeleteLabel(repoID, labelID int64) error {
  200. _, err := GetLabelInRepoByID(repoID, labelID)
  201. if err != nil {
  202. if IsErrLabelNotExist(err) {
  203. return nil
  204. }
  205. return err
  206. }
  207. sess := x.NewSession()
  208. defer sess.Close()
  209. if err = sess.Begin(); err != nil {
  210. return err
  211. }
  212. if _, err = sess.ID(labelID).Delete(new(Label)); err != nil {
  213. return err
  214. } else if _, err = sess.
  215. Where("label_id = ?", labelID).
  216. Delete(new(IssueLabel)); err != nil {
  217. return err
  218. }
  219. // Clear label id in comment table
  220. if _, err = sess.Where("label_id = ?", labelID).Cols("label_id").Update(&Comment{}); err != nil {
  221. return err
  222. }
  223. return sess.Commit()
  224. }
  225. // .___ .____ ___. .__
  226. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  227. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  228. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  229. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  230. // \/ \/ \/ \/ \/ \/ \/
  231. // IssueLabel represents an issue-label relation.
  232. type IssueLabel struct {
  233. ID int64 `xorm:"pk autoincr"`
  234. IssueID int64 `xorm:"UNIQUE(s)"`
  235. LabelID int64 `xorm:"UNIQUE(s)"`
  236. }
  237. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  238. has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
  239. return has
  240. }
  241. // HasIssueLabel returns true if issue has been labeled.
  242. func HasIssueLabel(issueID, labelID int64) bool {
  243. return hasIssueLabel(x, issueID, labelID)
  244. }
  245. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
  246. if _, err = e.Insert(&IssueLabel{
  247. IssueID: issue.ID,
  248. LabelID: label.ID,
  249. }); err != nil {
  250. return err
  251. }
  252. if err = issue.loadRepo(e); err != nil {
  253. return
  254. }
  255. if _, err = createLabelComment(e, doer, issue.Repo, issue, label, true); err != nil {
  256. return err
  257. }
  258. label.NumIssues++
  259. if issue.IsClosed {
  260. label.NumClosedIssues++
  261. }
  262. return updateLabel(e, label)
  263. }
  264. // NewIssueLabel creates a new issue-label relation.
  265. func NewIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
  266. if HasIssueLabel(issue.ID, label.ID) {
  267. return nil
  268. }
  269. sess := x.NewSession()
  270. defer sess.Close()
  271. if err = sess.Begin(); err != nil {
  272. return err
  273. }
  274. if err = newIssueLabel(sess, issue, label, doer); err != nil {
  275. return err
  276. }
  277. return sess.Commit()
  278. }
  279. func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label, doer *User) (err error) {
  280. for i := range labels {
  281. if hasIssueLabel(e, issue.ID, labels[i].ID) {
  282. continue
  283. }
  284. if err = newIssueLabel(e, issue, labels[i], doer); err != nil {
  285. return fmt.Errorf("newIssueLabel: %v", err)
  286. }
  287. }
  288. return nil
  289. }
  290. // NewIssueLabels creates a list of issue-label relations.
  291. func NewIssueLabels(issue *Issue, labels []*Label, doer *User) (err error) {
  292. sess := x.NewSession()
  293. defer sess.Close()
  294. if err = sess.Begin(); err != nil {
  295. return err
  296. }
  297. if err = newIssueLabels(sess, issue, labels, doer); err != nil {
  298. return err
  299. }
  300. return sess.Commit()
  301. }
  302. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  303. issueLabels := make([]*IssueLabel, 0, 10)
  304. return issueLabels, e.
  305. Where("issue_id=?", issueID).
  306. Asc("label_id").
  307. Find(&issueLabels)
  308. }
  309. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
  310. if count, err := e.Delete(&IssueLabel{
  311. IssueID: issue.ID,
  312. LabelID: label.ID,
  313. }); err != nil {
  314. return err
  315. } else if count == 0 {
  316. return nil
  317. }
  318. if err = issue.loadRepo(e); err != nil {
  319. return
  320. }
  321. if _, err = createLabelComment(e, doer, issue.Repo, issue, label, false); err != nil {
  322. return err
  323. }
  324. label.NumIssues--
  325. if issue.IsClosed {
  326. label.NumClosedIssues--
  327. }
  328. return updateLabel(e, label)
  329. }
  330. // DeleteIssueLabel deletes issue-label relation.
  331. func DeleteIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
  332. sess := x.NewSession()
  333. defer sess.Close()
  334. if err = sess.Begin(); err != nil {
  335. return err
  336. }
  337. if err = deleteIssueLabel(sess, issue, label, doer); err != nil {
  338. return err
  339. }
  340. return sess.Commit()
  341. }