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.

332 lines
8.5 KiB

  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 "github.com/go-gitea/go-sdk/gitea"
  13. "github.com/go-gitea/gitea/modules/base"
  14. )
  15. var labelColorPattern = regexp.MustCompile("#([a-fA-F0-9]{6})")
  16. // GetLabelTemplateFile loads the label template file by given name,
  17. // then parses and returns a list of name-color pairs.
  18. func GetLabelTemplateFile(name string) ([][2]string, error) {
  19. data, err := getRepoInitFile("label", name)
  20. if err != nil {
  21. return nil, fmt.Errorf("getRepoInitFile: %v", err)
  22. }
  23. lines := strings.Split(string(data), "\n")
  24. list := make([][2]string, 0, len(lines))
  25. for i := 0; i < len(lines); i++ {
  26. line := strings.TrimSpace(lines[i])
  27. if len(line) == 0 {
  28. continue
  29. }
  30. fields := strings.SplitN(line, " ", 2)
  31. if len(fields) != 2 {
  32. return nil, fmt.Errorf("line is malformed: %s", line)
  33. }
  34. if !labelColorPattern.MatchString(fields[0]) {
  35. return nil, fmt.Errorf("bad HTML color code in line: %s", line)
  36. }
  37. fields[1] = strings.TrimSpace(fields[1])
  38. list = append(list, [2]string{fields[1], fields[0]})
  39. }
  40. return list, nil
  41. }
  42. // Label represents a label of repository for issues.
  43. type Label struct {
  44. ID int64 `xorm:"pk autoincr"`
  45. RepoID int64 `xorm:"INDEX"`
  46. Name string
  47. Color string `xorm:"VARCHAR(7)"`
  48. NumIssues int
  49. NumClosedIssues int
  50. NumOpenIssues int `xorm:"-"`
  51. IsChecked bool `xorm:"-"`
  52. }
  53. func (label *Label) APIFormat() *api.Label {
  54. return &api.Label{
  55. ID: label.ID,
  56. Name: label.Name,
  57. Color: 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 (l *Label) ForegroundColor() template.CSS {
  67. if strings.HasPrefix(l.Color, "#") {
  68. if color, err := strconv.ParseUint(l.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. // NewLabels creates new label(s) for a repository.
  82. func NewLabels(labels ...*Label) error {
  83. _, err := x.Insert(labels)
  84. return err
  85. }
  86. // getLabelInRepoByID returns a label by ID in given repository.
  87. // If pass repoID as 0, then ORM will ignore limitation of repository
  88. // and can return arbitrary label with any valid ID.
  89. func getLabelInRepoByID(e Engine, repoID, labelID int64) (*Label, error) {
  90. if labelID <= 0 {
  91. return nil, ErrLabelNotExist{labelID, repoID}
  92. }
  93. l := &Label{
  94. ID: labelID,
  95. RepoID: repoID,
  96. }
  97. has, err := x.Get(l)
  98. if err != nil {
  99. return nil, err
  100. } else if !has {
  101. return nil, ErrLabelNotExist{l.ID, l.RepoID}
  102. }
  103. return l, nil
  104. }
  105. // GetLabelByID returns a label by given ID.
  106. func GetLabelByID(id int64) (*Label, error) {
  107. return getLabelInRepoByID(x, 0, id)
  108. }
  109. // GetLabelInRepoByID returns a label by ID in given repository.
  110. func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
  111. return getLabelInRepoByID(x, repoID, labelID)
  112. }
  113. // GetLabelsInRepoByIDs returns a list of labels by IDs in given repository,
  114. // it silently ignores label IDs that are not belong to the repository.
  115. func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
  116. labels := make([]*Label, 0, len(labelIDs))
  117. return labels, x.Where("repo_id = ?", repoID).In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
  118. }
  119. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  120. func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
  121. labels := make([]*Label, 0, 10)
  122. return labels, x.Where("repo_id = ?", repoID).Asc("name").Find(&labels)
  123. }
  124. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  125. issueLabels, err := getIssueLabels(e, issueID)
  126. if err != nil {
  127. return nil, fmt.Errorf("getIssueLabels: %v", err)
  128. } else if len(issueLabels) == 0 {
  129. return []*Label{}, nil
  130. }
  131. labelIDs := make([]int64, len(issueLabels))
  132. for i := range issueLabels {
  133. labelIDs[i] = issueLabels[i].LabelID
  134. }
  135. labels := make([]*Label, 0, len(labelIDs))
  136. return labels, e.Where("id > 0").In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
  137. }
  138. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  139. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  140. return getLabelsByIssueID(x, issueID)
  141. }
  142. func updateLabel(e Engine, l *Label) error {
  143. _, err := e.Id(l.ID).AllCols().Update(l)
  144. return err
  145. }
  146. // UpdateLabel updates label information.
  147. func UpdateLabel(l *Label) error {
  148. return updateLabel(x, l)
  149. }
  150. // DeleteLabel delete a label of given repository.
  151. func DeleteLabel(repoID, labelID int64) error {
  152. _, err := GetLabelInRepoByID(repoID, labelID)
  153. if err != nil {
  154. if IsErrLabelNotExist(err) {
  155. return nil
  156. }
  157. return err
  158. }
  159. sess := x.NewSession()
  160. defer sessionRelease(sess)
  161. if err = sess.Begin(); err != nil {
  162. return err
  163. }
  164. if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
  165. return err
  166. } else if _, err = sess.Where("label_id = ?", labelID).Delete(new(IssueLabel)); err != nil {
  167. return err
  168. }
  169. return sess.Commit()
  170. }
  171. // .___ .____ ___. .__
  172. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  173. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  174. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  175. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  176. // \/ \/ \/ \/ \/ \/ \/
  177. // IssueLabel represetns an issue-lable relation.
  178. type IssueLabel struct {
  179. ID int64 `xorm:"pk autoincr"`
  180. IssueID int64 `xorm:"UNIQUE(s)"`
  181. LabelID int64 `xorm:"UNIQUE(s)"`
  182. }
  183. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  184. has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
  185. return has
  186. }
  187. // HasIssueLabel returns true if issue has been labeled.
  188. func HasIssueLabel(issueID, labelID int64) bool {
  189. return hasIssueLabel(x, issueID, labelID)
  190. }
  191. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  192. if _, err = e.Insert(&IssueLabel{
  193. IssueID: issue.ID,
  194. LabelID: label.ID,
  195. }); err != nil {
  196. return err
  197. }
  198. label.NumIssues++
  199. if issue.IsClosed {
  200. label.NumClosedIssues++
  201. }
  202. return updateLabel(e, label)
  203. }
  204. // NewIssueLabel creates a new issue-label relation.
  205. func NewIssueLabel(issue *Issue, label *Label) (err error) {
  206. if HasIssueLabel(issue.ID, label.ID) {
  207. return nil
  208. }
  209. sess := x.NewSession()
  210. defer sessionRelease(sess)
  211. if err = sess.Begin(); err != nil {
  212. return err
  213. }
  214. if err = newIssueLabel(sess, issue, label); err != nil {
  215. return err
  216. }
  217. return sess.Commit()
  218. }
  219. func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label) (err error) {
  220. for i := range labels {
  221. if hasIssueLabel(e, issue.ID, labels[i].ID) {
  222. continue
  223. }
  224. if err = newIssueLabel(e, issue, labels[i]); err != nil {
  225. return fmt.Errorf("newIssueLabel: %v", err)
  226. }
  227. }
  228. return nil
  229. }
  230. // NewIssueLabels creates a list of issue-label relations.
  231. func NewIssueLabels(issue *Issue, labels []*Label) (err error) {
  232. sess := x.NewSession()
  233. defer sessionRelease(sess)
  234. if err = sess.Begin(); err != nil {
  235. return err
  236. }
  237. if err = newIssueLabels(sess, issue, labels); err != nil {
  238. return err
  239. }
  240. return sess.Commit()
  241. }
  242. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  243. issueLabels := make([]*IssueLabel, 0, 10)
  244. return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels)
  245. }
  246. // GetIssueLabels returns all issue-label relations of given issue by ID.
  247. func GetIssueLabels(issueID int64) ([]*IssueLabel, error) {
  248. return getIssueLabels(x, issueID)
  249. }
  250. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  251. if _, err = e.Delete(&IssueLabel{
  252. IssueID: issue.ID,
  253. LabelID: label.ID,
  254. }); err != nil {
  255. return err
  256. }
  257. label.NumIssues--
  258. if issue.IsClosed {
  259. label.NumClosedIssues--
  260. }
  261. return updateLabel(e, label)
  262. }
  263. // DeleteIssueLabel deletes issue-label relation.
  264. func DeleteIssueLabel(issue *Issue, label *Label) (err error) {
  265. sess := x.NewSession()
  266. defer sessionRelease(sess)
  267. if err = sess.Begin(); err != nil {
  268. return err
  269. }
  270. if err = deleteIssueLabel(sess, issue, label); err != nil {
  271. return err
  272. }
  273. return sess.Commit()
  274. }