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.

416 lines
10 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 and optionally description.
  17. func GetLabelTemplateFile(name string) ([][3]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([][3]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. parts := strings.SplitN(line, ";", 2)
  30. fields := strings.SplitN(parts[0], " ", 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. var description string
  38. if len(parts) > 1 {
  39. description = strings.TrimSpace(parts[1])
  40. }
  41. fields[1] = strings.TrimSpace(fields[1])
  42. list = append(list, [3]string{fields[1], fields[0], description})
  43. }
  44. return list, nil
  45. }
  46. // Label represents a label of repository for issues.
  47. type Label struct {
  48. ID int64 `xorm:"pk autoincr"`
  49. RepoID int64 `xorm:"INDEX"`
  50. Name string
  51. Description string
  52. Color string `xorm:"VARCHAR(7)"`
  53. NumIssues int
  54. NumClosedIssues int
  55. NumOpenIssues int `xorm:"-"`
  56. IsChecked bool `xorm:"-"`
  57. }
  58. // APIFormat converts a Label to the api.Label format
  59. func (label *Label) APIFormat() *api.Label {
  60. return &api.Label{
  61. ID: label.ID,
  62. Name: label.Name,
  63. Color: strings.TrimLeft(label.Color, "#"),
  64. }
  65. }
  66. // CalOpenIssues calculates the open issues of label.
  67. func (label *Label) CalOpenIssues() {
  68. label.NumOpenIssues = label.NumIssues - label.NumClosedIssues
  69. }
  70. // ForegroundColor calculates the text color for labels based
  71. // on their background color.
  72. func (label *Label) ForegroundColor() template.CSS {
  73. if strings.HasPrefix(label.Color, "#") {
  74. if color, err := strconv.ParseUint(label.Color[1:], 16, 64); err == nil {
  75. r := float32(0xFF & (color >> 16))
  76. g := float32(0xFF & (color >> 8))
  77. b := float32(0xFF & color)
  78. luminance := (0.2126*r + 0.7152*g + 0.0722*b) / 255
  79. if luminance < 0.66 {
  80. return template.CSS("#fff")
  81. }
  82. }
  83. }
  84. // default to black
  85. return template.CSS("#000")
  86. }
  87. func newLabel(e Engine, label *Label) error {
  88. _, err := e.Insert(label)
  89. return err
  90. }
  91. // NewLabel creates a new label for a repository
  92. func NewLabel(label *Label) error {
  93. return newLabel(x, label)
  94. }
  95. // NewLabels creates new labels for a repository.
  96. func NewLabels(labels ...*Label) error {
  97. sess := x.NewSession()
  98. defer sess.Close()
  99. if err := sess.Begin(); err != nil {
  100. return err
  101. }
  102. for _, label := range labels {
  103. if err := newLabel(sess, label); err != nil {
  104. return err
  105. }
  106. }
  107. return sess.Commit()
  108. }
  109. // getLabelInRepoByName returns a label by Name in given repository.
  110. // If pass repoID as 0, then ORM will ignore limitation of repository
  111. // and can return arbitrary label with any valid ID.
  112. func getLabelInRepoByName(e Engine, repoID int64, labelName string) (*Label, error) {
  113. if len(labelName) <= 0 {
  114. return nil, ErrLabelNotExist{0, repoID}
  115. }
  116. l := &Label{
  117. Name: labelName,
  118. RepoID: repoID,
  119. }
  120. has, err := e.Get(l)
  121. if err != nil {
  122. return nil, err
  123. } else if !has {
  124. return nil, ErrLabelNotExist{0, l.RepoID}
  125. }
  126. return l, nil
  127. }
  128. // getLabelInRepoByID returns a label by ID in given repository.
  129. // If pass repoID as 0, then ORM will ignore limitation of repository
  130. // and can return arbitrary label with any valid ID.
  131. func getLabelInRepoByID(e Engine, repoID, labelID int64) (*Label, error) {
  132. if labelID <= 0 {
  133. return nil, ErrLabelNotExist{labelID, repoID}
  134. }
  135. l := &Label{
  136. ID: labelID,
  137. RepoID: repoID,
  138. }
  139. has, err := e.Get(l)
  140. if err != nil {
  141. return nil, err
  142. } else if !has {
  143. return nil, ErrLabelNotExist{l.ID, l.RepoID}
  144. }
  145. return l, nil
  146. }
  147. // GetLabelByID returns a label by given ID.
  148. func GetLabelByID(id int64) (*Label, error) {
  149. return getLabelInRepoByID(x, 0, id)
  150. }
  151. // GetLabelInRepoByName returns a label by name in given repository.
  152. func GetLabelInRepoByName(repoID int64, labelName string) (*Label, error) {
  153. return getLabelInRepoByName(x, repoID, labelName)
  154. }
  155. // GetLabelInRepoByID returns a label by ID in given repository.
  156. func GetLabelInRepoByID(repoID, labelID int64) (*Label, error) {
  157. return getLabelInRepoByID(x, repoID, labelID)
  158. }
  159. // GetLabelsInRepoByIDs returns a list of labels by IDs in given repository,
  160. // it silently ignores label IDs that are not belong to the repository.
  161. func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
  162. labels := make([]*Label, 0, len(labelIDs))
  163. return labels, x.
  164. Where("repo_id = ?", repoID).
  165. In("id", labelIDs).
  166. Asc("name").
  167. Find(&labels)
  168. }
  169. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  170. func GetLabelsByRepoID(repoID int64, sortType string) ([]*Label, error) {
  171. labels := make([]*Label, 0, 10)
  172. sess := x.Where("repo_id = ?", repoID)
  173. switch sortType {
  174. case "reversealphabetically":
  175. sess.Desc("name")
  176. case "leastissues":
  177. sess.Asc("num_issues")
  178. case "mostissues":
  179. sess.Desc("num_issues")
  180. default:
  181. sess.Asc("name")
  182. }
  183. return labels, sess.Find(&labels)
  184. }
  185. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  186. var labels []*Label
  187. return labels, e.Where("issue_label.issue_id = ?", issueID).
  188. Join("LEFT", "issue_label", "issue_label.label_id = label.id").
  189. Asc("label.name").
  190. Find(&labels)
  191. }
  192. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  193. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  194. return getLabelsByIssueID(x, issueID)
  195. }
  196. func updateLabel(e Engine, l *Label) error {
  197. _, err := e.ID(l.ID).AllCols().Update(l)
  198. return err
  199. }
  200. // UpdateLabel updates label information.
  201. func UpdateLabel(l *Label) error {
  202. return updateLabel(x, l)
  203. }
  204. // DeleteLabel delete a label of given repository.
  205. func DeleteLabel(repoID, labelID int64) error {
  206. _, err := GetLabelInRepoByID(repoID, labelID)
  207. if err != nil {
  208. if IsErrLabelNotExist(err) {
  209. return nil
  210. }
  211. return err
  212. }
  213. sess := x.NewSession()
  214. defer sess.Close()
  215. if err = sess.Begin(); err != nil {
  216. return err
  217. }
  218. if _, err = sess.ID(labelID).Delete(new(Label)); err != nil {
  219. return err
  220. } else if _, err = sess.
  221. Where("label_id = ?", labelID).
  222. Delete(new(IssueLabel)); err != nil {
  223. return err
  224. }
  225. // Clear label id in comment table
  226. if _, err = sess.Where("label_id = ?", labelID).Cols("label_id").Update(&Comment{}); err != nil {
  227. return err
  228. }
  229. return sess.Commit()
  230. }
  231. // .___ .____ ___. .__
  232. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  233. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  234. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  235. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  236. // \/ \/ \/ \/ \/ \/ \/
  237. // IssueLabel represents an issue-label relation.
  238. type IssueLabel struct {
  239. ID int64 `xorm:"pk autoincr"`
  240. IssueID int64 `xorm:"UNIQUE(s)"`
  241. LabelID int64 `xorm:"UNIQUE(s)"`
  242. }
  243. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  244. has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
  245. return has
  246. }
  247. // HasIssueLabel returns true if issue has been labeled.
  248. func HasIssueLabel(issueID, labelID int64) bool {
  249. return hasIssueLabel(x, issueID, labelID)
  250. }
  251. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
  252. if _, err = e.Insert(&IssueLabel{
  253. IssueID: issue.ID,
  254. LabelID: label.ID,
  255. }); err != nil {
  256. return err
  257. }
  258. if err = issue.loadRepo(e); err != nil {
  259. return
  260. }
  261. if _, err = createLabelComment(e, doer, issue.Repo, issue, label, true); err != nil {
  262. return err
  263. }
  264. label.NumIssues++
  265. if issue.IsClosed {
  266. label.NumClosedIssues++
  267. }
  268. return updateLabel(e, label)
  269. }
  270. // NewIssueLabel creates a new issue-label relation.
  271. func NewIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
  272. if HasIssueLabel(issue.ID, label.ID) {
  273. return nil
  274. }
  275. sess := x.NewSession()
  276. defer sess.Close()
  277. if err = sess.Begin(); err != nil {
  278. return err
  279. }
  280. if err = newIssueLabel(sess, issue, label, doer); err != nil {
  281. return err
  282. }
  283. return sess.Commit()
  284. }
  285. func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label, doer *User) (err error) {
  286. for i := range labels {
  287. if hasIssueLabel(e, issue.ID, labels[i].ID) {
  288. continue
  289. }
  290. if err = newIssueLabel(e, issue, labels[i], doer); err != nil {
  291. return fmt.Errorf("newIssueLabel: %v", err)
  292. }
  293. }
  294. return nil
  295. }
  296. // NewIssueLabels creates a list of issue-label relations.
  297. func NewIssueLabels(issue *Issue, labels []*Label, doer *User) (err error) {
  298. sess := x.NewSession()
  299. defer sess.Close()
  300. if err = sess.Begin(); err != nil {
  301. return err
  302. }
  303. if err = newIssueLabels(sess, issue, labels, doer); err != nil {
  304. return err
  305. }
  306. return sess.Commit()
  307. }
  308. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  309. issueLabels := make([]*IssueLabel, 0, 10)
  310. return issueLabels, e.
  311. Where("issue_id=?", issueID).
  312. Asc("label_id").
  313. Find(&issueLabels)
  314. }
  315. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
  316. if count, err := e.Delete(&IssueLabel{
  317. IssueID: issue.ID,
  318. LabelID: label.ID,
  319. }); err != nil {
  320. return err
  321. } else if count == 0 {
  322. return nil
  323. }
  324. if err = issue.loadRepo(e); err != nil {
  325. return
  326. }
  327. if _, err = createLabelComment(e, doer, issue.Repo, issue, label, false); err != nil {
  328. return err
  329. }
  330. label.NumIssues--
  331. if issue.IsClosed {
  332. label.NumClosedIssues--
  333. }
  334. return updateLabel(e, label)
  335. }
  336. // DeleteIssueLabel deletes issue-label relation.
  337. func DeleteIssueLabel(issue *Issue, label *Label, doer *User) (err error) {
  338. sess := x.NewSession()
  339. defer sess.Close()
  340. if err = sess.Begin(); err != nil {
  341. return err
  342. }
  343. if err = deleteIssueLabel(sess, issue, label, doer); err != nil {
  344. return err
  345. }
  346. return sess.Commit()
  347. }