issue_label.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 db
  5. import (
  6. "fmt"
  7. "html/template"
  8. "strconv"
  9. "strings"
  10. "xorm.io/xorm"
  11. api "github.com/gogs/go-gogs-client"
  12. "gogs.io/gogs/internal/errutil"
  13. "gogs.io/gogs/internal/lazyregexp"
  14. "gogs.io/gogs/internal/tool"
  15. )
  16. var labelColorPattern = lazyregexp.New("#([a-fA-F0-9]{6})")
  17. // GetLabelTemplateFile loads the label template file by given name,
  18. // then parses and returns a list of name-color pairs.
  19. func GetLabelTemplateFile(name string) ([][2]string, error) {
  20. data, err := getRepoInitFile("label", name)
  21. if err != nil {
  22. return nil, fmt.Errorf("getRepoInitFile: %v", err)
  23. }
  24. lines := strings.Split(string(data), "\n")
  25. list := make([][2]string, 0, len(lines))
  26. for i := 0; i < len(lines); i++ {
  27. line := strings.TrimSpace(lines[i])
  28. if len(line) == 0 {
  29. continue
  30. }
  31. fields := strings.SplitN(line, " ", 2)
  32. if len(fields) != 2 {
  33. return nil, fmt.Errorf("line is malformed: %s", line)
  34. }
  35. if !labelColorPattern.MatchString(fields[0]) {
  36. return nil, fmt.Errorf("bad HTML color code in line: %s", line)
  37. }
  38. fields[1] = strings.TrimSpace(fields[1])
  39. list = append(list, [2]string{fields[1], fields[0]})
  40. }
  41. return list, nil
  42. }
  43. // Label represents a label of repository for issues.
  44. type Label struct {
  45. ID int64
  46. RepoID int64 `xorm:"INDEX"`
  47. Name string
  48. Color string `xorm:"VARCHAR(7)"`
  49. NumIssues int
  50. NumClosedIssues int
  51. NumOpenIssues int `xorm:"-" json:"-"`
  52. IsChecked bool `xorm:"-" json:"-"`
  53. }
  54. func (label *Label) APIFormat() *api.Label {
  55. return &api.Label{
  56. ID: label.ID,
  57. Name: label.Name,
  58. Color: strings.TrimLeft(label.Color, "#"),
  59. }
  60. }
  61. // CalOpenIssues calculates the open issues of label.
  62. func (label *Label) CalOpenIssues() {
  63. label.NumOpenIssues = label.NumIssues - label.NumClosedIssues
  64. }
  65. // ForegroundColor calculates the text color for labels based
  66. // on their background color.
  67. func (l *Label) ForegroundColor() template.CSS {
  68. if strings.HasPrefix(l.Color, "#") {
  69. if color, err := strconv.ParseUint(l.Color[1:], 16, 64); err == nil {
  70. r := float32(0xFF & (color >> 16))
  71. g := float32(0xFF & (color >> 8))
  72. b := float32(0xFF & color)
  73. luminance := (0.2126*r + 0.7152*g + 0.0722*b) / 255
  74. if luminance < 0.66 {
  75. return template.CSS("#fff")
  76. }
  77. }
  78. }
  79. // default to black
  80. return template.CSS("#000")
  81. }
  82. // NewLabels creates new label(s) for a repository.
  83. func NewLabels(labels ...*Label) error {
  84. _, err := x.Insert(labels)
  85. return err
  86. }
  87. var _ errutil.NotFound = (*ErrLabelNotExist)(nil)
  88. type ErrLabelNotExist struct {
  89. args map[string]interface{}
  90. }
  91. func IsErrLabelNotExist(err error) bool {
  92. _, ok := err.(ErrLabelNotExist)
  93. return ok
  94. }
  95. func (err ErrLabelNotExist) Error() string {
  96. return fmt.Sprintf("label does not exist: %v", err.args)
  97. }
  98. func (ErrLabelNotExist) NotFound() bool {
  99. return true
  100. }
  101. // getLabelOfRepoByName returns a label by Name in given repository.
  102. // If pass repoID as 0, then ORM will ignore limitation of repository
  103. // and can return arbitrary label with any valid ID.
  104. func getLabelOfRepoByName(e Engine, repoID int64, labelName string) (*Label, error) {
  105. if len(labelName) <= 0 {
  106. return nil, ErrLabelNotExist{args: map[string]interface{}{"repoID": repoID}}
  107. }
  108. l := &Label{
  109. Name: labelName,
  110. RepoID: repoID,
  111. }
  112. has, err := e.Get(l)
  113. if err != nil {
  114. return nil, err
  115. } else if !has {
  116. return nil, ErrLabelNotExist{args: map[string]interface{}{"repoID": repoID}}
  117. }
  118. return l, nil
  119. }
  120. // getLabelInRepoByID returns a label by ID in given repository.
  121. // If pass repoID as 0, then ORM will ignore limitation of repository
  122. // and can return arbitrary label with any valid ID.
  123. func getLabelOfRepoByID(e Engine, repoID, labelID int64) (*Label, error) {
  124. if labelID <= 0 {
  125. return nil, ErrLabelNotExist{args: map[string]interface{}{"repoID": repoID, "labelID": labelID}}
  126. }
  127. l := &Label{
  128. ID: labelID,
  129. RepoID: repoID,
  130. }
  131. has, err := e.Get(l)
  132. if err != nil {
  133. return nil, err
  134. } else if !has {
  135. return nil, ErrLabelNotExist{args: map[string]interface{}{"repoID": repoID, "labelID": labelID}}
  136. }
  137. return l, nil
  138. }
  139. // GetLabelByID returns a label by given ID.
  140. func GetLabelByID(id int64) (*Label, error) {
  141. return getLabelOfRepoByID(x, 0, id)
  142. }
  143. // GetLabelOfRepoByID returns a label by ID in given repository.
  144. func GetLabelOfRepoByID(repoID, labelID int64) (*Label, error) {
  145. return getLabelOfRepoByID(x, repoID, labelID)
  146. }
  147. // GetLabelOfRepoByName returns a label by name in given repository.
  148. func GetLabelOfRepoByName(repoID int64, labelName string) (*Label, error) {
  149. return getLabelOfRepoByName(x, repoID, labelName)
  150. }
  151. // GetLabelsInRepoByIDs returns a list of labels by IDs in given repository,
  152. // it silently ignores label IDs that are not belong to the repository.
  153. func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) {
  154. labels := make([]*Label, 0, len(labelIDs))
  155. return labels, x.Where("repo_id = ?", repoID).In("id", tool.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
  156. }
  157. // GetLabelsByRepoID returns all labels that belong to given repository by ID.
  158. func GetLabelsByRepoID(repoID int64) ([]*Label, error) {
  159. labels := make([]*Label, 0, 10)
  160. return labels, x.Where("repo_id = ?", repoID).Asc("name").Find(&labels)
  161. }
  162. func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) {
  163. issueLabels, err := getIssueLabels(e, issueID)
  164. if err != nil {
  165. return nil, fmt.Errorf("getIssueLabels: %v", err)
  166. } else if len(issueLabels) == 0 {
  167. return []*Label{}, nil
  168. }
  169. labelIDs := make([]int64, len(issueLabels))
  170. for i := range issueLabels {
  171. labelIDs[i] = issueLabels[i].LabelID
  172. }
  173. labels := make([]*Label, 0, len(labelIDs))
  174. return labels, e.Where("id > 0").In("id", tool.Int64sToStrings(labelIDs)).Asc("name").Find(&labels)
  175. }
  176. // GetLabelsByIssueID returns all labels that belong to given issue by ID.
  177. func GetLabelsByIssueID(issueID int64) ([]*Label, error) {
  178. return getLabelsByIssueID(x, issueID)
  179. }
  180. func updateLabel(e Engine, l *Label) error {
  181. _, err := e.ID(l.ID).AllCols().Update(l)
  182. return err
  183. }
  184. // UpdateLabel updates label information.
  185. func UpdateLabel(l *Label) error {
  186. return updateLabel(x, l)
  187. }
  188. // DeleteLabel delete a label of given repository.
  189. func DeleteLabel(repoID, labelID int64) error {
  190. _, err := GetLabelOfRepoByID(repoID, labelID)
  191. if err != nil {
  192. if IsErrLabelNotExist(err) {
  193. return nil
  194. }
  195. return err
  196. }
  197. sess := x.NewSession()
  198. defer sess.Close()
  199. if err = sess.Begin(); err != nil {
  200. return err
  201. }
  202. if _, err = sess.ID(labelID).Delete(new(Label)); err != nil {
  203. return err
  204. } else if _, err = sess.Where("label_id = ?", labelID).Delete(new(IssueLabel)); err != nil {
  205. return err
  206. }
  207. return sess.Commit()
  208. }
  209. // .___ .____ ___. .__
  210. // | | ______ ________ __ ____ | | _____ \_ |__ ____ | |
  211. // | |/ ___// ___/ | \_/ __ \| | \__ \ | __ \_/ __ \| |
  212. // | |\___ \ \___ \| | /\ ___/| |___ / __ \| \_\ \ ___/| |__
  213. // |___/____ >____ >____/ \___ >_______ (____ /___ /\___ >____/
  214. // \/ \/ \/ \/ \/ \/ \/
  215. // IssueLabel represents an issue-lable relation.
  216. type IssueLabel struct {
  217. ID int64
  218. IssueID int64 `xorm:"UNIQUE(s)"`
  219. LabelID int64 `xorm:"UNIQUE(s)"`
  220. }
  221. func hasIssueLabel(e Engine, issueID, labelID int64) bool {
  222. has, _ := e.Where("issue_id = ? AND label_id = ?", issueID, labelID).Get(new(IssueLabel))
  223. return has
  224. }
  225. // HasIssueLabel returns true if issue has been labeled.
  226. func HasIssueLabel(issueID, labelID int64) bool {
  227. return hasIssueLabel(x, issueID, labelID)
  228. }
  229. func newIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  230. if _, err = e.Insert(&IssueLabel{
  231. IssueID: issue.ID,
  232. LabelID: label.ID,
  233. }); err != nil {
  234. return err
  235. }
  236. label.NumIssues++
  237. if issue.IsClosed {
  238. label.NumClosedIssues++
  239. }
  240. if err = updateLabel(e, label); err != nil {
  241. return fmt.Errorf("updateLabel: %v", err)
  242. }
  243. issue.Labels = append(issue.Labels, label)
  244. return nil
  245. }
  246. // NewIssueLabel creates a new issue-label relation.
  247. func NewIssueLabel(issue *Issue, label *Label) (err error) {
  248. if HasIssueLabel(issue.ID, label.ID) {
  249. return nil
  250. }
  251. sess := x.NewSession()
  252. defer sess.Close()
  253. if err = sess.Begin(); err != nil {
  254. return err
  255. }
  256. if err = newIssueLabel(sess, issue, label); err != nil {
  257. return err
  258. }
  259. return sess.Commit()
  260. }
  261. func newIssueLabels(e *xorm.Session, issue *Issue, labels []*Label) (err error) {
  262. for i := range labels {
  263. if hasIssueLabel(e, issue.ID, labels[i].ID) {
  264. continue
  265. }
  266. if err = newIssueLabel(e, issue, labels[i]); err != nil {
  267. return fmt.Errorf("newIssueLabel: %v", err)
  268. }
  269. }
  270. return nil
  271. }
  272. // NewIssueLabels creates a list of issue-label relations.
  273. func NewIssueLabels(issue *Issue, labels []*Label) (err error) {
  274. sess := x.NewSession()
  275. defer sess.Close()
  276. if err = sess.Begin(); err != nil {
  277. return err
  278. }
  279. if err = newIssueLabels(sess, issue, labels); err != nil {
  280. return err
  281. }
  282. return sess.Commit()
  283. }
  284. func getIssueLabels(e Engine, issueID int64) ([]*IssueLabel, error) {
  285. issueLabels := make([]*IssueLabel, 0, 10)
  286. return issueLabels, e.Where("issue_id=?", issueID).Asc("label_id").Find(&issueLabels)
  287. }
  288. // GetIssueLabels returns all issue-label relations of given issue by ID.
  289. func GetIssueLabels(issueID int64) ([]*IssueLabel, error) {
  290. return getIssueLabels(x, issueID)
  291. }
  292. func deleteIssueLabel(e *xorm.Session, issue *Issue, label *Label) (err error) {
  293. if _, err = e.Delete(&IssueLabel{
  294. IssueID: issue.ID,
  295. LabelID: label.ID,
  296. }); err != nil {
  297. return err
  298. }
  299. label.NumIssues--
  300. if issue.IsClosed {
  301. label.NumClosedIssues--
  302. }
  303. if err = updateLabel(e, label); err != nil {
  304. return fmt.Errorf("updateLabel: %v", err)
  305. }
  306. for i := range issue.Labels {
  307. if issue.Labels[i].ID == label.ID {
  308. issue.Labels = append(issue.Labels[:i], issue.Labels[i+1:]...)
  309. break
  310. }
  311. }
  312. return nil
  313. }
  314. // DeleteIssueLabel deletes issue-label relation.
  315. func DeleteIssueLabel(issue *Issue, label *Label) (err error) {
  316. sess := x.NewSession()
  317. defer sess.Close()
  318. if err = sess.Begin(); err != nil {
  319. return err
  320. }
  321. if err = deleteIssueLabel(sess, issue, label); err != nil {
  322. return err
  323. }
  324. return sess.Commit()
  325. }