From 0dbfd6e84933c260fd043e4af3fa88198d932028 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 11 Dec 2021 17:24:28 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20Move=20=E2=80=B9task=E2=80=BA=20to?= =?UTF-8?q?=20the=20root=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- src/cmd/open.go | 12 +++--------- src/cmd/root.go | 8 ++++++++ src/cmd/stats.go | 12 +++--------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/cmd/open.go b/src/cmd/open.go index 01ac752..e6eaf5d 100644 --- a/src/cmd/open.go +++ b/src/cmd/open.go @@ -11,20 +11,18 @@ import ( ) var ( - openMilestone string - openCmd = &cobra.Command{ Use: "open", Short: "Opens pull requests for review.", - PreRun: InitializeConfig, + PreRun: TaskRequiredPreRun, Run: func(cmd *cobra.Command, args []string) { client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token)) core.ExitOnError("Couldn't create gitea client", err) - milestone, _, err := client.GetMilestoneByName(config.Gitea.Owner, config.Gitea.Repository, openMilestone) + milestone, _, err := client.GetMilestoneByName(config.Gitea.Owner, config.Gitea.Repository, task) core.ExitOnError("Couldn't find the milestone", err) - fmt.Printf("Milestone found %s (deadline %s)\n", milestone.Title, milestone.Deadline) + fmt.Printf("Task %s found (deadline %s)\n", milestone.Title, milestone.Deadline) branches, _, err := client.ListRepoBranches(config.Gitea.Owner, config.Gitea.Repository, gitea.ListRepoBranchesOptions{}) core.ExitOnError("Couldn't list all the branches", err) @@ -34,10 +32,6 @@ var ( } ) -func init() { - openCmd.Flags().StringVarP(&openMilestone, "milestone", "m", "", "specifies milestone, i.e. the task") -} - func ProcessBranches(client *gitea.Client, milestone *gitea.Milestone, branches []*gitea.Branch) { for _, branch := range branches { if !strings.HasPrefix(branch.Name, milestone.Title) { diff --git a/src/cmd/root.go b/src/cmd/root.go index ff8e062..a6eee92 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -11,6 +11,7 @@ import ( var ( configFilepath string config = core.Config{} + task string rootCmd = &cobra.Command{ Use: "frag-review", @@ -25,12 +26,19 @@ func InitializeConfig(cmd *cobra.Command, args []string) { } } +func TaskRequiredPreRun(cmd *cobra.Command, args []string) { + InitializeConfig(cmd, args) + rootCmd.MarkPersistentFlagRequired("task") +} + func Execute() error { return rootCmd.Execute() } func init() { rootCmd.PersistentFlags().StringVarP(&configFilepath, "conf", "c", ".frag_review.yml", "path to the config file") + rootCmd.PersistentFlags().StringVarP(&task, "task", "t", "", "specifies task, i.e. the milestone on gitea") + rootCmd.AddCommand(patchCmd) rootCmd.AddCommand(openCmd) rootCmd.AddCommand(statsCmd) diff --git a/src/cmd/stats.go b/src/cmd/stats.go index f1eb918..96e69ab 100644 --- a/src/cmd/stats.go +++ b/src/cmd/stats.go @@ -11,20 +11,18 @@ import ( ) var ( - statsMilestone string - statsCmd = &cobra.Command{ Use: "stats", Short: "Shows stats from already graded reviews.", - PreRun: InitializeConfig, + PreRun: TaskRequiredPreRun, Run: func(cmd *cobra.Command, args []string) { client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token)) core.ExitOnError("Couldn't create gitea client", err) - milestone, _, err := client.GetMilestoneByName(config.Gitea.Owner, config.Gitea.Repository, statsMilestone) + milestone, _, err := client.GetMilestoneByName(config.Gitea.Owner, config.Gitea.Repository, task) core.ExitOnError("Couldn't find the milestone", err) - fmt.Printf("Milestone found %s (deadline %s)\n", milestone.Title, milestone.Deadline) + fmt.Printf("Task %s found (deadline %s)\n", milestone.Title, milestone.Deadline) prs, _, err := client.ListRepoPullRequests(config.Gitea.Owner, config.Gitea.Repository, gitea.ListPullRequestsOptions{ Milestone: milestone.ID, @@ -36,10 +34,6 @@ var ( } ) -func init() { - statsCmd.Flags().StringVarP(&statsMilestone, "milestone", "m", "", "specifies milestone, i.e. the task") -} - func GetStats(client *gitea.Client, milestone *gitea.Milestone, prs []*gitea.PullRequest) { graded := 0 histogram := make(map[string]int)