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.

173 lines
4.3 KiB

  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. // IsProtectedBranch checks if branch is protected
  57. func (repo *Repository) IsProtectedBranch(branchName string) (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 true, err
  65. } else if has {
  66. return true, nil
  67. }
  68. return false, nil
  69. }
  70. // AddProtectedBranch add protection to branch
  71. func (repo *Repository) AddProtectedBranch(branchName string, canPush bool) error {
  72. protectedBranch := &ProtectedBranch{
  73. RepoID: repo.ID,
  74. BranchName: branchName,
  75. }
  76. has, err := x.Get(protectedBranch)
  77. if err != nil {
  78. return err
  79. } else if has {
  80. return nil
  81. }
  82. sess := x.NewSession()
  83. defer sess.Close()
  84. if err = sess.Begin(); err != nil {
  85. return err
  86. }
  87. protectedBranch.CanPush = canPush
  88. if _, err = sess.InsertOne(protectedBranch); err != nil {
  89. return err
  90. }
  91. return sess.Commit()
  92. }
  93. // ChangeProtectedBranch access mode sets new access mode for the ProtectedBranch.
  94. func (repo *Repository) ChangeProtectedBranch(id int64, canPush bool) error {
  95. ProtectedBranch := &ProtectedBranch{
  96. RepoID: repo.ID,
  97. ID: id,
  98. }
  99. has, err := x.Get(ProtectedBranch)
  100. if err != nil {
  101. return fmt.Errorf("get ProtectedBranch: %v", err)
  102. } else if !has {
  103. return nil
  104. }
  105. if ProtectedBranch.CanPush == canPush {
  106. return nil
  107. }
  108. ProtectedBranch.CanPush = canPush
  109. sess := x.NewSession()
  110. defer sess.Close()
  111. if err = sess.Begin(); err != nil {
  112. return err
  113. }
  114. if _, err = sess.Id(ProtectedBranch.ID).AllCols().Update(ProtectedBranch); err != nil {
  115. return fmt.Errorf("update ProtectedBranch: %v", err)
  116. }
  117. return sess.Commit()
  118. }
  119. // DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
  120. func (repo *Repository) DeleteProtectedBranch(id int64) (err error) {
  121. protectedBranch := &ProtectedBranch{
  122. RepoID: repo.ID,
  123. ID: id,
  124. }
  125. sess := x.NewSession()
  126. defer sess.Close()
  127. if err = sess.Begin(); err != nil {
  128. return err
  129. }
  130. if affected, err := sess.Delete(protectedBranch); err != nil {
  131. return err
  132. } else if affected != 1 {
  133. return fmt.Errorf("delete protected branch ID(%v) failed", id)
  134. }
  135. return sess.Commit()
  136. }
  137. // newProtectedBranch insert one queue
  138. func newProtectedBranch(protectedBranch *ProtectedBranch) error {
  139. _, err := x.InsertOne(protectedBranch)
  140. return err
  141. }
  142. // UpdateProtectedBranch update queue
  143. func UpdateProtectedBranch(protectedBranch *ProtectedBranch) error {
  144. _, err := x.Update(protectedBranch)
  145. return err
  146. }