issue_label.go 9.5 KB

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