optionally disable pr and fork at database level

This commit is contained in:
Brad Rydzewski 2019-02-21 11:06:33 -08:00
parent db7f0cdffb
commit 616433b709
13 changed files with 142 additions and 35 deletions

View file

@ -32,33 +32,35 @@ const (
type (
// Repository represents a source code repository.
Repository struct {
ID int64 `json:"id"`
UID string `json:"uid"`
UserID int64 `json:"user_id"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Slug string `json:"slug"`
SCM string `json:"scm"`
HTTPURL string `json:"git_http_url"`
SSHURL string `json:"git_ssh_url"`
Link string `json:"link"`
Branch string `json:"default_branch"`
Private bool `json:"private"`
Visibility string `json:"visibility"`
Active bool `json:"active"`
Config string `json:"config_path"`
Trusted bool `json:"trusted"`
Protected bool `json:"protected"`
Timeout int64 `json:"timeout"`
Counter int64 `json:"counter"`
Synced int64 `json:"synced"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Version int64 `json:"version"`
Signer string `json:"-"`
Secret string `json:"-"`
Build *Build `json:"build,omitempty"`
Perms *Perm `json:"permissions,omitempty"`
ID int64 `json:"id"`
UID string `json:"uid"`
UserID int64 `json:"user_id"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Slug string `json:"slug"`
SCM string `json:"scm"`
HTTPURL string `json:"git_http_url"`
SSHURL string `json:"git_ssh_url"`
Link string `json:"link"`
Branch string `json:"default_branch"`
Private bool `json:"private"`
Visibility string `json:"visibility"`
Active bool `json:"active"`
Config string `json:"config_path"`
Trusted bool `json:"trusted"`
Protected bool `json:"protected"`
IgnoreForks bool `json:"ignore_forks"`
IgnorePulls bool `json:"ignore_pull_requests"`
Timeout int64 `json:"timeout"`
Counter int64 `json:"counter"`
Synced int64 `json:"synced"`
Created int64 `json:"created"`
Updated int64 `json:"updated"`
Version int64 `json:"version"`
Signer string `json:"-"`
Secret string `json:"-"`
Build *Build `json:"build,omitempty"`
Perms *Perm `json:"permissions,omitempty"`
}
// RepositoryStore defines operations for working with repositories.

2
go.sum
View file

@ -89,8 +89,6 @@ github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uP
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-retryablehttp v0.0.0-20180718195005-e651d75abec6 h1:qCv4319q2q7XKn0MQbi8p37hsJ+9Xo8e6yojA73JVxk=
github.com/hashicorp/go-retryablehttp v0.0.0-20180718195005-e651d75abec6/go.mod h1:fXcdFsQoipQa7mwORhKad5jmDCeSy/RCGzWA08PO0lM=
github.com/hashicorp/go-rootcerts v1.0.0 h1:ueI78wUjYExhCvMLow4icJnayNNFRgy0d9EGs/a1T44=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCOH9wdo=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/nomad v0.0.0-20190125003214-134391155854 h1:L7WhLZt2ory/kQWxqkMwOiBpIoa4BWoadN7yx8LHEtk=

View file

@ -28,12 +28,14 @@ import (
type (
repositoryInput struct {
Visibility *string `json:"visibility"`
Config *string `json:"config_path"`
Trusted *bool `json:"trusted"`
Protected *bool `json:"protected"`
Timeout *int64 `json:"timeout"`
Counter *int64 `json:"counter"`
Visibility *string `json:"visibility"`
Config *string `json:"config_path"`
Trusted *bool `json:"trusted"`
Protected *bool `json:"protected"`
IgnoreForks *bool `json:"ignore_forks"`
IgnorePulls *bool `json:"ignore_pull_requests"`
Timeout *int64 `json:"timeout"`
Counter *int64 `json:"counter"`
}
)
@ -78,6 +80,12 @@ func HandleUpdate(repos core.RepositoryStore) http.HandlerFunc {
if in.Protected != nil {
repo.Protected = *in.Protected
}
if in.IgnoreForks != nil {
repo.IgnoreForks = *in.IgnoreForks
}
if in.IgnorePulls != nil {
repo.IgnorePulls = *in.IgnorePulls
}
//
// system administrator only

View file

@ -188,6 +188,8 @@ const stmtInsertBase = `
,repo_timeout
,repo_trusted
,repo_protected
,repo_no_forks
,repo_no_pulls
,repo_synced
,repo_created
,repo_updated
@ -213,6 +215,8 @@ const stmtInsertBase = `
,:repo_timeout
,:repo_trusted
,:repo_protected
,:repo_no_forks
,:repo_no_pulls
,:repo_synced
,:repo_created
,:repo_updated

View file

@ -261,6 +261,8 @@ SELECT
,repo_timeout
,repo_trusted
,repo_protected
,repo_no_forks
,repo_no_pulls
,repo_synced
,repo_created
,repo_updated
@ -344,6 +346,8 @@ INSERT INTO repos (
,repo_timeout
,repo_trusted
,repo_protected
,repo_no_forks
,repo_no_pulls
,repo_synced
,repo_created
,repo_updated
@ -369,6 +373,8 @@ INSERT INTO repos (
,:repo_timeout
,:repo_trusted
,:repo_protected
,:repo_no_forks
,:repo_no_pulls
,:repo_synced
,:repo_created
,:repo_updated
@ -412,6 +418,8 @@ UPDATE repos SET
,repo_config = :repo_config
,repo_trusted = :repo_trusted
,repo_protected = :repo_protected
,repo_no_forks = :repo_no_forks
,repo_no_pulls = :repo_no_pulls
,repo_timeout = :repo_timeout
,repo_counter = :repo_counter
,repo_synced = :repo_synced

View file

@ -43,6 +43,8 @@ func ToParams(v *core.Repository) map[string]interface{} {
"repo_config": v.Config,
"repo_trusted": v.Trusted,
"repo_protected": v.Protected,
"repo_no_forks": v.IgnoreForks,
"repo_no_pulls": v.IgnorePulls,
"repo_timeout": v.Timeout,
"repo_counter": v.Counter,
"repo_synced": v.Synced,
@ -77,6 +79,8 @@ func scanRow(scanner db.Scanner, dest *core.Repository) error {
&dest.Timeout,
&dest.Trusted,
&dest.Protected,
&dest.IgnoreForks,
&dest.IgnorePulls,
&dest.Synced,
&dest.Created,
&dest.Updated,
@ -127,6 +131,8 @@ func scanRowBuild(scanner db.Scanner, dest *core.Repository) error {
&dest.Timeout,
&dest.Trusted,
&dest.Protected,
&dest.IgnoreForks,
&dest.IgnorePulls,
&dest.Synced,
&dest.Created,
&dest.Updated,

View file

@ -16,6 +16,14 @@ var migrations = []struct {
name: "create-table-repos",
stmt: createTableRepos,
},
{
name: "alter-table-repos-add-column-no-fork",
stmt: alterTableReposAddColumnNoFork,
},
{
name: "alter-table-repos-add-column-no-pulls",
stmt: alterTableReposAddColumnNoPulls,
},
{
name: "create-table-perms",
stmt: createTablePerms,
@ -246,6 +254,14 @@ CREATE TABLE IF NOT EXISTS repos (
);
`
var alterTableReposAddColumnNoFork = `
ALTER TABLE repos ADD COLUMN repo_no_forks BOOLEAN NOT NULL DEFAULT false;
`
var alterTableReposAddColumnNoPulls = `
ALTER TABLE repos ADD COLUMN repo_no_pulls BOOLEAN NOT NULL DEFAULT false;
`
//
// 003_create_table_perms.sql
//

View file

@ -29,3 +29,11 @@ CREATE TABLE IF NOT EXISTS repos (
,UNIQUE(repo_slug)
,UNIQUE(repo_uid)
);
-- name: alter-table-repos-add-column-no-fork
ALTER TABLE repos ADD COLUMN repo_no_forks BOOLEAN NOT NULL DEFAULT false;
-- name: alter-table-repos-add-column-no-pulls
ALTER TABLE repos ADD COLUMN repo_no_pulls BOOLEAN NOT NULL DEFAULT false;

View file

@ -16,6 +16,14 @@ var migrations = []struct {
name: "create-table-repos",
stmt: createTableRepos,
},
{
name: "alter-table-repos-add-column-no-fork",
stmt: alterTableReposAddColumnNoFork,
},
{
name: "alter-table-repos-add-column-no-pulls",
stmt: alterTableReposAddColumnNoPulls,
},
{
name: "create-table-perms",
stmt: createTablePerms,
@ -242,6 +250,14 @@ CREATE TABLE IF NOT EXISTS repos (
);
`
var alterTableReposAddColumnNoFork = `
ALTER TABLE repos ADD COLUMN repo_no_forks BOOLEAN NOT NULL DEFAULT false;
`
var alterTableReposAddColumnNoPulls = `
ALTER TABLE repos ADD COLUMN repo_no_pulls BOOLEAN NOT NULL DEFAULT false;
`
//
// 003_create_table_perms.sql
//

View file

@ -29,3 +29,11 @@ CREATE TABLE IF NOT EXISTS repos (
,UNIQUE(repo_slug)
,UNIQUE(repo_uid)
);
-- name: alter-table-repos-add-column-no-fork
ALTER TABLE repos ADD COLUMN repo_no_forks BOOLEAN NOT NULL DEFAULT false;
-- name: alter-table-repos-add-column-no-pulls
ALTER TABLE repos ADD COLUMN repo_no_pulls BOOLEAN NOT NULL DEFAULT false;

View file

@ -16,6 +16,14 @@ var migrations = []struct {
name: "create-table-repos",
stmt: createTableRepos,
},
{
name: "alter-table-repos-add-column-no-fork",
stmt: alterTableReposAddColumnNoFork,
},
{
name: "alter-table-repos-add-column-no-pulls",
stmt: alterTableReposAddColumnNoPulls,
},
{
name: "create-table-perms",
stmt: createTablePerms,
@ -246,6 +254,14 @@ CREATE TABLE IF NOT EXISTS repos (
);
`
var alterTableReposAddColumnNoFork = `
ALTER TABLE repos ADD COLUMN repo_no_forks BOOLEAN NOT NULL DEFAULT 0;
`
var alterTableReposAddColumnNoPulls = `
ALTER TABLE repos ADD COLUMN repo_no_pulls BOOLEAN NOT NULL DEFAULT 0;
`
//
// 003_create_table_perms.sql
//

View file

@ -29,3 +29,11 @@ CREATE TABLE IF NOT EXISTS repos (
,UNIQUE(repo_slug)
,UNIQUE(repo_uid)
);
-- name: alter-table-repos-add-column-no-fork
ALTER TABLE repos ADD COLUMN repo_no_forks BOOLEAN NOT NULL DEFAULT 0;
-- name: alter-table-repos-add-column-no-pulls
ALTER TABLE repos ADD COLUMN repo_no_pulls BOOLEAN NOT NULL DEFAULT 0;

View file

@ -7,6 +7,7 @@ package trigger
import (
"context"
"runtime/debug"
"strings"
"time"
"github.com/drone/drone-yaml/yaml"
@ -77,6 +78,14 @@ func (t *triggerer) Trigger(ctx context.Context, repo *core.Repository, base *co
logger.Infoln("trigger: skipping hook. found skip directive")
return nil, nil
}
if repo.IgnorePulls && base.Event == core.EventPullRequest {
logger.Infoln("trigger: skipping hook. project ignores pull requests")
return nil, nil
}
if repo.IgnoreForks && !strings.EqualFold(base.Fork, repo.Slug) {
logger.Infoln("trigger: skipping hook. project ignores forks")
return nil, nil
}
user, err := t.users.Find(ctx, repo.UserID)
if err != nil {