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.

172 lines
4.7 KiB

  1. // Copyright 2019 The Gitea 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 release
  5. import (
  6. "fmt"
  7. "os"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/notification"
  13. "code.gitea.io/gitea/modules/repository"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. )
  16. func createTag(gitRepo *git.Repository, rel *models.Release) error {
  17. // Only actual create when publish.
  18. if !rel.IsDraft {
  19. if !gitRepo.IsTagExist(rel.TagName) {
  20. commit, err := gitRepo.GetCommit(rel.Target)
  21. if err != nil {
  22. return fmt.Errorf("GetCommit: %v", err)
  23. }
  24. // Trim '--' prefix to prevent command line argument vulnerability.
  25. rel.TagName = strings.TrimPrefix(rel.TagName, "--")
  26. if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
  27. if strings.Contains(err.Error(), "is not a valid tag name") {
  28. return models.ErrInvalidTagName{
  29. TagName: rel.TagName,
  30. }
  31. }
  32. return err
  33. }
  34. rel.LowerTagName = strings.ToLower(rel.TagName)
  35. // Prepare Notify
  36. if err := rel.LoadAttributes(); err != nil {
  37. log.Error("LoadAttributes: %v", err)
  38. return err
  39. }
  40. notification.NotifyPushCommits(
  41. rel.Publisher, rel.Repo, git.TagPrefix+rel.TagName,
  42. git.EmptySHA, commit.ID.String(), repository.NewPushCommits())
  43. notification.NotifyCreateRef(rel.Publisher, rel.Repo, "tag", git.TagPrefix+rel.TagName)
  44. }
  45. commit, err := gitRepo.GetTagCommit(rel.TagName)
  46. if err != nil {
  47. return fmt.Errorf("GetTagCommit: %v", err)
  48. }
  49. rel.Sha1 = commit.ID.String()
  50. rel.CreatedUnix = timeutil.TimeStampNow()
  51. rel.NumCommits, err = commit.CommitsCount()
  52. if err != nil {
  53. return fmt.Errorf("CommitsCount: %v", err)
  54. }
  55. } else {
  56. rel.CreatedUnix = timeutil.TimeStampNow()
  57. }
  58. return nil
  59. }
  60. // CreateRelease creates a new release of repository.
  61. func CreateRelease(gitRepo *git.Repository, rel *models.Release, attachmentUUIDs []string) error {
  62. isExist, err := models.IsReleaseExist(rel.RepoID, rel.TagName)
  63. if err != nil {
  64. return err
  65. } else if isExist {
  66. return models.ErrReleaseAlreadyExist{
  67. TagName: rel.TagName,
  68. }
  69. }
  70. if err = createTag(gitRepo, rel); err != nil {
  71. return err
  72. }
  73. rel.LowerTagName = strings.ToLower(rel.TagName)
  74. if err = models.InsertRelease(rel); err != nil {
  75. return err
  76. }
  77. if err = models.AddReleaseAttachments(rel.ID, attachmentUUIDs); err != nil {
  78. return err
  79. }
  80. if !rel.IsDraft {
  81. notification.NotifyNewRelease(rel)
  82. }
  83. return nil
  84. }
  85. // UpdateRelease updates information of a release.
  86. func UpdateRelease(doer *models.User, gitRepo *git.Repository, rel *models.Release, attachmentUUIDs []string) (err error) {
  87. if err = createTag(gitRepo, rel); err != nil {
  88. return err
  89. }
  90. rel.LowerTagName = strings.ToLower(rel.TagName)
  91. if err = models.UpdateRelease(models.DefaultDBContext(), rel); err != nil {
  92. return err
  93. }
  94. if err = models.AddReleaseAttachments(rel.ID, attachmentUUIDs); err != nil {
  95. log.Error("AddReleaseAttachments: %v", err)
  96. }
  97. notification.NotifyUpdateRelease(doer, rel)
  98. return err
  99. }
  100. // DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
  101. func DeleteReleaseByID(id int64, doer *models.User, delTag bool) error {
  102. rel, err := models.GetReleaseByID(id)
  103. if err != nil {
  104. return fmt.Errorf("GetReleaseByID: %v", err)
  105. }
  106. repo, err := models.GetRepositoryByID(rel.RepoID)
  107. if err != nil {
  108. return fmt.Errorf("GetRepositoryByID: %v", err)
  109. }
  110. if delTag {
  111. if stdout, err := git.NewCommand("tag", "-d", rel.TagName).
  112. SetDescription(fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID)).
  113. RunInDir(repo.RepoPath()); err != nil && !strings.Contains(err.Error(), "not found") {
  114. log.Error("DeleteReleaseByID (git tag -d): %d in %v Failed:\nStdout: %s\nError: %v", rel.ID, repo, stdout, err)
  115. return fmt.Errorf("git tag -d: %v", err)
  116. }
  117. if err := models.DeleteReleaseByID(id); err != nil {
  118. return fmt.Errorf("DeleteReleaseByID: %v", err)
  119. }
  120. } else {
  121. rel.IsTag = true
  122. rel.IsDraft = false
  123. rel.IsPrerelease = false
  124. rel.Title = ""
  125. rel.Note = ""
  126. if err = models.UpdateRelease(models.DefaultDBContext(), rel); err != nil {
  127. return fmt.Errorf("Update: %v", err)
  128. }
  129. }
  130. rel.Repo = repo
  131. if err = rel.LoadAttributes(); err != nil {
  132. return fmt.Errorf("LoadAttributes: %v", err)
  133. }
  134. if err := models.DeleteAttachmentsByRelease(rel.ID); err != nil {
  135. return fmt.Errorf("DeleteAttachments: %v", err)
  136. }
  137. for i := range rel.Attachments {
  138. attachment := rel.Attachments[i]
  139. if err := os.RemoveAll(attachment.LocalPath()); err != nil {
  140. log.Error("Delete attachment %s of release %s failed: %v", attachment.UUID, rel.ID, err)
  141. }
  142. }
  143. notification.NotifyDeleteRelease(doer, rel)
  144. return nil
  145. }