issue_label.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. "strconv"
  9. "strings"
  10. "github.com/go-xorm/xorm"
  11. )
  12. // Label represents a label of repository for issues.
  13. type Label struct {
  14. ID int64 `xorm:"pk autoincr"`
  15. RepoID int64 `xorm:"INDEX"`
  16. Name string
  17. Color string `xorm:"VARCHAR(7)"`
  18. NumIssues int
  19. NumClosedIssues int
  20. NumOpenIssues int `xorm:"-"`
  21. IsChecked bool `xorm:"-"`
  22. }
  23. // CalOpenIssues calculates the open issues of label.
  24. func (m *Label) CalOpenIssues() {
  25. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  26. }
  27. // ForegroundColor calculates the text color for labels based
  28. // on their background color.
  29. func (l *Label) ForegroundColor() template.CSS {
  30. if strings.HasPrefix(l.Color, "#") {
  31. if color, err := strconv.ParseUint(l.Color[1:], 16, 64); err == nil {
  32. r := float32(0xFF & (color >> 16))
  33. g := float32(0xFF & (color >> 8))
  34. b := float32(0xFF & color)
  35. luminance := (0.2126*r + 0.7152*g + 0.0722*b) / 255
  36. if luminance < 0.5 {
  37. return template.CSS("#fff")
  38. }
  39. }
  40. }
  41. // default to black
  42. return template.CSS("#000")
  43. }
  44. // NewLabel creates new label of repository.
  45. func NewLabel(l *Label) error {
  46. _, err := x.Insert(l)
  47. return err
  48. }
  49. func getLabelByID(e Engine, id int64) (*Label, error) {
  50. if id <= 0 {
  51. return nil, ErrLabelNotExist{id}
  52. }
  53. l := &Label{ID: id}
  54. has, err := x.Get(l)
  55. if err != nil {
  56. return nil, err
  57. } else if !has {
  58. return nil, ErrLabelNotExist{l.ID}
  59. }
  60. return l, nil
  61. }
  62. // GetLabelByID returns a label by given ID.
  63. func GetLabelByID(id int64) (*Label, error) {
  64. return getLabelByID(x, id)
  65. }
  66. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  67. func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
  68. labels := make([]*Label, 0, 10)
  69. return labels, x.Where("repo_id=?", repoID).Find(&labels)
  70. }
  71. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  72. issueLabels, err := getIssueLabels(e, issueID)
  73. if err != nil {
  74. return nil, fmt.Errorf("getIssueLabels: %v", err)
  75. }
  76. var label *Label
  77. labels := make([]*Label, 0, len(issueLabels))
  78. for idx := range issueLabels {
  79. label, err = getLabelByID(e, issueLabels[idx].LabelID)
  80. if err != nil && !IsErrLabelNotExist(err) {
  81. return nil, fmt.Errorf("getLabelByID: %v", err)
  82. }
  83. labels = append(labels, label)
  84. }
  85. return labels, nil
  86. }
  87. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  88. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  89. return getLabelsByIssueID(x, issueID)
  90. }
  91. func updateLabel(e Engine, l *Label) error {
  92. _, err := e.Id(l.ID).AllCols().Update(l)
  93. return err
  94. }
  95. // UpdateLabel updates label information.
  96. func UpdateLabel(l *Label) error {
  97. return updateLabel(x, l)
  98. }
  99. // DeleteLabel delete a label of given repository.
  100. func DeleteLabel(repoID, labelID int64) error {
  101. l, err := GetLabelByID(labelID)
  102. if err != nil {
  103. if IsErrLabelNotExist(err) {
  104. return nil
  105. }
  106. return err
  107. }
  108. sess := x.NewSession()
  109. defer sessionRelease(sess)
  110. if err = sess.Begin(); err != nil {
  111. return err
  112. }
  113. if _, err = x.Where("label_id=?", labelID).Delete(new(IssueLabel)); err != nil {
  114. return err
  115. } else if _, err = sess.Delete(l); err != nil {
  116. return err
  117. }
  118. return sess.Commit()
  119. }
  120. // .___ .____ ___. .__
  121. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  122. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  123. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  124. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  125. // \/ \/ \/ \/ \/ \/ \/
  126. // IssueLabel represetns an issue-lable relation.
  127. type IssueLabel struct {
  128. ID int64 `xorm:"pk autoincr"`
  129. IssueID int64 `xorm:"UNIQUE(s)"`
  130. LabelID int64 `xorm:"UNIQUE(s)"`
  131. }
  132. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  133. has, _ := e.Where("issue_id=? AND label_id=?", issueID, labelID).Get(new(IssueLabel))
  134. return has
  135. }
  136. // HasIssueLabel returns true if issue has been labeled.
  137. func HasIssueLabel(issueID, labelID int64) bool {
  138. return hasIssueLabel(x, issueID, labelID)
  139. }
  140. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  141. if _, err = e.Insert(&IssueLabel{
  142. IssueID: issue.ID,
  143. LabelID: label.ID,
  144. }); err != nil {
  145. return err
  146. }
  147. label.NumIssues++
  148. if issue.IsClosed {
  149. label.NumClosedIssues++
  150. }
  151. return updateLabel(e, label)
  152. }
  153. // NewIssueLabel creates a new issue-label relation.
  154. func NewIssueLabel(issue *Issue, label *Label) (err error) {
  155. sess := x.NewSession()
  156. defer sessionRelease(sess)
  157. if err = sess.Begin(); err != nil {
  158. return err
  159. }
  160. if err = newIssueLabel(sess, issue, label); err != nil {
  161. return err
  162. }
  163. return sess.Commit()
  164. }
  165. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  166. issueLabels := make([]*IssueLabel, 0, 10)
  167. return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels)
  168. }
  169. // GetIssueLabels returns all issue-label relations of given issue by ID.
  170. func GetIssueLabels(issueID int64) ([]*IssueLabel, error) {
  171. return getIssueLabels(x, issueID)
  172. }
  173. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  174. if _, err = e.Delete(&IssueLabel{
  175. IssueID: issue.ID,
  176. LabelID: label.ID,
  177. }); err != nil {
  178. return err
  179. }
  180. label.NumIssues--
  181. if issue.IsClosed {
  182. label.NumClosedIssues--
  183. }
  184. return updateLabel(e, label)
  185. }
  186. // DeleteIssueLabel deletes issue-label relation.
  187. func DeleteIssueLabel(issue *Issue, label *Label) (err error) {
  188. sess := x.NewSession()
  189. defer sessionRelease(sess)
  190. if err = sess.Begin(); err != nil {
  191. return err
  192. }
  193. if err = deleteIssueLabel(sess, issue, label); err != nil {
  194. return err
  195. }
  196. return sess.Commit()
  197. }