release.go 5.1 KB

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