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.

347 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. // 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: 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. // 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.
  118. Where("repo_id = ?", repoID).
  119. In("id", labelIDs).
  120. Asc("name").
  121. Find(&labels)
  122. }
  123. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  124. func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
  125. labels := make([]*Label, 0, 10)
  126. return labels, x.
  127. Where("repo_id = ?", repoID).
  128. Asc("name").
  129. Find(&labels)
  130. }
  131. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  132. issueLabels, err := getIssueLabels(e, issueID)
  133. if err != nil {
  134. return nil, fmt.Errorf("getIssueLabels: %v", err)
  135. } else if len(issueLabels) == 0 {
  136. return []*Label{}, nil
  137. }
  138. labelIDs := make([]int64, len(issueLabels))
  139. for i := range issueLabels {
  140. labelIDs[i] = issueLabels[i].LabelID
  141. }
  142. labels := make([]*Label, 0, len(labelIDs))
  143. return labels, e.
  144. Where("id > 0").
  145. In("id", labelIDs).
  146. Asc("name").
  147. Find(&labels)
  148. }
  149. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  150. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  151. return getLabelsByIssueID(x, issueID)
  152. }
  153. func updateLabel(e Engine, l *Label) error {
  154. _, err := e.Id(l.ID).AllCols().Update(l)
  155. return err
  156. }
  157. // UpdateLabel updates label information.
  158. func UpdateLabel(l *Label) error {
  159. return updateLabel(x, l)
  160. }
  161. // DeleteLabel delete a label of given repository.
  162. func DeleteLabel(repoID, labelID int64) error {
  163. _, err := GetLabelInRepoByID(repoID, labelID)
  164. if err != nil {
  165. if IsErrLabelNotExist(err) {
  166. return nil
  167. }
  168. return err
  169. }
  170. sess := x.NewSession()
  171. defer sessionRelease(sess)
  172. if err = sess.Begin(); err != nil {
  173. return err
  174. }
  175. if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
  176. return err
  177. } else if _, err = sess.
  178. Where("label_id = ?", labelID).
  179. Delete(new(IssueLabel)); err != nil {
  180. return err
  181. }
  182. return sess.Commit()
  183. }
  184. // .___ .____ ___. .__
  185. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  186. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  187. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  188. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  189. // \/ \/ \/ \/ \/ \/ \/
  190. // IssueLabel represetns an issue-lable relation.
  191. type IssueLabel struct {
  192. ID int64 `xorm:"pk autoincr"`
  193. IssueID int64 `xorm:"UNIQUE(s)"`
  194. LabelID int64 `xorm:"UNIQUE(s)"`
  195. }
  196. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  197. has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
  198. return has
  199. }
  200. // HasIssueLabel returns true if issue has been labeled.
  201. func HasIssueLabel(issueID, labelID int64) bool {
  202. return hasIssueLabel(x, issueID, labelID)
  203. }
  204. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  205. if _, err = e.Insert(&IssueLabel{
  206. IssueID: issue.ID,
  207. LabelID: label.ID,
  208. }); err != nil {
  209. return err
  210. }
  211. label.NumIssues++
  212. if issue.IsClosed {
  213. label.NumClosedIssues++
  214. }
  215. return updateLabel(e, label)
  216. }
  217. // NewIssueLabel creates a new issue-label relation.
  218. func NewIssueLabel(issue *Issue, label *Label) (err error) {
  219. if HasIssueLabel(issue.ID, label.ID) {
  220. return nil
  221. }
  222. sess := x.NewSession()
  223. defer sessionRelease(sess)
  224. if err = sess.Begin(); err != nil {
  225. return err
  226. }
  227. if err = newIssueLabel(sess, issue, label); err != nil {
  228. return err
  229. }
  230. return sess.Commit()
  231. }
  232. func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label) (err error) {
  233. for i := range labels {
  234. if hasIssueLabel(e, issue.ID, labels[i].ID) {
  235. continue
  236. }
  237. if err = newIssueLabel(e, issue, labels[i]); err != nil {
  238. return fmt.Errorf("newIssueLabel: %v", err)
  239. }
  240. }
  241. return nil
  242. }
  243. // NewIssueLabels creates a list of issue-label relations.
  244. func NewIssueLabels(issue *Issue, labels []*Label) (err error) {
  245. sess := x.NewSession()
  246. defer sessionRelease(sess)
  247. if err = sess.Begin(); err != nil {
  248. return err
  249. }
  250. if err = newIssueLabels(sess, issue, labels); err != nil {
  251. return err
  252. }
  253. return sess.Commit()
  254. }
  255. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  256. issueLabels := make([]*IssueLabel, 0, 10)
  257. return issueLabels, e.
  258. Where("issue_id=?", issueID).
  259. Asc("label_id").
  260. Find(&issueLabels)
  261. }
  262. // GetIssueLabels returns all issue-label relations of given issue by ID.
  263. func GetIssueLabels(issueID int64) ([]*IssueLabel, error) {
  264. return getIssueLabels(x, issueID)
  265. }
  266. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  267. if _, err = e.Delete(&IssueLabel{
  268. IssueID: issue.ID,
  269. LabelID: label.ID,
  270. }); err != nil {
  271. return err
  272. }
  273. label.NumIssues--
  274. if issue.IsClosed {
  275. label.NumClosedIssues--
  276. }
  277. return updateLabel(e, label)
  278. }
  279. // DeleteIssueLabel deletes issue-label relation.
  280. func DeleteIssueLabel(issue *Issue, label *Label) (err error) {
  281. sess := x.NewSession()
  282. defer sessionRelease(sess)
  283. if err = sess.Begin(); err != nil {
  284. return err
  285. }
  286. if err = deleteIssueLabel(sess, issue, label); err != nil {
  287. return err
  288. }
  289. return sess.Commit()
  290. }