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.

branches.go 4.1KB

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