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 (
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) {

View file

@ -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)

View file

@ -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)