issue_label.go 7.3 KB

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