Merge pull request 'refactor' (#10) from refactor into main

Reviewed-on: mfocko/frag-review#10
This commit is contained in:
Matej Focko 2021-12-11 17:26:35 +01:00
commit 171411f13a
3 changed files with 40 additions and 27 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

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"sort"
"strings" "strings"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
@ -11,61 +12,71 @@ 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,
}) })
core.ExitOnError("Couldn't list the pull requests", err) core.ExitOnError("Couldn't list the pull requests", err)
GetStats(client, milestone, prs) GetStats(prs)
}, },
} }
) )
func init() { func ParseHistogram(prs []*gitea.PullRequest) map[string]int {
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) histogram := make(map[string]int)
for _, pr := range prs { for _, pr := range prs {
for _, label := range pr.Labels { for _, label := range pr.Labels {
if strings.HasPrefix(label.Name, "grade:") { if strings.HasPrefix(label.Name, "grade:") {
grade := strings.Split(label.Name, ":")[1] grade := strings.Split(label.Name, ":")[1]
histogram[grade] += 1 histogram[grade] += 1
graded += 1
break break
} }
} }
} }
for grade, count := range histogram { return histogram
}
func SortedKeys(histogram map[string]int) []string {
grades := make([]string, 0, len(histogram))
for grade := range histogram {
grades = append(grades, grade)
}
sort.Strings(grades)
return grades
}
func GetStats(prs []*gitea.PullRequest) {
histogram := ParseHistogram(prs)
grades := SortedKeys(histogram)
graded := 0
for _, grade := range grades {
count := histogram[grade]
if (count == 0) { if (count == 0) {
continue continue
} }
fmt.Printf("%s: %d\n", grade, count) graded += count
fmt.Printf(" %s: %2d\n", grade, count)
} }
if (len(prs) - graded > 0) { if (len(prs) - graded > 0) {
fmt.Printf("ungraded: %d\n", len(prs) - graded) fmt.Println("--------")
fmt.Printf("TODO: %2d\n", len(prs) - graded)
} }
} }