Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

branches.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2016 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 models
  5. import (
  6. "fmt"
  7. "strings"
  8. "time"
  9. )
  10. const (
  11. // ProtectedBranchRepoID protected Repo ID
  12. ProtectedBranchRepoID = "GITEA_REPO_ID"
  13. )
  14. // ProtectedBranch struct
  15. type ProtectedBranch struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. RepoID int64 `xorm:"UNIQUE(s)"`
  18. BranchName string `xorm:"UNIQUE(s)"`
  19. CanPush bool
  20. Created time.Time `xorm:"-"`
  21. CreatedUnix int64
  22. Updated time.Time `xorm:"-"`
  23. UpdatedUnix int64
  24. }
  25. // BeforeInsert before protected branch insert create and update time
  26. func (protectBranch *ProtectedBranch) BeforeInsert() {
  27. protectBranch.CreatedUnix = time.Now().Unix()
  28. protectBranch.UpdatedUnix = protectBranch.CreatedUnix
  29. }
  30. // BeforeUpdate before protected branch update time
  31. func (protectBranch *ProtectedBranch) BeforeUpdate() {
  32. protectBranch.UpdatedUnix = time.Now().Unix()
  33. }
  34. // GetProtectedBranchByRepoID getting protected branch by repo ID
  35. func GetProtectedBranchByRepoID(RepoID int64) ([]*ProtectedBranch, error) {
  36. protectedBranches := make([]*ProtectedBranch, 0)
  37. return protectedBranches, x.Where("repo_id = ?", RepoID).Desc("updated_unix").Find(&protectedBranches)
  38. }
  39. // GetProtectedBranchBy getting protected branch by ID/Name
  40. func GetProtectedBranchBy(repoID int64, BranchName string) (*ProtectedBranch, error) {
  41. rel := &ProtectedBranch{RepoID: repoID, BranchName: strings.ToLower(BranchName)}
  42. has, err := x.Get(rel)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if !has {
  47. return nil, nil
  48. }
  49. return rel, nil
  50. }
  51. // GetProtectedBranches get all protected branches
  52. func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) {
  53. protectedBranches := make([]*ProtectedBranch, 0)
  54. return protectedBranches, x.Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID})
  55. }
  56. // AddProtectedBranch add protection to branch
  57. func (repo *Repository) AddProtectedBranch(branchName string, canPush bool) error {
  58. protectedBranch := &ProtectedBranch{
  59. RepoID: repo.ID,
  60. BranchName: branchName,
  61. }
  62. has, err := x.Get(protectedBranch)
  63. if err != nil {
  64. return err
  65. } else if has {
  66. return nil
  67. }
  68. sess := x.NewSession()
  69. defer sessionRelease(sess)
  70. if err = sess.Begin(); err != nil {
  71. return err
  72. }
  73. protectedBranch.CanPush = canPush
  74. if _, err = sess.InsertOne(protectedBranch); err != nil {
  75. return err
  76. }
  77. return sess.Commit()
  78. }
  79. // ChangeProtectedBranch access mode sets new access mode for the ProtectedBranch.
  80. func (repo *Repository) ChangeProtectedBranch(id int64, canPush bool) error {
  81. ProtectedBranch := &ProtectedBranch{
  82. RepoID: repo.ID,
  83. ID: id,
  84. }
  85. has, err := x.Get(ProtectedBranch)
  86. if err != nil {
  87. return fmt.Errorf("get ProtectedBranch: %v", err)
  88. } else if !has {
  89. return nil
  90. }
  91. if ProtectedBranch.CanPush == canPush {
  92. return nil
  93. }
  94. ProtectedBranch.CanPush = canPush
  95. sess := x.NewSession()
  96. defer sessionRelease(sess)
  97. if err = sess.Begin(); err != nil {
  98. return err
  99. }
  100. if _, err = sess.Id(ProtectedBranch.ID).AllCols().Update(ProtectedBranch); err != nil {
  101. return fmt.Errorf("update ProtectedBranch: %v", err)
  102. }
  103. return sess.Commit()
  104. }
  105. // DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
  106. func (repo *Repository) DeleteProtectedBranch(id int64) (err error) {
  107. protectedBranch := &ProtectedBranch{
  108. RepoID: repo.ID,
  109. ID: id,
  110. }
  111. sess := x.NewSession()
  112. defer sessionRelease(sess)
  113. if err = sess.Begin(); err != nil {
  114. return err
  115. }
  116. if affected, err := sess.Delete(protectedBranch); err != nil {
  117. return err
  118. } else if affected != 1 {
  119. return fmt.Errorf("delete protected branch ID(%v) failed", id)
  120. }
  121. return sess.Commit()
  122. }
  123. // newProtectedBranch insert one queue
  124. func newProtectedBranch(protectedBranch *ProtectedBranch) error {
  125. _, err := x.InsertOne(protectedBranch)
  126. return err
  127. }
  128. // UpdateProtectedBranch update queue
  129. func UpdateProtectedBranch(protectedBranch *ProtectedBranch) error {
  130. _, err := x.Update(protectedBranch)
  131. return err
  132. }