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.

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