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.

383 lines
9.5 KiB

8 years ago
  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: strings.TrimLeft(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. // getLabelInRepoByName returns a label by Name 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 getLabelInRepoByName(e Engine, repoID int64, labelName string) (*Label, error) {
  90. if len(labelName) <= 0 {
  91. return nil, ErrLabelNotExist{0, repoID}
  92. }
  93. l := &Label{
  94. Name: labelName,
  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{0, l.RepoID}
  102. }
  103. return l, nil
  104. }
  105. // getLabelInRepoByID returns a label by ID in given repository.
  106. // If pass repoID as 0, then ORM will ignore limitation of repository
  107. // and can return arbitrary label with any valid ID.
  108. func getLabelInRepoByID(e Engine, repoID, labelID int64) (*Label, error) {
  109. if labelID <= 0 {
  110. return nil, ErrLabelNotExist{labelID, repoID}
  111. }
  112. l := &Label{
  113. ID: labelID,
  114. RepoID: repoID,
  115. }
  116. has, err := x.Get(l)
  117. if err != nil {
  118. return nil, err
  119. } else if !has {
  120. return nil, ErrLabelNotExist{l.ID, l.RepoID}
  121. }
  122. return l, nil
  123. }
  124. // GetLabelByID returns a label by given ID.
  125. func GetLabelByID(id int64) (*Label, error) {
  126. return getLabelInRepoByID(x, 0, id)
  127. }
  128. // GetLabelInRepoByName returns a label by name in given repository.
  129. func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
  130. return getLabelInRepoByName(x, repoID, labelName)
  131. }
  132. // GetLabelInRepoByID returns a label by ID in given repository.
  133. func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
  134. return getLabelInRepoByID(x, repoID, labelID)
  135. }
  136. // GetLabelsInRepoByIDs returns a list of labels by IDs in given repository,
  137. // it silently ignores label IDs that are not belong to the repository.
  138. func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
  139. labels := make([]*Label, 0, len(labelIDs))
  140. return labels, x.
  141. Where("repo_id = ?", repoID).
  142. In("id", labelIDs).
  143. Asc("name").
  144. Find(&labels)
  145. }
  146. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  147. func GetLabelsByRepoID(repoID int64, sortType string) ([]*Label, error) {
  148. labels := make([]*Label, 0, 10)
  149. sess := x.Where("repo_id = ?", repoID)
  150. switch sortType {
  151. case "reversealphabetically":
  152. sess.Desc("name")
  153. case "leastissues":
  154. sess.Asc("num_issues")
  155. case "mostissues":
  156. sess.Desc("num_issues")
  157. default:
  158. sess.Asc("name")
  159. }
  160. return labels, sess.Find(&labels)
  161. }
  162. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  163. issueLabels, err := getIssueLabels(e, issueID)
  164. if err != nil {
  165. return nil, fmt.Errorf("getIssueLabels: %v", err)
  166. } else if len(issueLabels) == 0 {
  167. return []*Label{}, nil
  168. }
  169. labelIDs := make([]int64, len(issueLabels))
  170. for i := range issueLabels {
  171. labelIDs[i] = issueLabels[i].LabelID
  172. }
  173. labels := make([]*Label, 0, len(labelIDs))
  174. return labels, e.
  175. Where("id > 0").
  176. In("id", labelIDs).
  177. Asc("name").
  178. Find(&labels)
  179. }
  180. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  181. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  182. return getLabelsByIssueID(x, issueID)
  183. }
  184. func updateLabel(e Engine, l *Label) error {
  185. _, err := e.Id(l.ID).AllCols().Update(l)
  186. return err
  187. }
  188. // UpdateLabel updates label information.
  189. func UpdateLabel(l *Label) error {
  190. return updateLabel(x, l)
  191. }
  192. // DeleteLabel delete a label of given repository.
  193. func DeleteLabel(repoID, labelID int64) error {
  194. _, err := GetLabelInRepoByID(repoID, labelID)
  195. if err != nil {
  196. if IsErrLabelNotExist(err) {
  197. return nil
  198. }
  199. return err
  200. }
  201. sess := x.NewSession()
  202. defer sessionRelease(sess)
  203. if err = sess.Begin(); err != nil {
  204. return err
  205. }
  206. if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
  207. return err
  208. } else if _, err = sess.
  209. Where("label_id = ?", labelID).
  210. Delete(new(IssueLabel)); err != nil {
  211. return err
  212. }
  213. return sess.Commit()
  214. }
  215. // .___ .____ ___. .__
  216. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  217. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  218. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  219. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  220. // \/ \/ \/ \/ \/ \/ \/
  221. // IssueLabel represetns an issue-lable relation.
  222. type IssueLabel struct {
  223. ID int64 `xorm:"pk autoincr"`
  224. IssueID int64 `xorm:"UNIQUE(s)"`
  225. LabelID int64 `xorm:"UNIQUE(s)"`
  226. }
  227. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  228. has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
  229. return has
  230. }
  231. // HasIssueLabel returns true if issue has been labeled.
  232. func HasIssueLabel(issueID, labelID int64) bool {
  233. return hasIssueLabel(x, issueID, labelID)
  234. }
  235. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  236. if _, err = e.Insert(&IssueLabel{
  237. IssueID: issue.ID,
  238. LabelID: label.ID,
  239. }); err != nil {
  240. return err
  241. }
  242. label.NumIssues++
  243. if issue.IsClosed {
  244. label.NumClosedIssues++
  245. }
  246. return updateLabel(e, label)
  247. }
  248. // NewIssueLabel creates a new issue-label relation.
  249. func NewIssueLabel(issue *Issue, label *Label) (err error) {
  250. if HasIssueLabel(issue.ID, label.ID) {
  251. return nil
  252. }
  253. sess := x.NewSession()
  254. defer sessionRelease(sess)
  255. if err = sess.Begin(); err != nil {
  256. return err
  257. }
  258. if err = newIssueLabel(sess, issue, label); err != nil {
  259. return err
  260. }
  261. return sess.Commit()
  262. }
  263. func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label) (err error) {
  264. for i := range labels {
  265. if hasIssueLabel(e, issue.ID, labels[i].ID) {
  266. continue
  267. }
  268. if err = newIssueLabel(e, issue, labels[i]); err != nil {
  269. return fmt.Errorf("newIssueLabel: %v", err)
  270. }
  271. }
  272. return nil
  273. }
  274. // NewIssueLabels creates a list of issue-label relations.
  275. func NewIssueLabels(issue *Issue, labels []*Label) (err error) {
  276. sess := x.NewSession()
  277. defer sessionRelease(sess)
  278. if err = sess.Begin(); err != nil {
  279. return err
  280. }
  281. if err = newIssueLabels(sess, issue, labels); err != nil {
  282. return err
  283. }
  284. return sess.Commit()
  285. }
  286. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  287. issueLabels := make([]*IssueLabel, 0, 10)
  288. return issueLabels, e.
  289. Where("issue_id=?", issueID).
  290. Asc("label_id").
  291. Find(&issueLabels)
  292. }
  293. // GetIssueLabels returns all issue-label relations of given issue by ID.
  294. func GetIssueLabels(issueID int64) ([]*IssueLabel, error) {
  295. return getIssueLabels(x, issueID)
  296. }
  297. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  298. if _, err = e.Delete(&IssueLabel{
  299. IssueID: issue.ID,
  300. LabelID: label.ID,
  301. }); err != nil {
  302. return err
  303. }
  304. label.NumIssues--
  305. if issue.IsClosed {
  306. label.NumClosedIssues--
  307. }
  308. return updateLabel(e, label)
  309. }
  310. // DeleteIssueLabel deletes issue-label relation.
  311. func DeleteIssueLabel(issue *Issue, label *Label) (err error) {
  312. sess := x.NewSession()
  313. defer sessionRelease(sess)
  314. if err = sess.Begin(); err != nil {
  315. return err
  316. }
  317. if err = deleteIssueLabel(sess, issue, label); err != nil {
  318. return err
  319. }
  320. return sess.Commit()
  321. }