Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set new default approval mode based on repo visibility #4456

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d2171ad
Set new default RequireApproval based on repo visibility
6543 Nov 25, 2024
ff31dde
rm
6543 Nov 25, 2024
5c70144
remove depricated
6543 Nov 25, 2024
f70bee7
docs
6543 Nov 25, 2024
388675a
Merge branch 'main' into adjust-approval-migration-after-backport
6543 Nov 25, 2024
0e82b40
cleanup more
6543 Nov 25, 2024
aba358b
return
6543 Nov 25, 2024
0d5b24e
Merge branch 'main' into adjust-approval-migration-after-backport
6543 Nov 25, 2024
02c3cb1
update from merged
6543 Nov 25, 2024
c5b5d0f
Merge branch 'main' into adjust-approval-migration-after-backport
6543 Nov 25, 2024
6146351
Merge branch 'main' into adjust-approval-migration-after-backport
6543 Nov 26, 2024
038d123
Merge branch 'main' into adjust-approval-migration-after-backport
6543 Nov 28, 2024
1f41f3e
jup
6543 Nov 28, 2024
627539e
Update cli/repo/repo_update.go
6543 Dec 1, 2024
46a771c
Update docs/src/pages/migrations.md
6543 Dec 1, 2024
5f04748
Update server/api/repo.go
6543 Dec 1, 2024
5b9ecae
Merge branch 'main' into adjust-approval-migration-after-backport
6543 Dec 1, 2024
916127e
fix
6543 Dec 1, 2024
ef16b30
Update server/api/repo.go
6543 Dec 3, 2024
f702a06
Merge branch 'main' into adjust-approval-migration-after-backport
6543 Dec 3, 2024
a7f37c7
frontport
6543 Dec 3, 2024
006b6f0
Merge branch 'main' into adjust-approval-migration-after-backport
6543 Dec 4, 2024
3e00061
Update server/api/repo.go
6543 Dec 4, 2024
2babf41
Apply suggestions from code review
anbraten Dec 4, 2024
846926e
Merge branch 'main' into adjust-approval-migration-after-backport
anbraten Dec 4, 2024
45d5d8d
fix formatting
anbraten Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions cli/repo/repo_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var repoUpdateCmd = &cli.Command{
Usage: "repository is trusted",
},
&cli.BoolFlag{
Name: "gated",
Usage: "repository is gated",
Name: "gated", // TODO: remove in next release
Hidden: true,
},
&cli.StringFlag{
Name: "require-approval",
Expand Down Expand Up @@ -82,7 +82,6 @@ func repoUpdate(ctx context.Context, c *cli.Command) error {
config = c.String("config")
timeout = c.Duration("timeout")
trusted = c.Bool("trusted")
gated = c.Bool("gated")
requireApproval = c.String("require-approval")
pipelineCounter = int(c.Int("pipeline-counter"))
unsafe = c.Bool("unsafe")
Expand All @@ -92,29 +91,18 @@ func repoUpdate(ctx context.Context, c *cli.Command) error {
if c.IsSet("trusted") {
patch.IsTrusted = &trusted
}
// TODO: remove isGated in next major release

// TODO: remove in next release
if c.IsSet("gated") {
if gated {
patch.RequireApproval = &woodpecker.RequireApprovalAllEvents
} else {
patch.RequireApproval = &woodpecker.RequireApprovalNone
}
return fmt.Errorf("'gated' option has been set in version 2.8, use 'require-approval' in >= 3.0")
}

if c.IsSet("require-approval") {
if mode := woodpecker.ApprovalMode(requireApproval); mode.Valid() {
patch.RequireApproval = &mode
} else {
return fmt.Errorf("update approval mode failed: '%s' is no valid mode", mode)
}

// TODO: remove isGated in next major release
if requireApproval == string(woodpecker.RequireApprovalAllEvents) {
trueBool := true
patch.IsGated = &trueBool
} else if requireApproval == string(woodpecker.RequireApprovalNone) {
falseBool := false
patch.IsGated = &falseBool
}
}
if c.IsSet("timeout") {
v := int64(timeout / time.Minute)
Expand Down
1 change: 1 addition & 0 deletions docs/src/pages/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This will be the next version of Woodpecker.

## User migrations

- `gated` has been replaced by `require-approval`
- Removed built-in environment variables:
- `CI_COMMIT_URL` use `CI_PIPELINE_FORGE_URL`
- `CI_STEP_FINISHED` as empty during execution
Expand Down
9 changes: 3 additions & 6 deletions server/api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,9 @@ func PatchRepo(c *gin.Context) {
c.String(http.StatusBadRequest, "Invalid require-approval setting")
return
}
} else if in.IsGated != nil { // TODO: remove isGated in next major release
if *in.IsGated {
repo.RequireApproval = model.RequireApprovalAllEvents
} else {
repo.RequireApproval = model.RequireApprovalForks
}
} else if in.IsGated != nil {
c.String(http.StatusBadRequest, "'gated' option has been removed, use 'require-approval' in >= 3.0")
return
}
if in.Timeout != nil {
repo.Timeout = *in.Timeout
Expand Down
22 changes: 12 additions & 10 deletions server/pipeline/gated.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,25 @@ func needsApproval(repo *model.Repo, pipeline *model.Pipeline) bool {
return false
}

switch repo.RequireApproval {
6543 marked this conversation as resolved.
Show resolved Hide resolved
// repository allows all events without approval
if repo.RequireApproval == model.RequireApprovalNone {
case model.RequireApprovalNone:
return false
}

// repository requires approval for pull requests from forks
if pipeline.Event == model.EventPull && pipeline.FromFork {
return true
}
case model.RequireApprovalForks:
if pipeline.Event == model.EventPull && pipeline.FromFork {
return true
}

// repository requires approval for pull requests
if pipeline.Event == model.EventPull && repo.RequireApproval == model.RequireApprovalPullRequests {
return true
}
case model.RequireApprovalPullRequests:
if pipeline.Event == model.EventPull {
return true
}

// repository requires approval for all events
if repo.RequireApproval == model.RequireApprovalAllEvents {
// repository requires approval for all events
case model.RequireApprovalAllEvents:
anbraten marked this conversation as resolved.
Show resolved Hide resolved
return true
}

Expand Down
22 changes: 6 additions & 16 deletions server/store/datastore/migration/019_gated_to_require_approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ var gatedToRequireApproval = xormigrate.Migration{
ID: "gated-to-require-approval",
6543 marked this conversation as resolved.
Show resolved Hide resolved
MigrateSession: func(sess *xorm.Session) (err error) {
const (
RequireApprovalNone string = "none"
RequireApprovalForks string = "forks"
RequireApprovalPullRequests string = "pull_requests"
RequireApprovalAllEvents string = "all_events"
requireApprovalOldNotGated string = "old_not_gated"
requireApprovalAllEvents string = "all_events"
)

type repos struct {
Expand All @@ -45,25 +43,17 @@ var gatedToRequireApproval = xormigrate.Migration{

// migrate gated repos
if _, err := sess.Exec(
builder.Update(builder.Eq{"require_approval": RequireApprovalAllEvents}).
builder.Update(builder.Eq{"require_approval": requireApprovalAllEvents}).
From("repos").
Where(builder.Eq{"gated": true})); err != nil {
return err
}

// migrate public repos to new default require approval
// migrate non gated repos to old_not_gated (no approval required)
if _, err := sess.Exec(
builder.Update(builder.Eq{"require_approval": RequireApprovalForks}).
builder.Update(builder.Eq{"require_approval": requireApprovalOldNotGated}).
From("repos").
Where(builder.Eq{"gated": false, "visibility": "public"})); err != nil {
return err
}

// migrate private repos to new default require approval
if _, err := sess.Exec(
builder.Update(builder.Eq{"require_approval": RequireApprovalNone}).
From("repos").
Where(builder.Eq{"gated": false}.And(builder.Neq{"visibility": "public"}))); err != nil {
Where(builder.Eq{"gated": false})); err != nil {
return err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package migration

import (
"fmt"

"src.techknowlogick.com/xormigrate"
"xorm.io/builder"
"xorm.io/xorm"
)

var setNewDefaultsForRequireApproval = xormigrate.Migration{
ID: "set-new-defaults-for-require-approval",
MigrateSession: func(sess *xorm.Session) (err error) {
const (
RequireApprovalOldNotGated string = "old_not_gated"
RequireApprovalNone string = "none"
RequireApprovalForks string = "forks"
RequireApprovalAllEvents string = "all_events"
)

type repos struct {
RequireApproval string `xorm:"require_approval"`
Visibility string `xorm:"varchar(10) 'visibility'"`
}

if err := sess.Sync(new(repos)); err != nil {
return fmt.Errorf("sync new models failed: %w", err)
}

// migrate public repos to require approval for forks
if _, err := sess.Exec(
builder.Update(builder.Eq{"require_approval": RequireApprovalForks}).
From("repos").
Where(builder.Eq{"require_approval": RequireApprovalOldNotGated, "visibility": "public"})); err != nil {
return err
}

// migrate private repos to require no approval
if _, err := sess.Exec(
builder.Update(builder.Eq{"require_approval": RequireApprovalNone}).
From("repos").
Where(builder.Eq{"require_approval": RequireApprovalOldNotGated}.And(builder.Neq{"visibility": "public"}))); err != nil {
return err
}

return nil
},
}
1 change: 1 addition & 0 deletions server/store/datastore/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var migrationTasks = []*xormigrate.Migration{
&gatedToRequireApproval,
&removeRepoNetrcOnlyTrusted,
&renameTokenFields,
&setNewDefaultsForRequireApproval,
}

var allBeans = []any{
Expand Down
1 change: 0 additions & 1 deletion woodpecker-go/woodpecker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ type (
RepoPatch struct {
Config *string `json:"config_file,omitempty"`
IsTrusted *bool `json:"trusted,omitempty"`
IsGated *bool `json:"gated,omitempty"` // TODO: remove in next major release
RequireApproval *ApprovalMode `json:"require_approval,omitempty"`
Timeout *int64 `json:"timeout,omitempty"`
Visibility *string `json:"visibility"`
Expand Down