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.

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