tools: Add gerrit-stats

Gives you juicy stats for contributions to the project.

Change-Id: I4f3e7f03cc43947675e916a8036317af4a894d12
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57883
Auto-Submit: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: David Neto <dneto@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-07-15 16:29:16 +00:00 committed by Tint LUCI CQ
parent 9e0b3cceaf
commit ff3dbc361b
3 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,198 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// gerrit-stats gathers statistics about changes made to Tint.
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"regexp"
"strings"
"time"
"github.com/andygrunwald/go-gerrit"
)
const (
yyyymmdd = "2006-01-02"
gerritURL = "https://dawn-review.googlesource.com/"
)
var (
// See https://dawn-review.googlesource.com/new-password for obtaining
// username and password for gerrit.
gerritUser = flag.String("gerrit-user", "", "gerrit authentication username")
gerritPass = flag.String("gerrit-pass", "", "gerrit authentication password")
repoFlag = flag.String("repo", "tint", "the project (tint or dawn)")
userFlag = flag.String("user", "", "user name / email")
afterFlag = flag.String("after", "", "start date")
beforeFlag = flag.String("before", "", "end date")
daysFlag = flag.Int("days", 182, "interval in days (used if --after is not specified)")
verboseFlag = flag.Bool("v", false, "verbose mode - lists all the changes")
)
func main() {
flag.Parse()
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func queryChanges(client *gerrit.Client, queryParts ...string) ([]gerrit.ChangeInfo, string, error) {
query := strings.Join(queryParts, "+")
out := []gerrit.ChangeInfo{}
for {
changes, _, err := client.Changes.QueryChanges(&gerrit.QueryChangeOptions{
QueryOptions: gerrit.QueryOptions{Query: []string{query}},
Skip: len(out),
})
if err != nil {
return nil, "", err
}
out = append(out, *changes...)
if len(*changes) == 0 || !(*changes)[len(*changes)-1].MoreChanges {
break
}
}
return out, query, nil
}
func run() error {
var after, before time.Time
var err error
user := *userFlag
if user == "" {
return fmt.Errorf("Missing required 'user' flag")
}
if *beforeFlag != "" {
before, err = time.Parse(yyyymmdd, *beforeFlag)
if err != nil {
return fmt.Errorf("Couldn't parse before date: %w", err)
}
} else {
before = time.Now()
}
if *afterFlag != "" {
after, err = time.Parse(yyyymmdd, *afterFlag)
if err != nil {
return fmt.Errorf("Couldn't parse after date: %w", err)
}
} else {
after = before.Add(-time.Hour * time.Duration(24**daysFlag))
}
client, err := gerrit.NewClient(gerritURL, nil)
if err != nil {
return fmt.Errorf("Couldn't create gerrit client: %w", err)
}
if *gerritUser == "" {
cookiesFile := os.Getenv("HOME") + "/.gitcookies"
if cookies, err := ioutil.ReadFile(cookiesFile); err == nil {
re := regexp.MustCompile(`dawn-review.googlesource.com\s+(?:FALSE|TRUE)[\s/]+(?:FALSE|TRUE)\s+[0-9]+\s+.\s+(.*)=(.*)`)
match := re.FindStringSubmatch(string(cookies))
if len(match) == 3 {
*gerritUser, *gerritPass = match[1], match[2]
}
}
}
if *gerritUser != "" {
client.Authentication.SetBasicAuth(*gerritUser, *gerritPass)
}
submitted, submittedQuery, err := queryChanges(client,
"status:merged",
"owner:"+user,
"after:"+date(after),
"before:"+date(before),
"repo:"+*repoFlag)
if err != nil {
if *gerritUser == "" {
return fmt.Errorf(`Query failed, possibly because of authentication.
See https://dawn-review.googlesource.com/new-password for obtaining a username
and password which can be provided with --gerrit-user and --gerrit-pass.
%w`, err)
}
return fmt.Errorf("Query failed: %w", err)
}
reviewed, reviewQuery, err := queryChanges(client,
"commentby:"+user,
"-owner:"+user,
"after:"+date(after),
"before:"+date(before),
"repo:"+*repoFlag)
if err != nil {
return fmt.Errorf("Query failed: %w", err)
}
ignorelist := []*regexp.Regexp{
regexp.MustCompile("Revert .*"),
}
ignore := func(s string) bool {
for _, re := range ignorelist {
if re.MatchString(s) {
return true
}
}
return false
}
insertions, deletions := 0, 0
for _, change := range submitted {
if ignore(change.Subject) {
continue
}
insertions += change.Insertions
deletions += change.Deletions
}
fmt.Printf("Between %v and %v, %v:\n", date(after), date(before), user)
fmt.Printf(" Submitted %v changes (LOC: %v+, %v-) \n", len(submitted), insertions, deletions)
fmt.Printf(" Reviewed %v changes\n", len(reviewed))
fmt.Printf("\n")
if *verboseFlag {
fmt.Printf("Submitted changes:\n")
for i, change := range submitted {
fmt.Printf("%3.1v: %6.v %v (LOC: %v+, %v-)\n", i, change.Number, change.Subject, change.Insertions, change.Deletions)
}
fmt.Printf("\n")
fmt.Printf("Reviewed changes:\n")
for i, change := range reviewed {
fmt.Printf("%3.1v: %6.v %v (LOC: %v+, %v-)\n", i, change.Number, change.Subject, change.Insertions, change.Deletions)
}
}
fmt.Printf("\n")
fmt.Printf("Submitted query: %vq/%v\n", gerritURL, url.QueryEscape(submittedQuery))
fmt.Printf("Review query: %vq/%v\n", gerritURL, url.QueryEscape(reviewQuery))
return nil
}
func today() time.Time {
return time.Now()
}
func date(t time.Time) string {
return t.Format(yyyymmdd)
}

View File

@ -3,6 +3,7 @@ module dawn.googlesource.com/tint/tools/src
go 1.16 go 1.16
require ( require (
github.com/andygrunwald/go-gerrit v0.0.0-20210709065208-9d38b0be0268
github.com/fatih/color v1.10.0 github.com/fatih/color v1.10.0
github.com/sergi/go-diff v1.2.0 github.com/sergi/go-diff v1.2.0
golang.org/x/net v0.0.0-20210614182718-04defd469f4e golang.org/x/net v0.0.0-20210614182718-04defd469f4e

View File

@ -1,8 +1,14 @@
github.com/andygrunwald/go-gerrit v0.0.0-20210709065208-9d38b0be0268 h1:7gokoTWteZhP1t2f0OzrFFXlyL8o0+b0r4ZaRV9PXOs=
github.com/andygrunwald/go-gerrit v0.0.0-20210709065208-9d38b0be0268/go.mod h1:aqcjwEnmLLSalFNYR0p2ttnEXOVVRctIzsUMHbEcruU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
@ -27,6 +33,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=