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

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