Skip to content

Commit

Permalink
internal/devtools/cmd/labelhist: show label history
Browse files Browse the repository at this point in the history
Add a CLI tool for displaying the the labels added to and removed
from an issue.

This will be useful to evaluate labels added by gaby.

Example:

> go run ./internal/devtools/cmd/labelhist 69000
69000:
  2024-08-21T19:56:28Z cherrymui  +NeedsInvestigation
  2024-08-22T19:03:13Z dmitshur   -NeedsInvestigation
  2024-08-22T19:03:13Z dmitshur   +NeedsFix
  2024-08-22T19:03:30Z dmitshur   +Testing

For #64.

Change-Id: Ide5f429df92bfebd7978a8dfbb46b93708a6ab3d
Reviewed-on: https://go-review.googlesource.com/c/oscar/+/635876
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Tatiana Bradley <[email protected]>
  • Loading branch information
jba committed Dec 13, 2024
1 parent 89c5dcb commit 43c6ca6
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 6 deletions.
101 changes: 101 additions & 0 deletions internal/devtools/cmd/labelhist/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/*
Labelhist displays the events of a GitHub issue that affect its labels.
Usage:
labelhist issues...
Each argument can be a single issue number or a range of numbers "from-to".
*/
package main

import (
"context"
"flag"
"fmt"
"log"
"log/slog"
"os"
"strconv"
"strings"

"golang.org/x/oscar/internal/gcp/firestore"
"golang.org/x/oscar/internal/github"
)

var project = flag.String("project", "golang/go", "GitHub project")

func usage() {
fmt.Fprintf(os.Stderr, "usage: labelhist issues...\n")
flag.PrintDefaults()
os.Exit(2)
}

func main() {
log.SetFlags(0)
log.SetPrefix("labelhist: ")
flag.Usage = usage
flag.Parse()
if err := run(context.Background()); err != nil {
log.Fatal(err)
}
}

func run(ctx context.Context) error {
var ranges []Range
for _, arg := range flag.Args() {
r, err := parseIssueArg(arg)
if err != nil {
return err
}
ranges = append(ranges, r)
}
lg := slog.New(slog.NewTextHandler(os.Stderr, nil))
db, err := firestore.NewDB(ctx, lg, "oscar-go-1", "prod")
if err != nil {
return err
}

for _, r := range ranges {
for ev := range github.Events(db, *project, r.min, r.max) {
switch ev.API {
case "/issues":
fmt.Printf("%d:\n", ev.Issue)
case "/issues/events":
ie := ev.Typed.(*github.IssueEvent)
if ie.Event == "labeled" || ie.Event == "unlabeled" {
c := '+'
if ie.Event == "unlabeled" {
c = '-'
}
fmt.Printf(" %s %-10s %c%s\n", ie.CreatedAt, ie.Actor.Login, c, ie.Label.Name)
}
}
}
}
return nil
}

type Range struct {
min, max int64
}

func parseIssueArg(s string) (Range, error) {
sfrom, sto, found := strings.Cut(s, "-")
from, err := strconv.ParseInt(sfrom, 10, 64)
if err != nil {
return Range{}, err
}
if !found {
return Range{from, from}, nil
}
to, err := strconv.ParseInt(sto, 10, 64)
if err != nil {
return Range{}, err
}
return Range{from, to}, nil
}
14 changes: 8 additions & 6 deletions internal/github/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func LookupIssue(db storage.DB, project string, issue int64) (*Issue, error) {
// only consulting the database (not actual GitHub).
func LookupIssues(db storage.DB, project string, issueMin, issueMax int64) iter.Seq[*Issue] {
return func(yield func(*Issue) bool) {
for e := range events(db, project, issueMin, issueMax) {
for e := range Events(db, project, issueMin, issueMax) {
if e.API == "/issues" {
if !yield(e.Typed.(*Issue)) {
break
Expand Down Expand Up @@ -107,18 +107,19 @@ func CleanBody(body string) string {
return body
}

// Events calls [Events] with the client's db.
func (c *Client) Events(project string, issueMin, issueMax int64) iter.Seq[*Event] {
return Events(c.db, project, issueMin, issueMax)
}

// Events returns an iterator over issue events for the given project,
// limited to issues in the range issueMin ≤ issue ≤ issueMax.
// If issueMax < 0, there is no upper limit.
// The events are iterated over in (Project, Issue, API, ID) order,
// so "/issues" events come first, then "/issues/comments", then "/issues/events".
// Within a specific API, the events are ordered by increasing ID,
// which corresponds to increasing event time on GitHub.
func (c *Client) Events(project string, issueMin, issueMax int64) iter.Seq[*Event] {
return events(c.db, project, issueMin, issueMax)
}

func events(db storage.DB, project string, issueMin, issueMax int64) iter.Seq[*Event] {
func Events(db storage.DB, project string, issueMin, issueMax int64) iter.Seq[*Event] {
return func(yield func(*Event) bool) {
start := o(project, issueMin)
if issueMax < 0 {
Expand Down Expand Up @@ -206,6 +207,7 @@ type IssueEvent struct {
URL string `json:"url"`
Actor User `json:"actor"`
Event string `json:"event"`
Label Label `json:"label"` // for "labeled" and "unlabeled" events
Labels []Label `json:"labels"`
LockReason string `json:"lock_reason"`
CreatedAt string `json:"created_at"`
Expand Down

0 comments on commit 43c6ca6

Please sign in to comment.