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.

213 lines
5.2 KiB

10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 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. "fmt"
  7. "sort"
  8. "strings"
  9. "time"
  10. "github.com/go-xorm/xorm"
  11. "code.gitea.io/git"
  12. "code.gitea.io/gitea/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.
  112. Id(id).
  113. Get(rel)
  114. if err != nil {
  115. return nil, err
  116. } else if !has {
  117. return nil, ErrReleaseNotExist{id, ""}
  118. }
  119. return rel, nil
  120. }
  121. // GetReleasesByRepoID returns a list of releases of repository.
  122. func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err error) {
  123. if page <= 0 {
  124. page = 1
  125. }
  126. err = x.
  127. Desc("created_unix").
  128. Limit(pageSize, (page-1)*pageSize).
  129. Find(&rels, Release{RepoID: repoID})
  130. return rels, err
  131. }
  132. type ReleaseSorter struct {
  133. rels []*Release
  134. }
  135. func (rs *ReleaseSorter) Len() int {
  136. return len(rs.rels)
  137. }
  138. func (rs *ReleaseSorter) Less(i, j int) bool {
  139. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  140. if diffNum != 0 {
  141. return diffNum > 0
  142. }
  143. return rs.rels[i].Created.After(rs.rels[j].Created)
  144. }
  145. func (rs *ReleaseSorter) Swap(i, j int) {
  146. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  147. }
  148. // SortReleases sorts releases by number of commits and created time.
  149. func SortReleases(rels []*Release) {
  150. sorter := &ReleaseSorter{rels: rels}
  151. sort.Sort(sorter)
  152. }
  153. // UpdateRelease updates information of a release.
  154. func UpdateRelease(gitRepo *git.Repository, rel *Release) (err error) {
  155. if err = createTag(gitRepo, rel); err != nil {
  156. return err
  157. }
  158. _, err = x.Id(rel.ID).AllCols().Update(rel)
  159. return err
  160. }
  161. // DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
  162. func DeleteReleaseByID(id int64) error {
  163. rel, err := GetReleaseByID(id)
  164. if err != nil {
  165. return fmt.Errorf("GetReleaseByID: %v", err)
  166. }
  167. repo, err := GetRepositoryByID(rel.RepoID)
  168. if err != nil {
  169. return fmt.Errorf("GetRepositoryByID: %v", err)
  170. }
  171. _, stderr, err := process.ExecDir(-1, repo.RepoPath(),
  172. fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID),
  173. "git", "tag", "-d", rel.TagName)
  174. if err != nil && !strings.Contains(stderr, "not found") {
  175. return fmt.Errorf("git tag -d: %v - %s", err, stderr)
  176. }
  177. if _, err = x.Id(rel.ID).Delete(new(Release)); err != nil {
  178. return fmt.Errorf("Delete: %v", err)
  179. }
  180. return nil
  181. }