Skip to content

Commit 43c6ca6

Browse files
committed
internal/devtools/cmd/labelhist: show label 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]>
1 parent 89c5dcb commit 43c6ca6

File tree

2 files changed

+109
-6
lines changed

2 files changed

+109
-6
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
/*
6+
Labelhist displays the events of a GitHub issue that affect its labels.
7+
8+
Usage:
9+
10+
labelhist issues...
11+
12+
Each argument can be a single issue number or a range of numbers "from-to".
13+
*/
14+
package main
15+
16+
import (
17+
"context"
18+
"flag"
19+
"fmt"
20+
"log"
21+
"log/slog"
22+
"os"
23+
"strconv"
24+
"strings"
25+
26+
"golang.org/x/oscar/internal/gcp/firestore"
27+
"golang.org/x/oscar/internal/github"
28+
)
29+
30+
var project = flag.String("project", "golang/go", "GitHub project")
31+
32+
func usage() {
33+
fmt.Fprintf(os.Stderr, "usage: labelhist issues...\n")
34+
flag.PrintDefaults()
35+
os.Exit(2)
36+
}
37+
38+
func main() {
39+
log.SetFlags(0)
40+
log.SetPrefix("labelhist: ")
41+
flag.Usage = usage
42+
flag.Parse()
43+
if err := run(context.Background()); err != nil {
44+
log.Fatal(err)
45+
}
46+
}
47+
48+
func run(ctx context.Context) error {
49+
var ranges []Range
50+
for _, arg := range flag.Args() {
51+
r, err := parseIssueArg(arg)
52+
if err != nil {
53+
return err
54+
}
55+
ranges = append(ranges, r)
56+
}
57+
lg := slog.New(slog.NewTextHandler(os.Stderr, nil))
58+
db, err := firestore.NewDB(ctx, lg, "oscar-go-1", "prod")
59+
if err != nil {
60+
return err
61+
}
62+
63+
for _, r := range ranges {
64+
for ev := range github.Events(db, *project, r.min, r.max) {
65+
switch ev.API {
66+
case "/issues":
67+
fmt.Printf("%d:\n", ev.Issue)
68+
case "/issues/events":
69+
ie := ev.Typed.(*github.IssueEvent)
70+
if ie.Event == "labeled" || ie.Event == "unlabeled" {
71+
c := '+'
72+
if ie.Event == "unlabeled" {
73+
c = '-'
74+
}
75+
fmt.Printf(" %s %-10s %c%s\n", ie.CreatedAt, ie.Actor.Login, c, ie.Label.Name)
76+
}
77+
}
78+
}
79+
}
80+
return nil
81+
}
82+
83+
type Range struct {
84+
min, max int64
85+
}
86+
87+
func parseIssueArg(s string) (Range, error) {
88+
sfrom, sto, found := strings.Cut(s, "-")
89+
from, err := strconv.ParseInt(sfrom, 10, 64)
90+
if err != nil {
91+
return Range{}, err
92+
}
93+
if !found {
94+
return Range{from, from}, nil
95+
}
96+
to, err := strconv.ParseInt(sto, 10, 64)
97+
if err != nil {
98+
return Range{}, err
99+
}
100+
return Range{from, to}, nil
101+
}

internal/github/data.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func LookupIssue(db storage.DB, project string, issue int64) (*Issue, error) {
6262
// only consulting the database (not actual GitHub).
6363
func LookupIssues(db storage.DB, project string, issueMin, issueMax int64) iter.Seq[*Issue] {
6464
return func(yield func(*Issue) bool) {
65-
for e := range events(db, project, issueMin, issueMax) {
65+
for e := range Events(db, project, issueMin, issueMax) {
6666
if e.API == "/issues" {
6767
if !yield(e.Typed.(*Issue)) {
6868
break
@@ -107,18 +107,19 @@ func CleanBody(body string) string {
107107
return body
108108
}
109109

110+
// Events calls [Events] with the client's db.
111+
func (c *Client) Events(project string, issueMin, issueMax int64) iter.Seq[*Event] {
112+
return Events(c.db, project, issueMin, issueMax)
113+
}
114+
110115
// Events returns an iterator over issue events for the given project,
111116
// limited to issues in the range issueMin ≤ issue ≤ issueMax.
112117
// If issueMax < 0, there is no upper limit.
113118
// The events are iterated over in (Project, Issue, API, ID) order,
114119
// so "/issues" events come first, then "/issues/comments", then "/issues/events".
115120
// Within a specific API, the events are ordered by increasing ID,
116121
// which corresponds to increasing event time on GitHub.
117-
func (c *Client) Events(project string, issueMin, issueMax int64) iter.Seq[*Event] {
118-
return events(c.db, project, issueMin, issueMax)
119-
}
120-
121-
func events(db storage.DB, project string, issueMin, issueMax int64) iter.Seq[*Event] {
122+
func Events(db storage.DB, project string, issueMin, issueMax int64) iter.Seq[*Event] {
122123
return func(yield func(*Event) bool) {
123124
start := o(project, issueMin)
124125
if issueMax < 0 {
@@ -206,6 +207,7 @@ type IssueEvent struct {
206207
URL string `json:"url"`
207208
Actor User `json:"actor"`
208209
Event string `json:"event"`
210+
Label Label `json:"label"` // for "labeled" and "unlabeled" events
209211
Labels []Label `json:"labels"`
210212
LockReason string `json:"lock_reason"`
211213
CreatedAt string `json:"created_at"`

0 commit comments

Comments
 (0)