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.

388 lines
9.7 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 := e.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 := e.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. var labels []*Label
  164. return labels, e.Where("issue_label.issue_id = ?", issueID).
  165. Join("LEFT", "issue_label", "issue_label.label_id = label.id").
  166. Asc("label.name").
  167. Find(&labels)
  168. }
  169. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  170. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  171. return getLabelsByIssueID(x, issueID)
  172. }
  173. func updateLabel(e Engine, l *Label) error {
  174. _, err := e.Id(l.ID).AllCols().Update(l)
  175. return err
  176. }
  177. // UpdateLabel updates label information.
  178. func UpdateLabel(l *Label) error {
  179. return updateLabel(x, l)
  180. }
  181. // DeleteLabel delete a label of given repository.
  182. func DeleteLabel(repoID, labelID int64) error {
  183. _, err := GetLabelInRepoByID(repoID, labelID)
  184. if err != nil {
  185. if IsErrLabelNotExist(err) {
  186. return nil
  187. }
  188. return err
  189. }
  190. sess := x.NewSession()
  191. defer sessionRelease(sess)
  192. if err = sess.Begin(); err != nil {
  193. return err
  194. }
  195. if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
  196. return err
  197. } else if _, err = sess.
  198. Where("label_id = ?", labelID).
  199. Delete(new(IssueLabel)); err != nil {
  200. return err
  201. }
  202. // Clear label id in comment table
  203. if _, err = sess.Where("label_id = ?", labelID).Cols("label_id").Update(&Comment{}); err != nil {
  204. return err
  205. }
  206. return sess.Commit()
  207. }
  208. // .___ .____ ___. .__
  209. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  210. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  211. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  212. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  213. // \/ \/ \/ \/ \/ \/ \/
  214. // IssueLabel represents an issue-label relation.
  215. type IssueLabel struct {
  216. ID int64 `xorm:"pk autoincr"`
  217. IssueID int64 `xorm:"UNIQUE(s)"`
  218. LabelID int64 `xorm:"UNIQUE(s)"`
  219. }
  220. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  221. has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
  222. return has
  223. }
  224. // HasIssueLabel returns true if issue has been labeled.
  225. func HasIssueLabel(issueID, labelID int64) bool {
  226. return hasIssueLabel(x, issueID, labelID)
  227. }
  228. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
  229. if _, err = e.Insert(&IssueLabel{
  230. IssueID: issue.ID,
  231. LabelID: label.ID,
  232. }); err != nil {
  233. return err
  234. }
  235. if err = issue.loadRepo(e); err != nil {
  236. return
  237. }
  238. if _, err = createLabelComment(e, doer, issue.Repo, issue, label, true); err != nil {
  239. return err
  240. }
  241. label.NumIssues++
  242. if issue.IsClosed {
  243. label.NumClosedIssues++
  244. }
  245. return updateLabel(e, label)
  246. }
  247. // NewIssueLabel creates a new issue-label relation.
  248. func NewIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
  249. if HasIssueLabel(issue.ID, label.ID) {
  250. return nil
  251. }
  252. sess := x.NewSession()
  253. defer sessionRelease(sess)
  254. if err = sess.Begin(); err != nil {
  255. return err
  256. }
  257. if err = newIssueLabel(sess, issue, label, doer); err != nil {
  258. return err
  259. }
  260. return sess.Commit()
  261. }
  262. func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label, doer *User) (err error) {
  263. for i := range labels {
  264. if hasIssueLabel(e, issue.ID, labels[i].ID) {
  265. continue
  266. }
  267. if err = newIssueLabel(e, issue, labels[i], doer); err != nil {
  268. return fmt.Errorf("newIssueLabel: %v", err)
  269. }
  270. }
  271. return nil
  272. }
  273. // NewIssueLabels creates a list of issue-label relations.
  274. func NewIssueLabels(issue *Issue, labels []*Label, doer *User) (err error) {
  275. sess := x.NewSession()
  276. defer sessionRelease(sess)
  277. if err = sess.Begin(); err != nil {
  278. return err
  279. }
  280. if err = newIssueLabels(sess, issue, labels, doer); err != nil {
  281. return err
  282. }
  283. return sess.Commit()
  284. }
  285. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  286. issueLabels := make([]*IssueLabel, 0, 10)
  287. return issueLabels, e.
  288. Where("issue_id=?", issueID).
  289. Asc("label_id").
  290. Find(&issueLabels)
  291. }
  292. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
  293. if count, err := e.Delete(&IssueLabel{
  294. IssueID: issue.ID,
  295. LabelID: label.ID,
  296. }); err != nil {
  297. return err
  298. } else if count == 0 {
  299. return nil
  300. }
  301. if err = issue.loadRepo(e); err != nil {
  302. return
  303. }
  304. if _, err = createLabelComment(e, doer, issue.Repo, issue, label, false); err != nil {
  305. return err
  306. }
  307. label.NumIssues--
  308. if issue.IsClosed {
  309. label.NumClosedIssues--
  310. }
  311. return updateLabel(e, label)
  312. }
  313. // DeleteIssueLabel deletes issue-label relation.
  314. func DeleteIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
  315. sess := x.NewSession()
  316. defer sessionRelease(sess)
  317. if err = sess.Begin(); err != nil {
  318. return err
  319. }
  320. if err = deleteIssueLabel(sess, issue, label, doer); err != nil {
  321. return err
  322. }
  323. return sess.Commit()
  324. }