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.

346 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 "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. func (label *Label) APIFormat() *api.Label {
  53. return &api.Label{
  54. ID: label.ID,
  55. Name: label.Name,
  56. Color: label.Color,
  57. }
  58. }
  59. // CalOpenIssues calculates the open issues of label.
  60. func (label *Label) CalOpenIssues() {
  61. label.NumOpenIssues = label.NumIssues - label.NumClosedIssues
  62. }
  63. // ForegroundColor calculates the text color for labels based
  64. // on their background color.
  65. func (l *Label) ForegroundColor() template.CSS {
  66. if strings.HasPrefix(l.Color, "#") {
  67. if color, err := strconv.ParseUint(l.Color[1:], 16, 64); err == nil {
  68. r := float32(0xFF & (color >> 16))
  69. g := float32(0xFF & (color >> 8))
  70. b := float32(0xFF & color)
  71. luminance := (0.2126*r + 0.7152*g + 0.0722*b) / 255
  72. if luminance < 0.66 {
  73. return template.CSS("#fff")
  74. }
  75. }
  76. }
  77. // default to black
  78. return template.CSS("#000")
  79. }
  80. // NewLabels creates new label(s) for a repository.
  81. func NewLabels(labels ...*Label) error {
  82. _, err := x.Insert(labels)
  83. return err
  84. }
  85. // getLabelInRepoByID returns a label by ID in given repository.
  86. // If pass repoID as 0, then ORM will ignore limitation of repository
  87. // and can return arbitrary label with any valid ID.
  88. func getLabelInRepoByID(e Engine, repoID, labelID int64) (*Label, error) {
  89. if labelID <= 0 {
  90. return nil, ErrLabelNotExist{labelID, repoID}
  91. }
  92. l := &Label{
  93. ID: labelID,
  94. RepoID: repoID,
  95. }
  96. has, err := x.Get(l)
  97. if err != nil {
  98. return nil, err
  99. } else if !has {
  100. return nil, ErrLabelNotExist{l.ID, l.RepoID}
  101. }
  102. return l, nil
  103. }
  104. // GetLabelByID returns a label by given ID.
  105. func GetLabelByID(id int64) (*Label, error) {
  106. return getLabelInRepoByID(x, 0, id)
  107. }
  108. // GetLabelInRepoByID returns a label by ID in given repository.
  109. func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
  110. return getLabelInRepoByID(x, repoID, labelID)
  111. }
  112. // GetLabelsInRepoByIDs returns a list of labels by IDs in given repository,
  113. // it silently ignores label IDs that are not belong to the repository.
  114. func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
  115. labels := make([]*Label, 0, len(labelIDs))
  116. return labels, x.
  117. Where("repo_id = ?", repoID).
  118. In("id", labelIDs).
  119. Asc("name").
  120. Find(&labels)
  121. }
  122. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  123. func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
  124. labels := make([]*Label, 0, 10)
  125. return labels, x.
  126. Where("repo_id = ?", repoID).
  127. Asc("name").
  128. Find(&labels)
  129. }
  130. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  131. issueLabels, err := getIssueLabels(e, issueID)
  132. if err != nil {
  133. return nil, fmt.Errorf("getIssueLabels: %v", err)
  134. } else if len(issueLabels) == 0 {
  135. return []*Label{}, nil
  136. }
  137. labelIDs := make([]int64, len(issueLabels))
  138. for i := range issueLabels {
  139. labelIDs[i] = issueLabels[i].LabelID
  140. }
  141. labels := make([]*Label, 0, len(labelIDs))
  142. return labels, e.
  143. Where("id > 0").
  144. In("id", labelIDs).
  145. Asc("name").
  146. Find(&labels)
  147. }
  148. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  149. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  150. return getLabelsByIssueID(x, issueID)
  151. }
  152. func updateLabel(e Engine, l *Label) error {
  153. _, err := e.Id(l.ID).AllCols().Update(l)
  154. return err
  155. }
  156. // UpdateLabel updates label information.
  157. func UpdateLabel(l *Label) error {
  158. return updateLabel(x, l)
  159. }
  160. // DeleteLabel delete a label of given repository.
  161. func DeleteLabel(repoID, labelID int64) error {
  162. _, err := GetLabelInRepoByID(repoID, labelID)
  163. if err != nil {
  164. if IsErrLabelNotExist(err) {
  165. return nil
  166. }
  167. return err
  168. }
  169. sess := x.NewSession()
  170. defer sessionRelease(sess)
  171. if err = sess.Begin(); err != nil {
  172. return err
  173. }
  174. if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
  175. return err
  176. } else if _, err = sess.
  177. Where("label_id = ?", labelID).
  178. Delete(new(IssueLabel)); err != nil {
  179. return err
  180. }
  181. return sess.Commit()
  182. }
  183. // .___ .____ ___. .__
  184. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  185. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  186. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  187. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  188. // \/ \/ \/ \/ \/ \/ \/
  189. // IssueLabel represetns an issue-lable relation.
  190. type IssueLabel struct {
  191. ID int64 `xorm:"pk autoincr"`
  192. IssueID int64 `xorm:"UNIQUE(s)"`
  193. LabelID int64 `xorm:"UNIQUE(s)"`
  194. }
  195. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  196. has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
  197. return has
  198. }
  199. // HasIssueLabel returns true if issue has been labeled.
  200. func HasIssueLabel(issueID, labelID int64) bool {
  201. return hasIssueLabel(x, issueID, labelID)
  202. }
  203. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  204. if _, err = e.Insert(&IssueLabel{
  205. IssueID: issue.ID,
  206. LabelID: label.ID,
  207. }); err != nil {
  208. return err
  209. }
  210. label.NumIssues++
  211. if issue.IsClosed {
  212. label.NumClosedIssues++
  213. }
  214. return updateLabel(e, label)
  215. }
  216. // NewIssueLabel creates a new issue-label relation.
  217. func NewIssueLabel(issue *Issue, label *Label) (err error) {
  218. if HasIssueLabel(issue.ID, label.ID) {
  219. return nil
  220. }
  221. sess := x.NewSession()
  222. defer sessionRelease(sess)
  223. if err = sess.Begin(); err != nil {
  224. return err
  225. }
  226. if err = newIssueLabel(sess, issue, label); err != nil {
  227. return err
  228. }
  229. return sess.Commit()
  230. }
  231. func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label) (err error) {
  232. for i := range labels {
  233. if hasIssueLabel(e, issue.ID, labels[i].ID) {
  234. continue
  235. }
  236. if err = newIssueLabel(e, issue, labels[i]); err != nil {
  237. return fmt.Errorf("newIssueLabel: %v", err)
  238. }
  239. }
  240. return nil
  241. }
  242. // NewIssueLabels creates a list of issue-label relations.
  243. func NewIssueLabels(issue *Issue, labels []*Label) (err error) {
  244. sess := x.NewSession()
  245. defer sessionRelease(sess)
  246. if err = sess.Begin(); err != nil {
  247. return err
  248. }
  249. if err = newIssueLabels(sess, issue, labels); err != nil {
  250. return err
  251. }
  252. return sess.Commit()
  253. }
  254. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  255. issueLabels := make([]*IssueLabel, 0, 10)
  256. return issueLabels, e.
  257. Where("issue_id=?", issueID).
  258. Asc("label_id").
  259. Find(&issueLabels)
  260. }
  261. // GetIssueLabels returns all issue-label relations of given issue by ID.
  262. func GetIssueLabels(issueID int64) ([]*IssueLabel, error) {
  263. return getIssueLabels(x, issueID)
  264. }
  265. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  266. if _, err = e.Delete(&IssueLabel{
  267. IssueID: issue.ID,
  268. LabelID: label.ID,
  269. }); err != nil {
  270. return err
  271. }
  272. label.NumIssues--
  273. if issue.IsClosed {
  274. label.NumClosedIssues--
  275. }
  276. return updateLabel(e, label)
  277. }
  278. // DeleteIssueLabel deletes issue-label relation.
  279. func DeleteIssueLabel(issue *Issue, label *Label) (err error) {
  280. sess := x.NewSession()
  281. defer sessionRelease(sess)
  282. if err = sess.Begin(); err != nil {
  283. return err
  284. }
  285. if err = deleteIssueLabel(sess, issue, label); err != nil {
  286. return err
  287. }
  288. return sess.Commit()
  289. }