re-use gated logic

This commit is contained in:
Brad Rydzewski
2017-05-05 19:13:40 +02:00
parent 4569b60f09
commit 4aac0bc4d6
8 changed files with 8 additions and 18 deletions

View File

@@ -26,7 +26,6 @@ type Repo struct {
IsTrusted bool `json:"trusted" meddler:"repo_trusted"` IsTrusted bool `json:"trusted" meddler:"repo_trusted"`
IsStarred bool `json:"starred,omitempty" meddler:"-"` IsStarred bool `json:"starred,omitempty" meddler:"-"`
IsGated bool `json:"gated" meddler:"repo_gated"` IsGated bool `json:"gated" meddler:"repo_gated"`
IsGatedConf bool `json:"gated_conf" meddler:"repo_gated_conf"`
AllowPull bool `json:"allow_pr" meddler:"repo_allow_pr"` AllowPull bool `json:"allow_pr" meddler:"repo_allow_pr"`
AllowPush bool `json:"allow_push" meddler:"repo_allow_push"` AllowPush bool `json:"allow_push" meddler:"repo_allow_push"`
AllowDeploy bool `json:"allow_deploys" meddler:"repo_allow_deploys"` AllowDeploy bool `json:"allow_deploys" meddler:"repo_allow_deploys"`

View File

@@ -1,7 +1,7 @@
package model package model
type SenderService interface { type SenderService interface {
SenderAllowed(*User, *Repo, *Build) (bool, error) SenderAllowed(*User, *Repo, *Build, *Config) (bool, error)
SenderCreate(*Repo, *Sender) error SenderCreate(*Repo, *Sender) error
SenderUpdate(*Repo, *Sender) error SenderUpdate(*Repo, *Sender) error
SenderDelete(*Repo, string) error SenderDelete(*Repo, string) error

View File

@@ -13,8 +13,8 @@ func New(store model.SenderStore) model.SenderService {
return &builtin{store} return &builtin{store}
} }
func (b *builtin) SenderAllowed(user *model.User, repo *model.Repo, build *model.Build) (bool, error) { func (b *builtin) SenderAllowed(user *model.User, repo *model.Repo, build *model.Build, conf *model.Config) (bool, error) {
if repo.IsPrivate == false && build.Event == model.EventPull && build.Sender != user.Login { if !conf.Approved {
sender, err := b.store.SenderFind(repo, build.Sender) sender, err := b.store.SenderFind(repo, build.Sender)
if err != nil || sender.Block { if err != nil || sender.Block {
return false, nil return false, nil

View File

@@ -16,7 +16,7 @@ func NewRemote(endpoint string) model.SenderService {
return &plugin{endpoint} return &plugin{endpoint}
} }
func (p *plugin) SenderAllowed(user *model.User, repo *model.Repo, build *model.Build) (bool, error) { func (p *plugin) SenderAllowed(user *model.User, repo *model.Repo, build *model.Build, conf *model.Config) (bool, error) {
path := fmt.Sprintf("%s/senders/%s/%s/%s/verify", p.endpoint, repo.Owner, repo.Name, build.Sender) path := fmt.Sprintf("%s/senders/%s/%s/%s/verify", p.endpoint, repo.Owner, repo.Name, build.Sender)
err := internal.Send("POST", path, build, nil) err := internal.Send("POST", path, build, nil)
if err != nil { if err != nil {

View File

@@ -147,7 +147,7 @@ func PostHook(c *gin.Context) {
Hash: sha, Hash: sha,
Approved: false, Approved: false,
} }
if user.Login == repo.Owner || build.Event != model.EventPull { if user.Login == repo.Owner || build.Event != model.EventPull || repo.IsGated == false {
conf.Approved = true conf.Approved = true
} }
err = Config.Storage.Config.ConfigInsert(conf) err = Config.Storage.Config.ConfigInsert(conf)
@@ -158,7 +158,7 @@ func PostHook(c *gin.Context) {
} }
} }
if !conf.Approved { if !conf.Approved {
if user.Login == repo.Owner || build.Event != model.EventPull || !repo.IsGatedConf { if user.Login == repo.Owner || build.Event != model.EventPull || repo.IsGated == false {
conf.Approved = true conf.Approved = true
Config.Storage.Config.ConfigUpdate(conf) Config.Storage.Config.ConfigUpdate(conf)
} }
@@ -195,8 +195,8 @@ func PostHook(c *gin.Context) {
build.Verified = true build.Verified = true
build.Status = model.StatusPending build.Status = model.StatusPending
if repo.IsGated || repo.IsGatedConf { if repo.IsGated {
allowed, _ := Config.Services.Senders.SenderAllowed(user, repo, build) allowed, _ := Config.Services.Senders.SenderAllowed(user, repo, build, conf)
if !allowed { if !allowed {
build.Status = model.StatusBlocked build.Status = model.StatusBlocked
} }

View File

@@ -13,9 +13,6 @@ CREATE TABLE config (
ALTER TABLE builds ADD COLUMN build_config_id INTEGER; ALTER TABLE builds ADD COLUMN build_config_id INTEGER;
UPDATE builds set build_config_id = 0; UPDATE builds set build_config_id = 0;
ALTER TABLE repos ADD COLUMN repo_gated_conf BOOLEAN;
UPDATE repos SET repo_gated_conf = 0;
-- +migrate Down -- +migrate Down
DROP TABLE config; DROP TABLE config;

View File

@@ -13,9 +13,6 @@ CREATE TABLE config (
ALTER TABLE builds ADD COLUMN build_config_id INTEGER; ALTER TABLE builds ADD COLUMN build_config_id INTEGER;
UPDATE builds set build_config_id = 0; UPDATE builds set build_config_id = 0;
ALTER TABLE repos ADD COLUMN repo_gated_conf BOOLEAN;
UPDATE repos SET repo_gated_conf = 0;
-- +migrate Down -- +migrate Down
DROP TABLE config; DROP TABLE config;

View File

@@ -13,9 +13,6 @@ CREATE TABLE config (
ALTER TABLE builds ADD COLUMN build_config_id INTEGER; ALTER TABLE builds ADD COLUMN build_config_id INTEGER;
UPDATE builds set build_config_id = 0; UPDATE builds set build_config_id = 0;
ALTER TABLE repos ADD COLUMN repo_gated_conf BOOLEAN;
UPDATE repos SET repo_gated_conf = 0;
-- +migrate Down -- +migrate Down
DROP TABLE config; DROP TABLE config;