ability to update repo settings from cli

This commit is contained in:
Brad Rydzewski 2017-04-12 14:12:21 +02:00
parent 1d46344bf6
commit 5dfc61ca7e
7 changed files with 98 additions and 12 deletions

View file

@ -37,7 +37,7 @@ type Client interface {
RepoPost(string, string) (*model.Repo, error)
// RepoPatch updates a repository.
RepoPatch(*model.Repo) (*model.Repo, error)
RepoPatch(string, string, *model.RepoPatch) (*model.Repo, error)
// RepoChown updates a repository owner.
RepoChown(string, string) (*model.Repo, error)

View file

@ -174,9 +174,9 @@ func (c *client) RepoChown(owner string, name string) (*model.Repo, error) {
}
// RepoPatch updates a repository.
func (c *client) RepoPatch(in *model.Repo) (*model.Repo, error) {
func (c *client) RepoPatch(owner, name string, in *model.RepoPatch) (*model.Repo, error) {
out := new(model.Repo)
uri := fmt.Sprintf(pathRepo, c.base, in.Owner, in.Name)
uri := fmt.Sprintf(pathRepo, c.base, owner, name)
err := c.patch(uri, in, out)
return out, err
}

View file

@ -9,6 +9,7 @@ var repoCmd = cli.Command{
repoListCmd,
repoInfoCmd,
repoAddCmd,
repoUpdateCmd,
repoRemoveCmd,
repoChownCmd,
},

View file

@ -48,6 +48,9 @@ func repoInfo(c *cli.Context) error {
var tmplRepoInfo = `Owner: {{ .Owner }}
Repo: {{ .Name }}
Type: {{ .Kind }}
Config: {{ .Config }}
Private: {{ .IsPrivate }}
Trusted: {{ .IsTrusted }}
Gated: {{ .IsGated }}
Remote: {{ .Clone }}
`

74
drone/repo_update.go Normal file
View file

@ -0,0 +1,74 @@
package main
import (
"fmt"
"time"
"github.com/drone/drone/model"
"github.com/urfave/cli"
)
var repoUpdateCmd = cli.Command{
Name: "update",
Usage: "update a repository",
Action: repoUpdate,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "trusted",
Usage: "repository is trusted",
},
cli.BoolFlag{
Name: "gated",
Usage: "repository is gated",
},
cli.DurationFlag{
Name: "timeout",
Usage: "repository timeout",
},
cli.StringFlag{
Name: "config",
Usage: "repository configuration path (e.g. .drone.yml)",
},
},
}
func repoUpdate(c *cli.Context) error {
repo := c.Args().First()
owner, name, err := parseRepo(repo)
if err != nil {
return err
}
client, err := newClient(c)
if err != nil {
return err
}
var (
config = c.String("config")
timeout = c.Duration("timeout")
trusted = c.Bool("trusted")
gated = c.Bool("trusted")
)
patch := new(model.RepoPatch)
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 _, err := client.RepoPatch(owner, name, patch); err != nil {
return err
}
fmt.Printf("Successfully updated repository %s/%s\n", owner, name)
return nil
}

View file

@ -33,3 +33,15 @@ type Repo struct {
Config string `json:"config_file" meddler:"repo_config_path"`
Hash string `json:"-" meddler:"repo_hash"`
}
// RepoPatch represents a repository patch object.
type RepoPatch struct {
Config *string `json:"config_file,omitempty"`
IsTrusted *bool `json:"trusted,omitempty"`
IsGated *bool `json:"gated,omitempty"`
Timeout *int64 `json:"timeout,omitempty"`
AllowPull *bool `json:"allow_pr,omitempty"`
AllowPush *bool `json:"allow_push,omitempty"`
AllowDeploy *bool `json:"allow_deploy,omitempty"`
AllowTag *bool `json:"allow_tag,omitempty"`
}

View file

@ -9,6 +9,7 @@ import (
"github.com/gorilla/securecookie"
"github.com/drone/drone/cache"
"github.com/drone/drone/model"
"github.com/drone/drone/remote"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/shared/httputil"
@ -96,15 +97,7 @@ func PatchRepo(c *gin.Context) {
repo := session.Repo(c)
user := session.User(c)
in := &struct {
IsTrusted *bool `json:"trusted,omitempty"`
IsGated *bool `json:"gated,omitempty"`
Timeout *int64 `json:"timeout,omitempty"`
AllowPull *bool `json:"allow_pr,omitempty"`
AllowPush *bool `json:"allow_push,omitempty"`
AllowDeploy *bool `json:"allow_deploy,omitempty"`
AllowTag *bool `json:"allow_tag,omitempty"`
}{}
in := new(model.RepoPatch)
if err := c.Bind(in); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
@ -136,6 +129,9 @@ func PatchRepo(c *gin.Context) {
if in.Timeout != nil {
repo.Timeout = *in.Timeout
}
if in.Config != nil {
repo.Config = *in.Config
}
err := store.UpdateRepo(c, repo)
if err != nil {