From: Karim Hamidou Date: Tue, 6 Aug 2024 01:15:53 +0000 (-0700) Subject: group by repo X-Git-Tag: 0.91 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=65dc889083ff67897e6e044c454d9c8592bacbac;p=prguy.git group by repo --- diff --git a/cmd/prguy/main.go b/cmd/prguy/main.go index c21b1d5..79cb3fc 100644 --- a/cmd/prguy/main.go +++ b/cmd/prguy/main.go @@ -85,25 +85,41 @@ func setupMenu(ctx context.Context, cancel context.CancelFunc) { systray.ResetMenu() - if len(myPRs) == 0 { + if len(myPRs.Keys()) == 0 { systray.AddMenuItem("No PRs out from you – yet", "") } else { - for _, pr := range myPRs { - systrayItem := renderPR(pr) - channels = append(channels, systrayItem.ClickedCh) - channelsMap[systrayItem.ClickedCh] = pr + for _, repo := range myPRs.Keys() { + prs, _ := myPRs.Get(repo) + prList := prs.([]pullRequest) + if len(prList) == 0 { + continue + } + + for _, pr := range prList { + systrayItem := renderPR(pr) + channels = append(channels, systrayItem.ClickedCh) + channelsMap[systrayItem.ClickedCh] = pr + } } } systray.AddSeparator() - if len(myPRs) != 0 && len(otherPRs) == 0 { + if len(myPRs.Keys()) != 0 && len(otherPRs.Keys()) == 0 { systray.AddMenuItem("No PRs to review, no news is good news.", "") - } else if len(otherPRs) != 0 { - for _, pr := range otherPRs { - systrayItem := renderPR(pr) - channels = append(channels, systrayItem.ClickedCh) - channelsMap[systrayItem.ClickedCh] = pr + } else if len(otherPRs.Keys()) != 0 { + for _, repo := range otherPRs.Keys() { + prs, _ := otherPRs.Get(repo) + prList := prs.([]pullRequest) + if len(prList) == 0 { + continue + } + + for _, pr := range prList { + systrayItem := renderPR(pr) + channels = append(channels, systrayItem.ClickedCh) + channelsMap[systrayItem.ClickedCh] = pr + } } } diff --git a/cmd/prguy/repos.go b/cmd/prguy/repos.go index 85286ce..ec11d96 100644 --- a/cmd/prguy/repos.go +++ b/cmd/prguy/repos.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/iancoleman/orderedmap" "io/ioutil" "net/http" "time" @@ -27,14 +28,20 @@ type pullRequest struct { buildStatus buildStatus } -func listUserPRs(token string, isDemo bool) ([]pullRequest, []pullRequest, error) { +func listUserPRs(token string, isDemo bool) (*orderedmap.OrderedMap, *orderedmap.OrderedMap, error) { + myPRs := orderedmap.New() + otherPRs := orderedmap.New() + if isDemo { - return []pullRequest{ - {url: "blah", title: "Unbreak prod", mergeable: true, buildStatus: buildFailure}, - {url: "blah", title: "Fix the tests michael broke", mergeable: true, buildStatus: buildPending}, - }, []pullRequest{ - {url: "blah", title: "Revert michael's 'fix'", mergeable: true, buildStatus: buildFailure}, - }, nil + myPRs.Set("michael/repo", []pullRequest{ + {url: "blah", title: "Unbreak prod", mergeable: true, buildStatus: buildFailure}, + {url: "blah", title: "Fix the tests michael broke", mergeable: true, buildStatus: buildPending}}) + + otherPRs.Set("michael/repo", []pullRequest{ + {url: "blah", title: "Revert michael's 'fix'", mergeable: true, buildStatus: buildFailure}, + }) + + return myPRs, otherPRs, nil } githubUrl := "https://api.github.com/notifications?participating=true&all=true" @@ -63,9 +70,6 @@ func listUserPRs(token string, isDemo bool) ([]pullRequest, []pullRequest, error return nil, nil, err } - var myPRs []pullRequest - var otherPRs []pullRequest - var jsonData []map[string]interface{} var PRsSeen = make(map[string]bool) @@ -129,19 +133,34 @@ func listUserPRs(token string, isDemo bool) ([]pullRequest, []pullRequest, error mergeableState := prData["mergeable_state"].(string) mergeable := mergeableState == "clean" || mergeableState == "has_hooks" if reason == "author" { - myPRs = append(myPRs, pullRequest{ + if _, ok := myPRs.Get(repoFullName); !ok { + myPRs.Set(repoFullName, []pullRequest{}) + } + + prContents := pullRequest{ url: prUrl, title: subject["title"].(string), mergeable: mergeable, buildStatus: prStatus, - }) + } + contents, _ := myPRs.Get(repoFullName) + var PRsList = contents.([]pullRequest) + PRsList = append(PRsList, prContents) + myPRs.Set(repoFullName, PRsList) } else { - otherPRs = append(otherPRs, pullRequest{ - url: prUrl, + if _, ok := otherPRs.Get(repoFullName); !ok { + otherPRs.Set(repoFullName, []pullRequest{}) + } + + prContents := pullRequest{url: prUrl, title: subject["title"].(string), mergeable: mergeable, buildStatus: prStatus, - }) + } + contents, _ := otherPRs.Get(repoFullName) + var PRsList = contents.([]pullRequest) + PRsList = append(PRsList, prContents) + otherPRs.Set(repoFullName, PRsList) } time.Sleep(1 * time.Second) diff --git a/go.mod b/go.mod index d49640c..83fd09d 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.22.3 require ( fyne.io/systray v1.10.0 + github.com/iancoleman/orderedmap v0.3.0 github.com/ncruces/zenity v0.10.12 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 ) diff --git a/go.sum b/go.sum index 4ee7ce4..c429481 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f h1:OGqDDftRTwrvUoL6pO github.com/dchest/jsmin v0.0.0-20220218165748-59f39799265f/go.mod h1:Dv9D0NUlAsaQcGQZa5kc5mqR9ua72SmA8VXi4cd+cBw= github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc= +github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE= github.com/josephspurrier/goversioninfo v1.4.0 h1:Puhl12NSHUSALHSuzYwPYQkqa2E1+7SrtAPJorKK0C8= github.com/josephspurrier/goversioninfo v1.4.0/go.mod h1:JWzv5rKQr+MmW+LvM412ToT/IkYDZjaclF2pKDss8IY= github.com/ncruces/zenity v0.10.12 h1:o4SErDa0kQijlqG6W4OYYzO6kA0fGu34uegvJGcMLBI=