From 918071a0bedb1321feafbbe7a9dcc191385b9bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Engin=20A=C3=A7=C4=B1kg=C3=B6z?= Date: Wed, 5 Jul 2023 16:31:22 +0300 Subject: [PATCH 1/3] Create team via kdt --- client/team.go | 38 +++++++++++++++++++++++++++++++ cmd/createTeam.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 client/team.go create mode 100644 cmd/createTeam.go diff --git a/client/team.go b/client/team.go new file mode 100644 index 0000000..3800308 --- /dev/null +++ b/client/team.go @@ -0,0 +1,38 @@ +/* +Copyright © 2023 Kondukto + +*/ + +package client + +import "net/http" + +type Team struct { + Name string `json:"name"` + IssueResponsible IssueResponsible `json:"issue_responsible"` +} + +type IssueResponsible struct { + Username string `json:"username"` +} + +func (c *Client) CreateTeam(teamName, responsible string) error { + var team = Team{ + Name: teamName, + IssueResponsible: IssueResponsible{ + Username: responsible, + }, + } + + req, err := c.newRequest(http.MethodPost, "/api/v3/teams", team) + if err != nil { + return err + } + + _, err = c.do(req, nil) + if err != nil { + return err + } + + return nil +} diff --git a/cmd/createTeam.go b/cmd/createTeam.go new file mode 100644 index 0000000..948520c --- /dev/null +++ b/cmd/createTeam.go @@ -0,0 +1,57 @@ +package cmd + +import ( + "fmt" + + "github.com/kondukto-io/kdt/client" + + "github.com/spf13/cobra" +) + +// createTeamCmd represents the create project command +var createTeamCmd = &cobra.Command{ + Use: "team", + Short: "creates a new team on Kondukto", + Run: createTeamRootCommand, +} + +func init() { + createCmd.AddCommand(createTeamCmd) + + createTeamCmd.Flags().StringP("name", "n", "", "team name") + createTeamCmd.Flags().StringP("responsible", "r", "", "responsible user name") +} + +func createTeamRootCommand(cmd *cobra.Command, _ []string) { + c, err := client.New() + if err != nil { + qwe(ExitCodeError, err, "could not initialize Kondukto client") + } + + teamName, err := cmd.Flags().GetString("name") + if err != nil { + qwe(ExitCodeError, err, "failed to parse the name flag") + } + + responsible, err := cmd.Flags().GetString("responsible") + if err != nil { + qwe(ExitCodeError, err, "failed to parse the responsible flag") + } + + if teamName == "" { + qwm(ExitCodeError, "team name is required") + } + + if err := c.CreateTeam(teamName, responsible); err != nil { + qwe(ExitCodeError, err, "failed to create team") + } + + var successfulMessage string + if responsible != "" { + successfulMessage = fmt.Sprintf("team [%s] created successfuly with responsible [%s]", teamName, responsible) + } else { + successfulMessage = fmt.Sprintf("team [%s] created successfuly with responsible [admin]", teamName) + } + + qwm(ExitCodeSuccess, successfulMessage) +} From f2ab8045f63396410373612323abd9f207b96441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Engin=20A=C3=A7=C4=B1kg=C3=B6z?= Date: Wed, 5 Jul 2023 16:33:59 +0300 Subject: [PATCH 2/3] Add copyright --- cmd/createTeam.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/createTeam.go b/cmd/createTeam.go index 948520c..4d55a73 100644 --- a/cmd/createTeam.go +++ b/cmd/createTeam.go @@ -1,3 +1,8 @@ +/* +Copyright © 2023 Kondukto + +*/ + package cmd import ( From 4952462218e6e0640e814086b99ca701a84eb934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Engin=20A=C3=A7=C4=B1kg=C3=B6z?= Date: Tue, 11 Jul 2023 10:03:41 +0300 Subject: [PATCH 3/3] Improve the code --- cmd/createTeam.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/createTeam.go b/cmd/createTeam.go index 4d55a73..baed175 100644 --- a/cmd/createTeam.go +++ b/cmd/createTeam.go @@ -38,15 +38,16 @@ func createTeamRootCommand(cmd *cobra.Command, _ []string) { qwe(ExitCodeError, err, "failed to parse the name flag") } + if teamName == "" { + qwm(ExitCodeError, "team name is required") + } + + // Responsible is optional responsible, err := cmd.Flags().GetString("responsible") if err != nil { qwe(ExitCodeError, err, "failed to parse the responsible flag") } - if teamName == "" { - qwm(ExitCodeError, "team name is required") - } - if err := c.CreateTeam(teamName, responsible); err != nil { qwe(ExitCodeError, err, "failed to create team") }