Protected branches system (#339)
* Protected branches system * Moved default branch to branches section (`:org/:reponame/settings/branches`). * Initial support Protected Branch. - Admin does not restrict - Owner not to limit - To write permission restrictions * reformat tmpl * finished the UI and add/delete protected branch response * remove unused comment * indent all the template files and remove ru translations since we use crowdin * fix the push bugtokarchuk/v1.17
parent
fe5ff8e4b2
commit
fd941db246
@ -0,0 +1,161 @@ |
||||
// Copyright 2016 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package models |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
"time" |
||||
) |
||||
|
||||
// Protected metadata
|
||||
const ( |
||||
// Protected User ID
|
||||
ProtectedBranchUserID = "GITEA_USER_ID" |
||||
// Protected Repo ID
|
||||
ProtectedBranchRepoID = "GITEA_REPO_ID" |
||||
// Protected access mode
|
||||
ProtectedBranchAccessMode = "GITEA_ACCESS_MODE" |
||||
) |
||||
|
||||
// ProtectedBranch struct
|
||||
type ProtectedBranch struct { |
||||
ID int64 `xorm:"pk autoincr"` |
||||
RepoID int64 `xorm:"UNIQUE(s)"` |
||||
BranchName string `xorm:"UNIQUE(s)"` |
||||
CanPush bool |
||||
Created time.Time `xorm:"-"` |
||||
CreatedUnix int64 |
||||
Updated time.Time `xorm:"-"` |
||||
UpdatedUnix int64 |
||||
} |
||||
|
||||
// BeforeInsert before protected branch insert create and update time
|
||||
func (protectBranch *ProtectedBranch) BeforeInsert() { |
||||
protectBranch.CreatedUnix = time.Now().Unix() |
||||
protectBranch.UpdatedUnix = protectBranch.CreatedUnix |
||||
} |
||||
|
||||
// BeforeUpdate before protected branch update time
|
||||
func (protectBranch *ProtectedBranch) BeforeUpdate() { |
||||
protectBranch.UpdatedUnix = time.Now().Unix() |
||||
} |
||||
|
||||
// GetProtectedBranchByRepoID getting protected branch by repo ID
|
||||
func GetProtectedBranchByRepoID(RepoID int64) ([]*ProtectedBranch, error) { |
||||
protectedBranches := make([]*ProtectedBranch, 0) |
||||
return protectedBranches, x.Where("repo_id = ?", RepoID).Desc("updated_unix").Find(&protectedBranches) |
||||
} |
||||
|
||||
// GetProtectedBranchBy getting protected branch by ID/Name
|
||||
func GetProtectedBranchBy(repoID int64, BranchName string) (*ProtectedBranch, error) { |
||||
rel := &ProtectedBranch{RepoID: repoID, BranchName: strings.ToLower(BranchName)} |
||||
has, err := x.Get(rel) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if !has { |
||||
return nil, nil |
||||
} |
||||
return rel, nil |
||||
} |
||||
|
||||
// GetProtectedBranches get all protected btanches
|
||||
func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) { |
||||
protectedBranches := make([]*ProtectedBranch, 0) |
||||
return protectedBranches, x.Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID}) |
||||
} |
||||
|
||||
// AddProtectedBranch add protection to branch
|
||||
func (repo *Repository) AddProtectedBranch(branchName string, canPush bool) error { |
||||
protectedBranch := &ProtectedBranch{ |
||||
RepoID: repo.ID, |
||||
BranchName: branchName, |
||||
} |
||||
|
||||
has, err := x.Get(protectedBranch) |
||||
if err != nil { |
||||
return err |
||||
} else if has { |
||||
return nil |
||||
} |
||||
|
||||
sess := x.NewSession() |
||||
defer sessionRelease(sess) |
||||
if err = sess.Begin(); err != nil { |
||||
return err |
||||
} |
||||
protectedBranch.CanPush = canPush |
||||
if _, err = sess.InsertOne(protectedBranch); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return sess.Commit() |
||||
} |
||||
|
||||
// ChangeProtectedBranch access mode sets new access mode for the ProtectedBranch.
|
||||
func (repo *Repository) ChangeProtectedBranch(id int64, canPush bool) error { |
||||
ProtectedBranch := &ProtectedBranch{ |
||||
RepoID: repo.ID, |
||||
ID: id, |
||||
} |
||||
has, err := x.Get(ProtectedBranch) |
||||
if err != nil { |
||||
return fmt.Errorf("get ProtectedBranch: %v", err) |
||||
} else if !has { |
||||
return nil |
||||
} |
||||
|
||||
if ProtectedBranch.CanPush == canPush { |
||||
return nil |
||||
} |
||||
ProtectedBranch.CanPush = canPush |
||||
|
||||
sess := x.NewSession() |
||||
defer sessionRelease(sess) |
||||
if err = sess.Begin(); err != nil { |
||||
return err |
||||
} |
||||
|
||||
if _, err = sess.Id(ProtectedBranch.ID).AllCols().Update(ProtectedBranch); err != nil { |
||||
return fmt.Errorf("update ProtectedBranch: %v", err) |
||||
} |
||||
|
||||
return sess.Commit() |
||||
} |
||||
|
||||
// DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
|
||||
func (repo *Repository) DeleteProtectedBranch(id int64) (err error) { |
||||
protectedBranch := &ProtectedBranch{ |
||||
RepoID: repo.ID, |
||||
ID: id, |
||||
} |
||||
|
||||
sess := x.NewSession() |
||||
defer sessionRelease(sess) |
||||
if err = sess.Begin(); err != nil { |
||||
return err |
||||
} |
||||
|
||||
if affected, err := sess.Delete(protectedBranch); err != nil { |
||||
return err |
||||
} else if affected != 1 { |
||||
return fmt.Errorf("delete protected branch ID(%v) failed", id) |
||||
} |
||||
|
||||
return sess.Commit() |
||||
} |
||||
|
||||
// newProtectedBranch insert one queue
|
||||
func newProtectedBranch(protectedBranch *ProtectedBranch) error { |
||||
_, err := x.InsertOne(protectedBranch) |
||||
return err |
||||
} |
||||
|
||||
// UpdateProtectedBranch update queue
|
||||
func UpdateProtectedBranch(protectedBranch *ProtectedBranch) error { |
||||
_, err := x.Update(protectedBranch) |
||||
return err |
||||
} |
@ -0,0 +1,29 @@ |
||||
// Copyright 2016 Gitea. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package migrations |
||||
|
||||
import ( |
||||
"fmt" |
||||
"time" |
||||
|
||||
"github.com/go-xorm/xorm" |
||||
) |
||||
|
||||
func setProtectedBranchUpdatedWithCreated(x *xorm.Engine) (err error) { |
||||
type ProtectedBranch struct { |
||||
ID int64 `xorm:"pk autoincr"` |
||||
RepoID int64 `xorm:"UNIQUE(s)"` |
||||
BranchName string `xorm:"UNIQUE(s)"` |
||||
CanPush bool |
||||
Created time.Time `xorm:"-"` |
||||
CreatedUnix int64 |
||||
Updated time.Time `xorm:"-"` |
||||
UpdatedUnix int64 |
||||
} |
||||
if err = x.Sync2(new(ProtectedBranch)); err != nil { |
||||
return fmt.Errorf("Sync2: %v", err) |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,91 @@ |
||||
{{template "base/head" .}} |
||||
<div class="repository settings edit"> |
||||
{{template "repo/header" .}} |
||||
<div class="ui container"> |
||||
<div class="ui grid"> |
||||
{{template "repo/settings/navbar" .}} |
||||
<div class="twelve wide column content"> |
||||
{{template "base/alert" .}} |
||||
<h4 class="ui top attached header"> |
||||
{{.i18n.Tr "repo.default_branch"}} |
||||
</h4> |
||||
<div class="ui attached table segment"> |
||||
<form class="ui hook list form" action="{{.Link}}" method="post"> |
||||
{{.CsrfTokenHtml}} |
||||
<input type="hidden" name="action" value="default_branch"> |
||||
<div class="item"> |
||||
The default branch is considered the "base" branch in your repository, |
||||
against which all pull requests and code commits are automatically made, |
||||
unless you specify a different branch. |
||||
</div> |
||||
{{if not .Repository.IsBare}} |
||||
<div class="ui grid padded"> |
||||
<div class="eight wide column"> |
||||
<div class="ui fluid dropdown selection visible" tabindex="0"> |
||||
<select name="branch"> |
||||
<option value="{{.Repository.DefaultBranch}}">{{.Repository.DefaultBranch}}</option> |
||||
{{range .Branches}} |
||||
<option value="{{.}}">{{.}}</option> |
||||
{{end}} |
||||
</select><i class="dropdown icon"></i> |
||||
<div class="default text">{{.Repository.DefaultBranch}}</div> |
||||
<div class="menu transition hidden" tabindex="-1" style="display: block !important;"> |
||||
{{range .Branches}} |
||||
<div class="item" data-value="{{.}}">{{.}}</div> |
||||
{{end}} |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
{{end}} |
||||
<div class="item field"> |
||||
<button class="ui green button">{{$.i18n.Tr "repo.settings.update_settings"}}</button> |
||||
</div> |
||||
</form> |
||||
</div> |
||||
|
||||
<h4 class="ui top attached header"> |
||||
{{.i18n.Tr "repo.settings.protected_branch"}} |
||||
</h4> |
||||
<div class="ui attached table segment"> |
||||
<div class="ui grid padded"> |
||||
<div class="eight wide column"> |
||||
<div class="ui fluid dropdown selection visible" tabindex="0"> |
||||
<select id="protectedBranch" name="branch" data-url="{{.Repository.Link}}/settings/branches?action=protected_branch"> |
||||
{{range .LeftBranches}} |
||||
<option value="">Choose a branch...</option> |
||||
<option value="{{.}}">{{.}}</option> |
||||
{{end}} |
||||
</select><i class="dropdown icon"></i> |
||||
<div class="default text">Choose a branch...</div> |
||||
<div class="menu transition hidden" tabindex="-1" style="display: block !important;"> |
||||
{{range .LeftBranches}} |
||||
<div class="item" data-value="{{.}}">{{.}}</div> |
||||
{{end}} |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
|
||||
<div class="ui grid padded"> |
||||
<div class="sixteen wide column"> |
||||
<table class="ui single line table padded"> |
||||
<tbody> |
||||
{{range .ProtectedBranches}} |
||||
<tr> |
||||
<td><div class="ui large label">{{.BranchName}}</div></td> |
||||
<td class="right aligned"><button class="rm ui red button" data-url="{{$.Repository.Link}}/settings/branches?action=protected_branch&id={{.ID}}" data-val="{{.BranchName}}">Delete</button></td> |
||||
</tr> |
||||
{{else}} |
||||
<tr class="center aligned"><td>There is no protected branch</td></tr> |
||||
{{end}} |
||||
</tbody> |
||||
</table> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
{{template "base/footer" .}} |
Loading…
Reference in new issue