2019-04-06 19:32:14 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/common"
|
2021-09-21 14:36:41 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cli/internal"
|
2021-10-02 22:27:43 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/woodpecker-go/woodpecker"
|
2019-04-06 19:32:14 +00:00
|
|
|
)
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
var repoUpdateCmd = &cli.Command{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "update",
|
|
|
|
Usage: "update a repository",
|
|
|
|
ArgsUsage: "<repo/name>",
|
|
|
|
Action: repoUpdate,
|
2021-10-27 19:03:14 +00:00
|
|
|
Flags: append(common.GlobalFlags,
|
|
|
|
&cli.BoolFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "trusted",
|
|
|
|
Usage: "repository is trusted",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.BoolFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "gated",
|
|
|
|
Usage: "repository is gated",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.DurationFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "timeout",
|
|
|
|
Usage: "repository timeout",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "visibility",
|
|
|
|
Usage: "repository visibility",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.StringFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "config",
|
2021-10-02 22:27:43 +00:00
|
|
|
Usage: "repository configuration path (e.g. .woodpecker.yml)",
|
2019-04-06 19:32:14 +00:00
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.IntFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "build-counter",
|
|
|
|
Usage: "repository starting build number",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
&cli.BoolFlag{
|
2019-04-06 19:32:14 +00:00
|
|
|
Name: "unsafe",
|
|
|
|
Usage: "validate updating the build-counter is unsafe",
|
|
|
|
},
|
2021-10-27 19:03:14 +00:00
|
|
|
),
|
2019-04-06 19:32:14 +00: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 (
|
|
|
|
visibility = c.String("visibility")
|
|
|
|
config = c.String("config")
|
|
|
|
timeout = c.Duration("timeout")
|
|
|
|
trusted = c.Bool("trusted")
|
|
|
|
gated = c.Bool("gated")
|
|
|
|
buildCounter = c.Int("build-counter")
|
|
|
|
unsafe = c.Bool("unsafe")
|
|
|
|
)
|
|
|
|
|
2021-10-02 22:27:43 +00:00
|
|
|
patch := new(woodpecker.RepoPatch)
|
2019-04-06 19:32:14 +00:00
|
|
|
if c.IsSet("trusted") {
|
|
|
|
patch.IsTrusted = &trusted
|
|
|
|
}
|
|
|
|
if c.IsSet("gated") {
|
|
|
|
patch.IsGated = &gated
|
|
|
|
}
|
|
|
|
if c.IsSet("timeout") {
|
|
|
|
v := int64(timeout / time.Minute)
|
|
|
|
patch.Timeout = &v
|
|
|
|
}
|
|
|
|
if c.IsSet("config") {
|
|
|
|
patch.Config = &config
|
|
|
|
}
|
|
|
|
if c.IsSet("visibility") {
|
|
|
|
switch visibility {
|
|
|
|
case "public", "private", "internal":
|
|
|
|
patch.Visibility = &visibility
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
patch.BuildCounter = &buildCounter
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := client.RepoPatch(owner, name, patch); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("Successfully updated repository %s/%s\n", owner, name)
|
|
|
|
return nil
|
|
|
|
}
|