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.

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