diff --git a/cmd/agola/cmd/projectupdate.go b/cmd/agola/cmd/projectupdate.go new file mode 100644 index 0000000..db5af17 --- /dev/null +++ b/cmd/agola/cmd/projectupdate.go @@ -0,0 +1,89 @@ +// Copyright 2019 Sorint.lab +// +// 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 cmd + +import ( + "context" + + gwapitypes "agola.io/agola/services/gateway/api/types" + gwclient "agola.io/agola/services/gateway/client" + + "github.com/spf13/cobra" + errors "golang.org/x/xerrors" +) + +var cmdProjectUpdate = &cobra.Command{ + Use: "update", + Short: "update a project", + Run: func(cmd *cobra.Command, args []string) { + if err := projectUpdate(cmd, args); err != nil { + log.Fatalf("err: %v", err) + } + }, +} + +type projectUpdateOptions struct { + ref string + + name string + parentPath string + visibility string +} + +var projectUpdateOpts projectUpdateOptions + +func init() { + flags := cmdProjectUpdate.Flags() + + flags.StringVarP(&projectUpdateOpts.ref, "ref", "", "", "current project path or id") + flags.StringVarP(&projectUpdateOpts.name, "name", "n", "", "project name") + flags.StringVar(&projectUpdateOpts.parentPath, "parent", "", `parent project group path (i.e "org/org01" for root project group in org01, "user/user01/group01/subgroub01") or project group id where the project should be moved`) + flags.StringVar(&projectUpdateOpts.visibility, "visibility", "public", `project visibility (public or private)`) + + if err := cmdProjectUpdate.MarkFlagRequired("ref"); err != nil { + log.Fatal(err) + } + + cmdProject.AddCommand(cmdProjectUpdate) +} + +func projectUpdate(cmd *cobra.Command, args []string) error { + gwclient := gwclient.NewClient(gatewayURL, token) + + req := &gwapitypes.UpdateProjectRequest{} + + flags := cmd.Flags() + if flags.Changed("name") { + req.Name = &projectUpdateOpts.name + } + if flags.Changed("parent") { + req.ParentRef = &projectUpdateOpts.parentPath + } + if flags.Changed("visibility") { + if !IsValidVisibility(projectUpdateOpts.visibility) { + return errors.Errorf("invalid visibility %q", projectUpdateOpts.visibility) + } + req.Name = &projectUpdateOpts.visibility + } + + log.Infof("updating project") + project, _, err := gwclient.UpdateProject(context.TODO(), projectUpdateOpts.ref, req) + if err != nil { + return errors.Errorf("failed to update project: %w", err) + } + log.Infof("project %s update, ID: %s", project.Name, project.ID) + + return nil +} diff --git a/services/gateway/client/client.go b/services/gateway/client/client.go index 666d7d9..3399e23 100644 --- a/services/gateway/client/client.go +++ b/services/gateway/client/client.go @@ -160,6 +160,17 @@ func (c *Client) CreateProject(ctx context.Context, req *gwapitypes.CreateProjec return project, resp, err } +func (c *Client) UpdateProject(ctx context.Context, projectRef string, req *gwapitypes.UpdateProjectRequest) (*gwapitypes.ProjectResponse, *http.Response, error) { + reqj, err := json.Marshal(req) + if err != nil { + return nil, nil, err + } + + project := new(gwapitypes.ProjectResponse) + resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projects", url.PathEscape(projectRef)), nil, jsonContent, bytes.NewReader(reqj), project) + return project, resp, err +} + func (c *Client) CreateProjectGroupSecret(ctx context.Context, projectGroupRef string, req *gwapitypes.CreateSecretRequest) (*gwapitypes.SecretResponse, *http.Response, error) { reqj, err := json.Marshal(req) if err != nil {