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.

155 lines
3.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 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. "errors"
  7. "sort"
  8. "strings"
  9. "time"
  10. "github.com/go-xorm/xorm"
  11. "github.com/gogits/gogs/modules/git"
  12. )
  13. var (
  14. ErrReleaseAlreadyExist = errors.New("Release already exist")
  15. ErrReleaseNotExist = errors.New("Release does not exist")
  16. )
  17. // Release represents a release of repository.
  18. type Release struct {
  19. Id int64
  20. RepoId int64
  21. PublisherId int64
  22. Publisher *User `xorm:"-"`
  23. TagName string
  24. LowerTagName string
  25. Target string
  26. Title string
  27. Sha1 string `xorm:"VARCHAR(40)"`
  28. NumCommits int
  29. NumCommitsBehind int `xorm:"-"`
  30. Note string `xorm:"TEXT"`
  31. IsDraft bool `xorm:"NOT NULL DEFAULT false"`
  32. IsPrerelease bool
  33. Created time.Time `xorm:"CREATED"`
  34. }
  35. func (r *Release) AfterSet(colName string, _ xorm.Cell) {
  36. switch colName {
  37. case "created":
  38. r.Created = regulateTimeZone(r.Created)
  39. }
  40. }
  41. // IsReleaseExist returns true if release with given tag name already exists.
  42. func IsReleaseExist(repoId int64, tagName string) (bool, error) {
  43. if len(tagName) == 0 {
  44. return false, nil
  45. }
  46. return x.Get(&Release{RepoId: repoId, LowerTagName: strings.ToLower(tagName)})
  47. }
  48. func createTag(gitRepo *git.Repository, rel *Release) error {
  49. // Only actual create when publish.
  50. if !rel.IsDraft {
  51. if !gitRepo.IsTagExist(rel.TagName) {
  52. commit, err := gitRepo.GetCommitOfBranch(rel.Target)
  53. if err != nil {
  54. return err
  55. }
  56. if err = gitRepo.CreateTag(rel.TagName, commit.Id.String()); err != nil {
  57. return err
  58. }
  59. } else {
  60. commit, err := gitRepo.GetCommitOfTag(rel.TagName)
  61. if err != nil {
  62. return err
  63. }
  64. rel.NumCommits, err = commit.CommitsCount()
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. }
  70. return nil
  71. }
  72. // CreateRelease creates a new release of repository.
  73. func CreateRelease(gitRepo *git.Repository, rel *Release) error {
  74. isExist, err := IsReleaseExist(rel.RepoId, rel.TagName)
  75. if err != nil {
  76. return err
  77. } else if isExist {
  78. return ErrReleaseAlreadyExist
  79. }
  80. if err = createTag(gitRepo, rel); err != nil {
  81. return err
  82. }
  83. rel.LowerTagName = strings.ToLower(rel.TagName)
  84. _, err = x.InsertOne(rel)
  85. return err
  86. }
  87. // GetRelease returns release by given ID.
  88. func GetRelease(repoId int64, tagName string) (*Release, error) {
  89. isExist, err := IsReleaseExist(repoId, tagName)
  90. if err != nil {
  91. return nil, err
  92. } else if !isExist {
  93. return nil, ErrReleaseNotExist
  94. }
  95. rel := &Release{RepoId: repoId, LowerTagName: strings.ToLower(tagName)}
  96. _, err = x.Get(rel)
  97. return rel, err
  98. }
  99. // GetReleasesByRepoId returns a list of releases of repository.
  100. func GetReleasesByRepoId(repoId int64) (rels []*Release, err error) {
  101. err = x.Desc("created").Find(&rels, Release{RepoId: repoId})
  102. return rels, err
  103. }
  104. type ReleaseSorter struct {
  105. rels []*Release
  106. }
  107. func (rs *ReleaseSorter) Len() int {
  108. return len(rs.rels)
  109. }
  110. func (rs *ReleaseSorter) Less(i, j int) bool {
  111. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  112. if diffNum != 0 {
  113. return diffNum > 0
  114. }
  115. return rs.rels[i].Created.After(rs.rels[j].Created)
  116. }
  117. func (rs *ReleaseSorter) Swap(i, j int) {
  118. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  119. }
  120. // SortReleases sorts releases by number of commits and created time.
  121. func SortReleases(rels []*Release) {
  122. sorter := &ReleaseSorter{rels: rels}
  123. sort.Sort(sorter)
  124. }
  125. // UpdateRelease updates information of a release.
  126. func UpdateRelease(gitRepo *git.Repository, rel *Release) (err error) {
  127. if err = createTag(gitRepo, rel); err != nil {
  128. return err
  129. }
  130. _, err = x.Id(rel.Id).AllCols().Update(rel)
  131. return err
  132. }