issue_label.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 "github.com/gogits/go-gogs-client"
  13. "github.com/gogits/gogs/pkg/base"
  14. )
  15. var labelColorPattern = regexp.MustCompile("#([a-fA-F0-9]{6})")
  16. // GetLabelTemplateFile loads the label template file by given name,
  17. // then parses and returns a list of name-color pairs.
  18. func GetLabelTemplateFile(name string) ([][2]string, error) {
  19. data, err := getRepoInitFile("label", name)
  20. if err != nil {
  21. return nil, fmt.Errorf("getRepoInitFile: %v", err)
  22. }
  23. lines := strings.Split(string(data), "\n")
  24. list := make([][2]string, 0, len(lines))
  25. for i := 0; i < len(lines); i++ {
  26. line := strings.TrimSpace(lines[i])
  27. if len(line) == 0 {
  28. continue
  29. }
  30. fields := strings.SplitN(line, " ", 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. fields[1] = strings.TrimSpace(fields[1])
  38. list = append(list, [2]string{fields[1], fields[0]})
  39. }
  40. return list, nil
  41. }
  42. // Label represents a label of repository for issues.
  43. type Label struct {
  44. ID int64 `xorm:"pk autoincr"`
  45. RepoID int64 `xorm:"INDEX"`
  46. Name string
  47. Color string `xorm:"VARCHAR(7)"`
  48. NumIssues int
  49. NumClosedIssues int
  50. NumOpenIssues int `xorm:"-"`
  51. IsChecked bool `xorm:"-"`
  52. }
  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 (l *Label) ForegroundColor() template.CSS {
  67. if strings.HasPrefix(l.Color, "#") {
  68. if color, err := strconv.ParseUint(l.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. // getLabelOfRepoByID 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 getLabelOfRepoByID(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 getLabelOfRepoByID(x, 0, id)
  108. }
  109. // GetLabelOfRepoByID returns a label by ID in given repository.
  110. func GetLabelOfRepoByID(repoID, labelID int64) (*Label, error) {
  111. return getLabelOfRepoByID(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.Where("repo_id = ?", repoID).In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
  118. }
  119. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  120. func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
  121. labels := make([]*Label, 0, 10)
  122. return labels, x.Where("repo_id = ?", repoID).Asc("name").Find(&labels)
  123. }
  124. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  125. issueLabels, err := getIssueLabels(e, issueID)
  126. if err != nil {
  127. return nil, fmt.Errorf("getIssueLabels: %v", err)
  128. } else if len(issueLabels) == 0 {
  129. return []*Label{}, nil
  130. }
  131. labelIDs := make([]int64, len(issueLabels))
  132. for i := range issueLabels {
  133. labelIDs[i] = issueLabels[i].LabelID
  134. }
  135. labels := make([]*Label, 0, len(labelIDs))
  136. return labels, e.Where("id > 0").In("id", base.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
  137. }
  138. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  139. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  140. return getLabelsByIssueID(x, issueID)
  141. }
  142. func updateLabel(e Engine, l *Label) error {
  143. _, err := e.Id(l.ID).AllCols().Update(l)
  144. return err
  145. }
  146. // UpdateLabel updates label information.
  147. func UpdateLabel(l *Label) error {
  148. return updateLabel(x, l)
  149. }
  150. // DeleteLabel delete a label of given repository.
  151. func DeleteLabel(repoID, labelID int64) error {
  152. _, err := GetLabelOfRepoByID(repoID, labelID)
  153. if err != nil {
  154. if IsErrLabelNotExist(err) {
  155. return nil
  156. }
  157. return err
  158. }
  159. sess := x.NewSession()
  160. defer sessionRelease(sess)
  161. if err = sess.Begin(); err != nil {
  162. return err
  163. }
  164. if _, err = sess.Id(labelID).Delete(new(Label)); err != nil {
  165. return err
  166. } else if _, err = sess.Where("label_id = ?", labelID).Delete(new(IssueLabel)); err != nil {
  167. return err
  168. }
  169. return sess.Commit()
  170. }
  171. // .___ .____ ___. .__
  172. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  173. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  174. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  175. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  176. // \/ \/ \/ \/ \/ \/ \/
  177. // IssueLabel represetns an issue-lable relation.
  178. type IssueLabel struct {
  179. ID int64 `xorm:"pk autoincr"`
  180. IssueID int64 `xorm:"UNIQUE(s)"`
  181. LabelID int64 `xorm:"UNIQUE(s)"`
  182. }
  183. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  184. has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
  185. return has
  186. }
  187. // HasIssueLabel returns true if issue has been labeled.
  188. func HasIssueLabel(issueID, labelID int64) bool {
  189. return hasIssueLabel(x, issueID, labelID)
  190. }
  191. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  192. if _, err = e.Insert(&IssueLabel{
  193. IssueID: issue.ID,
  194. LabelID: label.ID,
  195. }); err != nil {
  196. return err
  197. }
  198. label.NumIssues++
  199. if issue.IsClosed {
  200. label.NumClosedIssues++
  201. }
  202. if err = updateLabel(e, label); err != nil {
  203. return fmt.Errorf("updateLabel: %v", err)
  204. }
  205. issue.Labels = append(issue.Labels, label)
  206. return nil
  207. }
  208. // NewIssueLabel creates a new issue-label relation.
  209. func NewIssueLabel(issue *Issue, label *Label) (err error) {
  210. if HasIssueLabel(issue.ID, label.ID) {
  211. return nil
  212. }
  213. sess := x.NewSession()
  214. defer sessionRelease(sess)
  215. if err = sess.Begin(); err != nil {
  216. return err
  217. }
  218. if err = newIssueLabel(sess, issue, label); err != nil {
  219. return err
  220. }
  221. return sess.Commit()
  222. }
  223. func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label) (err error) {
  224. for i := range labels {
  225. if hasIssueLabel(e, issue.ID, labels[i].ID) {
  226. continue
  227. }
  228. if err = newIssueLabel(e, issue, labels[i]); err != nil {
  229. return fmt.Errorf("newIssueLabel: %v", err)
  230. }
  231. }
  232. return nil
  233. }
  234. // NewIssueLabels creates a list of issue-label relations.
  235. func NewIssueLabels(issue *Issue, labels []*Label) (err error) {
  236. sess := x.NewSession()
  237. defer sessionRelease(sess)
  238. if err = sess.Begin(); err != nil {
  239. return err
  240. }
  241. if err = newIssueLabels(sess, issue, labels); err != nil {
  242. return err
  243. }
  244. return sess.Commit()
  245. }
  246. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  247. issueLabels := make([]*IssueLabel, 0, 10)
  248. return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels)
  249. }
  250. // GetIssueLabels returns all issue-label relations of given issue by ID.
  251. func GetIssueLabels(issueID int64) ([]*IssueLabel, error) {
  252. return getIssueLabels(x, issueID)
  253. }
  254. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  255. if _, err = e.Delete(&IssueLabel{
  256. IssueID: issue.ID,
  257. LabelID: label.ID,
  258. }); err != nil {
  259. return err
  260. }
  261. label.NumIssues--
  262. if issue.IsClosed {
  263. label.NumClosedIssues--
  264. }
  265. if err = updateLabel(e, label); err != nil {
  266. return fmt.Errorf("updateLabel: %v", err)
  267. }
  268. for i := range issue.Labels {
  269. if issue.Labels[i].ID == label.ID {
  270. issue.Labels = append(issue.Labels[:i], issue.Labels[i+1:]...)
  271. break
  272. }
  273. }
  274. return nil
  275. }
  276. // DeleteIssueLabel deletes issue-label relation.
  277. func DeleteIssueLabel(issue *Issue, label *Label) (err error) {
  278. sess := x.NewSession()
  279. defer sessionRelease(sess)
  280. if err = sess.Begin(); err != nil {
  281. return err
  282. }
  283. if err = deleteIssueLabel(sess, issue, label); err != nil {
  284. return err
  285. }
  286. return sess.Commit()
  287. }