feat: Implement statistics

Fixes #8

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-11 16:43:49 +01:00
parent df2660698d
commit a24d7bdc0c
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B
4 changed files with 77 additions and 1 deletions

View file

@ -33,4 +33,5 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&configFilepath, "conf", "c", ".frag_review.yml", "path to the config file")
rootCmd.AddCommand(patchCmd)
rootCmd.AddCommand(openCmd)
rootCmd.AddCommand(statsCmd)
}

71
src/cmd/stats.go Normal file
View file

@ -0,0 +1,71 @@
package cmd
import (
"fmt"
"strings"
"code.gitea.io/sdk/gitea"
"github.com/spf13/cobra"
"git.mfocko.xyz/mfocko/frag-review/core"
)
var (
statsMilestone string
statsCmd = &cobra.Command{
Use: "stats",
Short: "Shows stats from already graded reviews.",
PreRun: InitializeConfig,
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)
core.ExitOnError("Couldn't find the milestone", err)
fmt.Printf("Milestone found %s (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)
},
}
)
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)
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 {
if (count == 0) {
continue
}
fmt.Printf("%s: %d\n", grade, count)
}
if (len(prs) - graded > 0) {
fmt.Printf("ungraded: %d\n", len(prs) - graded)
}
}

View file

@ -3,7 +3,7 @@ module git.mfocko.xyz/mfocko/frag-review
go 1.16
require (
code.gitea.io/sdk/gitea v0.14.0 // indirect
code.gitea.io/sdk/gitea v0.15.0 // indirect
github.com/bbrks/wrap/v2 v2.5.0 // indirect
github.com/spf13/cobra v1.2.1
gopkg.in/yaml.v2 v2.4.0

View file

@ -36,8 +36,11 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo
cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk=
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE=
code.gitea.io/sdk/gitea v0.14.0 h1:m4J352I3p9+bmJUfS+g0odeQzBY/5OXP91Gv6D4fnJ0=
code.gitea.io/sdk/gitea v0.14.0/go.mod h1:89WiyOX1KEcvjP66sRHdu0RafojGo60bT9UqW17VbWs=
code.gitea.io/sdk/gitea v0.15.0 h1:tsNhxDM/2N1Ohv1Xq5UWrht/esg0WmtRj4wsHVHriTg=
code.gitea.io/sdk/gitea v0.15.0/go.mod h1:klY2LVI3s3NChzIk/MzMn7G1FHrfU7qd63iSMVoHRBA=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
@ -427,6 +430,7 @@ golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapK
golang.org/x/tools v0.0.0-20200227222343-706bc42d1f0d/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
golang.org/x/tools v0.0.0-20200312045724-11d5b4c81c7d/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw=
golang.org/x/tools v0.0.0-20200325010219-a49f79bcc224/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=