From 5dfc61ca7ec4916f4151996ca6be17a8ff6c7a56 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 12 Apr 2017 14:12:21 +0200 Subject: [PATCH] ability to update repo settings from cli --- client/client.go | 2 +- client/client_impl.go | 4 +-- drone/repo.go | 1 + drone/repo_info.go | 3 ++ drone/repo_update.go | 74 +++++++++++++++++++++++++++++++++++++++++++ model/repo.go | 12 +++++++ server/repo.go | 14 +++----- 7 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 drone/repo_update.go diff --git a/client/client.go b/client/client.go index 712204b70..b1b762fd4 100644 --- a/client/client.go +++ b/client/client.go @@ -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) diff --git a/client/client_impl.go b/client/client_impl.go index 9acf7cb08..f5d675dbc 100644 --- a/client/client_impl.go +++ b/client/client_impl.go @@ -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 } diff --git a/drone/repo.go b/drone/repo.go index d3ded8b2f..c00a1f4d0 100644 --- a/drone/repo.go +++ b/drone/repo.go @@ -9,6 +9,7 @@ var repoCmd = cli.Command{ repoListCmd, repoInfoCmd, repoAddCmd, + repoUpdateCmd, repoRemoveCmd, repoChownCmd, }, diff --git a/drone/repo_info.go b/drone/repo_info.go index f96ee69b7..9cb9bc922 100644 --- a/drone/repo_info.go +++ b/drone/repo_info.go @@ -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 }} ` diff --git a/drone/repo_update.go b/drone/repo_update.go new file mode 100644 index 000000000..229e8e5c8 --- /dev/null +++ b/drone/repo_update.go @@ -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 +} diff --git a/model/repo.go b/model/repo.go index 9d4e1fad4..c650e5dd9 100644 --- a/model/repo.go +++ b/model/repo.go @@ -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"` +} diff --git a/server/repo.go b/server/repo.go index 4124f9b04..6150459b6 100644 --- a/server/repo.go +++ b/server/repo.go @@ -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 {