2017-05-15 13:59:26 +02:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drone/drone-cli/drone/internal"
|
|
|
|
"github.com/drone/drone-go/drone"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
var repoUpdateCmd = cli.Command{
|
2017-07-28 23:51:52 +02:00
|
|
|
Name: "update",
|
|
|
|
Usage: "update a repository",
|
|
|
|
ArgsUsage: "<repo/name>",
|
|
|
|
Action: repoUpdate,
|
2017-05-15 13:59:26 +02:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "trusted",
|
|
|
|
Usage: "repository is trusted",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
2018-09-21 01:04:58 +02:00
|
|
|
Name: "protected",
|
|
|
|
Usage: "repository is protected",
|
2017-05-15 13:59:26 +02:00
|
|
|
},
|
2020-12-11 20:12:50 +01:00
|
|
|
cli.Int64Flag{
|
|
|
|
Name: "throttle",
|
|
|
|
Usage: "repository throttle",
|
|
|
|
},
|
2017-05-15 13:59:26 +02:00
|
|
|
cli.DurationFlag{
|
|
|
|
Name: "timeout",
|
|
|
|
Usage: "repository timeout",
|
|
|
|
},
|
2017-05-23 00:49:58 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "visibility",
|
|
|
|
Usage: "repository visibility",
|
|
|
|
},
|
2019-10-02 18:00:50 +02:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "ignore-forks",
|
|
|
|
Usage: "ignore forks",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "ignore-pull-requests",
|
|
|
|
Usage: "ignore pull requests",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "auto-cancel-pull-requests",
|
|
|
|
Usage: "automatically cancel pending pull request builds",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "auto-cancel-pushes",
|
|
|
|
Usage: "automatically cancel pending push builds",
|
|
|
|
},
|
2021-08-27 11:06:54 +02:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "auto-cancel-running",
|
|
|
|
Usage: "automatically cancel running builds if newer commit pushed",
|
|
|
|
},
|
2017-05-15 13:59:26 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "repository configuration path (e.g. .drone.yml)",
|
|
|
|
},
|
2020-11-16 21:53:29 +01:00
|
|
|
cli.Int64Flag{
|
2017-08-13 01:35:15 +02:00
|
|
|
Name: "build-counter",
|
2017-08-13 01:32:11 +02:00
|
|
|
Usage: "repository starting build number",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
2017-08-13 01:35:15 +02:00
|
|
|
Name: "unsafe",
|
2017-08-13 01:32:11 +02:00
|
|
|
Usage: "validate updating the build-counter is unsafe",
|
|
|
|
},
|
2017-05-15 13:59:26 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func repoUpdate(c *cli.Context) error {
|
|
|
|
repo := c.Args().First()
|
|
|
|
owner, name, err := internal.ParseRepo(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := internal.NewClient(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-08-27 11:06:54 +02:00
|
|
|
visibility = c.String("visibility")
|
|
|
|
config = c.String("config")
|
|
|
|
timeout = c.Duration("timeout")
|
|
|
|
trusted = c.Bool("trusted")
|
|
|
|
throttle = c.Int64("throttle")
|
|
|
|
protected = c.Bool("protected")
|
|
|
|
ignoreForks = c.Bool("ignore-forks")
|
|
|
|
ignorePulls = c.Bool("ignore-pull-requests")
|
|
|
|
cancelPulls = c.Bool("auto-cancel-pull-requests")
|
|
|
|
cancelPush = c.Bool("auto-cancel-pushes")
|
|
|
|
cancelRunning = c.Bool("auto-cancel-running")
|
|
|
|
buildCounter = c.Int64("build-counter")
|
|
|
|
unsafe = c.Bool("unsafe")
|
2017-05-15 13:59:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
patch := new(drone.RepoPatch)
|
|
|
|
if c.IsSet("trusted") {
|
2018-09-21 01:04:58 +02:00
|
|
|
patch.Trusted = &trusted
|
2017-05-15 13:59:26 +02:00
|
|
|
}
|
2018-09-21 01:04:58 +02:00
|
|
|
if c.IsSet("protected") {
|
|
|
|
patch.Protected = &protected
|
2017-05-15 13:59:26 +02:00
|
|
|
}
|
2020-12-11 20:12:50 +01:00
|
|
|
if c.IsSet("throttle") {
|
|
|
|
patch.Throttle = &throttle
|
|
|
|
}
|
2017-05-15 13:59:26 +02:00
|
|
|
if c.IsSet("timeout") {
|
|
|
|
v := int64(timeout / time.Minute)
|
|
|
|
patch.Timeout = &v
|
|
|
|
}
|
|
|
|
if c.IsSet("config") {
|
|
|
|
patch.Config = &config
|
|
|
|
}
|
2019-10-02 18:00:50 +02:00
|
|
|
if c.IsSet("ignore-forks") {
|
|
|
|
patch.IgnoreForks = &ignoreForks
|
|
|
|
}
|
|
|
|
if c.IsSet("ignore-pull-requests") {
|
|
|
|
patch.IgnorePulls = &ignorePulls
|
|
|
|
}
|
|
|
|
if c.IsSet("auto-cancel-pull-requests") {
|
|
|
|
patch.CancelPulls = &cancelPulls
|
|
|
|
}
|
|
|
|
if c.IsSet("auto-cancel-pushes") {
|
|
|
|
patch.CancelPush = &cancelPush
|
|
|
|
}
|
2021-08-27 11:06:54 +02:00
|
|
|
if c.IsSet("auto-cancel-running") {
|
|
|
|
patch.CancelRunning = &cancelRunning
|
|
|
|
}
|
2017-05-23 00:49:58 +02:00
|
|
|
if c.IsSet("visibility") {
|
|
|
|
switch visibility {
|
|
|
|
case "public", "private", "internal":
|
|
|
|
patch.Visibility = &visibility
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 01:32:11 +02:00
|
|
|
if c.IsSet("build-counter") && !unsafe {
|
|
|
|
fmt.Printf("Setting the build counter is an unsafe operation that could put your repository in an inconsistent state. Please use --unsafe to proceed")
|
|
|
|
}
|
|
|
|
if c.IsSet("build-counter") && unsafe {
|
2018-09-21 01:04:58 +02:00
|
|
|
patch.Counter = &buildCounter
|
2017-08-13 01:32:11 +02:00
|
|
|
}
|
2017-05-15 13:59:26 +02:00
|
|
|
|
2018-09-21 01:04:58 +02:00
|
|
|
if _, err := client.RepoUpdate(owner, name, patch); err != nil {
|
2017-05-15 13:59:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Successfully updated repository %s/%s\n", owner, name)
|
|
|
|
return nil
|
|
|
|
}
|