A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

96 lines
2.4 KiB

  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. "strings"
  8. )
  9. // PushUpdateDeleteTagsContext updates a number of delete tags with context
  10. func PushUpdateDeleteTagsContext(ctx DBContext, repo *Repository, tags []string) error {
  11. return pushUpdateDeleteTags(ctx.e, repo, tags)
  12. }
  13. func pushUpdateDeleteTags(e Engine, repo *Repository, tags []string) error {
  14. if len(tags) == 0 {
  15. return nil
  16. }
  17. lowerTags := make([]string, 0, len(tags))
  18. for _, tag := range tags {
  19. lowerTags = append(lowerTags, strings.ToLower(tag))
  20. }
  21. if _, err := e.
  22. Where("repo_id = ? AND is_tag = ?", repo.ID, true).
  23. In("lower_tag_name", lowerTags).
  24. Delete(new(Release)); err != nil {
  25. return fmt.Errorf("Delete: %v", err)
  26. }
  27. if _, err := e.
  28. Where("repo_id = ? AND is_tag = ?", repo.ID, false).
  29. In("lower_tag_name", lowerTags).
  30. Cols("is_draft", "num_commits", "sha1").
  31. Update(&Release{
  32. IsDraft: true,
  33. }); err != nil {
  34. return fmt.Errorf("Update: %v", err)
  35. }
  36. return nil
  37. }
  38. // PushUpdateDeleteTag must be called for any push actions to delete tag
  39. func PushUpdateDeleteTag(repo *Repository, tagName string) error {
  40. rel, err := GetRelease(repo.ID, tagName)
  41. if err != nil {
  42. if IsErrReleaseNotExist(err) {
  43. return nil
  44. }
  45. return fmt.Errorf("GetRelease: %v", err)
  46. }
  47. if rel.IsTag {
  48. if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
  49. return fmt.Errorf("Delete: %v", err)
  50. }
  51. } else {
  52. rel.IsDraft = true
  53. rel.NumCommits = 0
  54. rel.Sha1 = ""
  55. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  56. return fmt.Errorf("Update: %v", err)
  57. }
  58. }
  59. return nil
  60. }
  61. // SaveOrUpdateTag must be called for any push actions to add tag
  62. func SaveOrUpdateTag(repo *Repository, newRel *Release) error {
  63. rel, err := GetRelease(repo.ID, newRel.TagName)
  64. if err != nil && !IsErrReleaseNotExist(err) {
  65. return fmt.Errorf("GetRelease: %v", err)
  66. }
  67. if rel == nil {
  68. rel = newRel
  69. if _, err = x.Insert(rel); err != nil {
  70. return fmt.Errorf("InsertOne: %v", err)
  71. }
  72. } else {
  73. rel.Sha1 = newRel.Sha1
  74. rel.CreatedUnix = newRel.CreatedUnix
  75. rel.NumCommits = newRel.NumCommits
  76. rel.IsDraft = false
  77. if rel.IsTag && newRel.PublisherID > 0 {
  78. rel.PublisherID = newRel.PublisherID
  79. }
  80. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  81. return fmt.Errorf("Update: %v", err)
  82. }
  83. }
  84. return nil
  85. }