release.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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:"-"`
  31. CreatedUnix int64
  32. }
  33. func (r *Release) BeforeInsert() {
  34. if r.CreatedUnix == 0 {
  35. r.CreatedUnix = time.Now().Unix()
  36. }
  37. }
  38. func (r *Release) AfterSet(colName string, _ xorm.Cell) {
  39. switch colName {
  40. case "created_unix":
  41. r.Created = time.Unix(r.CreatedUnix, 0).Local()
  42. }
  43. }
  44. // IsReleaseExist returns true if release with given tag name already exists.
  45. func IsReleaseExist(repoID int64, tagName string) (bool, error) {
  46. if len(tagName) == 0 {
  47. return false, nil
  48. }
  49. return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
  50. }
  51. func createTag(gitRepo *git.Repository, rel *Release) error {
  52. // Only actual create when publish.
  53. if !rel.IsDraft {
  54. if !gitRepo.IsTagExist(rel.TagName) {
  55. commit, err := gitRepo.GetBranchCommit(rel.Target)
  56. if err != nil {
  57. return fmt.Errorf("GetBranchCommit: %v", err)
  58. }
  59. // Trim '--' prefix to prevent command line argument vulnerability.
  60. rel.TagName = strings.TrimPrefix(rel.TagName, "--")
  61. if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
  62. if strings.Contains(err.Error(), "is not a valid tag name") {
  63. return ErrInvalidTagName{rel.TagName}
  64. }
  65. return err
  66. }
  67. } else {
  68. commit, err := gitRepo.GetTagCommit(rel.TagName)
  69. if err != nil {
  70. return fmt.Errorf("GetTagCommit: %v", err)
  71. }
  72. rel.Sha1 = commit.ID.String()
  73. rel.NumCommits, err = commit.CommitsCount()
  74. if err != nil {
  75. return fmt.Errorf("CommitsCount: %v", err)
  76. }
  77. }
  78. }
  79. return nil
  80. }
  81. // CreateRelease creates a new release of repository.
  82. func CreateRelease(gitRepo *git.Repository, rel *Release) error {
  83. isExist, err := IsReleaseExist(rel.RepoID, rel.TagName)
  84. if err != nil {
  85. return err
  86. } else if isExist {
  87. return ErrReleaseAlreadyExist{rel.TagName}
  88. }
  89. if err = createTag(gitRepo, rel); err != nil {
  90. return err
  91. }
  92. rel.LowerTagName = strings.ToLower(rel.TagName)
  93. _, err = x.InsertOne(rel)
  94. return err
  95. }
  96. // GetRelease returns release by given ID.
  97. func GetRelease(repoID int64, tagName string) (*Release, error) {
  98. isExist, err := IsReleaseExist(repoID, tagName)
  99. if err != nil {
  100. return nil, err
  101. } else if !isExist {
  102. return nil, ErrReleaseNotExist{0, tagName}
  103. }
  104. rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
  105. _, err = x.Get(rel)
  106. return rel, err
  107. }
  108. // GetReleaseByID returns release with given ID.
  109. func GetReleaseByID(id int64) (*Release, error) {
  110. rel := new(Release)
  111. has, err := x.Id(id).Get(rel)
  112. if err != nil {
  113. return nil, err
  114. } else if !has {
  115. return nil, ErrReleaseNotExist{id, ""}
  116. }
  117. return rel, nil
  118. }
  119. // GetReleasesByRepoID returns a list of releases of repository.
  120. func GetReleasesByRepoID(repoID int64) (rels []*Release, err error) {
  121. err = x.Desc("created_unix").Find(&rels, Release{RepoID: repoID})
  122. return rels, err
  123. }
  124. type ReleaseSorter struct {
  125. rels []*Release
  126. }
  127. func (rs *ReleaseSorter) Len() int {
  128. return len(rs.rels)
  129. }
  130. func (rs *ReleaseSorter) Less(i, j int) bool {
  131. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  132. if diffNum != 0 {
  133. return diffNum > 0
  134. }
  135. return rs.rels[i].Created.After(rs.rels[j].Created)
  136. }
  137. func (rs *ReleaseSorter) Swap(i, j int) {
  138. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  139. }
  140. // SortReleases sorts releases by number of commits and created time.
  141. func SortReleases(rels []*Release) {
  142. sorter := &ReleaseSorter{rels: rels}
  143. sort.Sort(sorter)
  144. }
  145. // UpdateRelease updates information of a release.
  146. func UpdateRelease(gitRepo *git.Repository, rel *Release) (err error) {
  147. if err = createTag(gitRepo, rel); err != nil {
  148. return err
  149. }
  150. _, err = x.Id(rel.ID).AllCols().Update(rel)
  151. return err
  152. }
  153. // DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
  154. func DeleteReleaseByID(id int64) error {
  155. rel, err := GetReleaseByID(id)
  156. if err != nil {
  157. return fmt.Errorf("GetReleaseByID: %v", err)
  158. }
  159. repo, err := GetRepositoryByID(rel.RepoID)
  160. if err != nil {
  161. return fmt.Errorf("GetRepositoryByID: %v", err)
  162. }
  163. _, stderr, err := process.ExecDir(-1, repo.RepoPath(),
  164. fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID),
  165. "git", "tag", "-d", rel.TagName)
  166. if err != nil && !strings.Contains(stderr, "not found") {
  167. return fmt.Errorf("git tag -d: %v - %s", err, stderr)
  168. }
  169. if _, err = x.Id(rel.ID).Delete(new(Release)); err != nil {
  170. return fmt.Errorf("Delete: %v", err)
  171. }
  172. return nil
  173. }