release.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. "fmt"
  7. "sort"
  8. "strings"
  9. "time"
  10. "github.com/go-xorm/xorm"
  11. "github.com/gogits/git-module"
  12. "github.com/gogits/gogs/modules/process"
  13. )
  14. // Release represents a release of repository.
  15. type Release struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. RepoID int64
  18. PublisherID int64
  19. Publisher *User `xorm:"-"`
  20. TagName string
  21. LowerTagName string
  22. Target string
  23. Title string
  24. Sha1 string `xorm:"VARCHAR(40)"`
  25. NumCommits int64
  26. NumCommitsBehind int64 `xorm:"-"`
  27. Note string `xorm:"TEXT"`
  28. IsDraft bool `xorm:"NOT NULL DEFAULT false"`
  29. IsPrerelease bool
  30. Created time.Time `xorm:"CREATED"`
  31. }
  32. func (r *Release) AfterSet(colName string, _ xorm.Cell) {
  33. switch colName {
  34. case "created":
  35. r.Created = regulateTimeZone(r.Created)
  36. }
  37. }
  38. // IsReleaseExist returns true if release with given tag name already exists.
  39. func IsReleaseExist(repoID int64, tagName string) (bool, error) {
  40. if len(tagName) == 0 {
  41. return false, nil
  42. }
  43. return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
  44. }
  45. func createTag(gitRepo *git.Repository, rel *Release) error {
  46. // Only actual create when publish.
  47. if !rel.IsDraft {
  48. if !gitRepo.IsTagExist(rel.TagName) {
  49. commit, err := gitRepo.GetBranchCommit(rel.Target)
  50. if err != nil {
  51. return fmt.Errorf("GetBranchCommit: %v", err)
  52. }
  53. if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
  54. return err
  55. }
  56. } else {
  57. commit, err := gitRepo.GetTagCommit(rel.TagName)
  58. if err != nil {
  59. return fmt.Errorf("GetTagCommit: %v", err)
  60. }
  61. rel.NumCommits, err = commit.CommitsCount()
  62. if err != nil {
  63. return fmt.Errorf("CommitsCount: %v", err)
  64. }
  65. }
  66. }
  67. return nil
  68. }
  69. // CreateRelease creates a new release of repository.
  70. func CreateRelease(gitRepo *git.Repository, rel *Release) error {
  71. isExist, err := IsReleaseExist(rel.RepoID, rel.TagName)
  72. if err != nil {
  73. return err
  74. } else if isExist {
  75. return ErrReleaseAlreadyExist{rel.TagName}
  76. }
  77. if err = createTag(gitRepo, rel); err != nil {
  78. return err
  79. }
  80. rel.LowerTagName = strings.ToLower(rel.TagName)
  81. _, err = x.InsertOne(rel)
  82. return err
  83. }
  84. // GetRelease returns release by given ID.
  85. func GetRelease(repoID int64, tagName string) (*Release, error) {
  86. isExist, err := IsReleaseExist(repoID, tagName)
  87. if err != nil {
  88. return nil, err
  89. } else if !isExist {
  90. return nil, ErrReleaseNotExist{0, tagName}
  91. }
  92. rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
  93. _, err = x.Get(rel)
  94. return rel, err
  95. }
  96. // GetReleaseByID returns release with given ID.
  97. func GetReleaseByID(id int64) (*Release, error) {
  98. rel := new(Release)
  99. has, err := x.Id(id).Get(rel)
  100. if err != nil {
  101. return nil, err
  102. } else if !has {
  103. return nil, ErrReleaseNotExist{id, ""}
  104. }
  105. return rel, nil
  106. }
  107. // GetReleasesByRepoID returns a list of releases of repository.
  108. func GetReleasesByRepoID(repoID int64) (rels []*Release, err error) {
  109. err = x.Desc("created").Find(&rels, Release{RepoID: repoID})
  110. return rels, err
  111. }
  112. type ReleaseSorter struct {
  113. rels []*Release
  114. }
  115. func (rs *ReleaseSorter) Len() int {
  116. return len(rs.rels)
  117. }
  118. func (rs *ReleaseSorter) Less(i, j int) bool {
  119. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  120. if diffNum != 0 {
  121. return diffNum > 0
  122. }
  123. return rs.rels[i].Created.After(rs.rels[j].Created)
  124. }
  125. func (rs *ReleaseSorter) Swap(i, j int) {
  126. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  127. }
  128. // SortReleases sorts releases by number of commits and created time.
  129. func SortReleases(rels []*Release) {
  130. sorter := &ReleaseSorter{rels: rels}
  131. sort.Sort(sorter)
  132. }
  133. // UpdateRelease updates information of a release.
  134. func UpdateRelease(gitRepo *git.Repository, rel *Release) (err error) {
  135. if err = createTag(gitRepo, rel); err != nil {
  136. return err
  137. }
  138. _, err = x.Id(rel.ID).AllCols().Update(rel)
  139. return err
  140. }
  141. // DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
  142. func DeleteReleaseByID(id int64) error {
  143. rel, err := GetReleaseByID(id)
  144. if err != nil {
  145. return fmt.Errorf("GetReleaseByID: %v", err)
  146. }
  147. repo, err := GetRepositoryByID(rel.RepoID)
  148. if err != nil {
  149. return fmt.Errorf("GetRepositoryByID: %v", err)
  150. }
  151. _, stderr, err := process.ExecDir(-1, repo.RepoPath(),
  152. fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID),
  153. "git", "tag", "-d", rel.TagName)
  154. if err != nil && !strings.Contains(stderr, "not found") {
  155. return fmt.Errorf("git tag -d: %v - %s", err, stderr)
  156. }
  157. if _, err = x.Id(rel.ID).Delete(new(Release)); err != nil {
  158. return fmt.Errorf("Delete: %v", err)
  159. }
  160. return nil
  161. }