refactor: Move ‹task› to the root command

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-11 17:24:28 +01:00
parent c8631ca324
commit 0dbfd6e849
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B
3 changed files with 14 additions and 18 deletions

View file

@ -11,20 +11,18 @@ import (
) )
var ( var (
openMilestone string
openCmd = &cobra.Command{ openCmd = &cobra.Command{
Use: "open", Use: "open",
Short: "Opens pull requests for review.", Short: "Opens pull requests for review.",
PreRun: InitializeConfig, PreRun: TaskRequiredPreRun,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token)) client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token))
core.ExitOnError("Couldn't create gitea client", err) 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) 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{}) branches, _, err := client.ListRepoBranches(config.Gitea.Owner, config.Gitea.Repository, gitea.ListRepoBranchesOptions{})
core.ExitOnError("Couldn't list all the branches", err) 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) { func ProcessBranches(client *gitea.Client, milestone *gitea.Milestone, branches []*gitea.Branch) {
for _, branch := range branches { for _, branch := range branches {
if !strings.HasPrefix(branch.Name, milestone.Title) { if !strings.HasPrefix(branch.Name, milestone.Title) {

View file

@ -11,6 +11,7 @@ import (
var ( var (
configFilepath string configFilepath string
config = core.Config{} config = core.Config{}
task string
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "frag-review", 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 { func Execute() error {
return rootCmd.Execute() return rootCmd.Execute()
} }
func init() { func init() {
rootCmd.PersistentFlags().StringVarP(&configFilepath, "conf", "c", ".frag_review.yml", "path to the config file") 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(patchCmd)
rootCmd.AddCommand(openCmd) rootCmd.AddCommand(openCmd)
rootCmd.AddCommand(statsCmd) rootCmd.AddCommand(statsCmd)

View file

@ -11,20 +11,18 @@ import (
) )
var ( var (
statsMilestone string
statsCmd = &cobra.Command{ statsCmd = &cobra.Command{
Use: "stats", Use: "stats",
Short: "Shows stats from already graded reviews.", Short: "Shows stats from already graded reviews.",
PreRun: InitializeConfig, PreRun: TaskRequiredPreRun,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token)) client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token))
core.ExitOnError("Couldn't create gitea client", err) 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) 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{ prs, _, err := client.ListRepoPullRequests(config.Gitea.Owner, config.Gitea.Repository, gitea.ListPullRequestsOptions{
Milestone: milestone.ID, 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) { func GetStats(client *gitea.Client, milestone *gitea.Milestone, prs []*gitea.PullRequest) {
graded := 0 graded := 0
histogram := make(map[string]int) histogram := make(map[string]int)