release.go 3.5 KB

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