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