mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
76
modules/gerrit/display.go
Normal file
76
modules/gerrit/display.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package gerrit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
func (widget *Widget) display() {
|
||||
|
||||
project := widget.currentGerritProject()
|
||||
if project == nil {
|
||||
widget.View.SetText(fmt.Sprintf("%s", " Gerrit project data is unavailable (1)"))
|
||||
return
|
||||
}
|
||||
|
||||
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s- %s", widget.Name, widget.title(project))))
|
||||
|
||||
str := wtf.SigilStr(len(widget.GerritProjects), widget.Idx, widget.View) + "\n"
|
||||
str = str + " [red]Stats[white]\n"
|
||||
str = str + widget.displayStats(project)
|
||||
str = str + "\n"
|
||||
str = str + " [red]Open Incoming Reviews[white]\n"
|
||||
str = str + widget.displayMyIncomingReviews(project, wtf.Config.UString("wtf.mods.gerrit.username"))
|
||||
str = str + "\n"
|
||||
str = str + " [red]My Outgoing Reviews[white]\n"
|
||||
str = str + widget.displayMyOutgoingReviews(project, wtf.Config.UString("wtf.mods.gerrit.username"))
|
||||
|
||||
widget.View.SetText(str)
|
||||
}
|
||||
|
||||
func (widget *Widget) displayMyIncomingReviews(project *GerritProject, username string) string {
|
||||
if len(project.IncomingReviews) == 0 {
|
||||
return " [grey]none[white]\n"
|
||||
}
|
||||
|
||||
str := ""
|
||||
for idx, r := range project.IncomingReviews {
|
||||
str = str + fmt.Sprintf(" [%s] [green]%d[white] [%s] %s\n", widget.rowColor(idx), r.Number, widget.rowColor(idx), r.Subject)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) displayMyOutgoingReviews(project *GerritProject, username string) string {
|
||||
if len(project.OutgoingReviews) == 0 {
|
||||
return " [grey]none[white]\n"
|
||||
}
|
||||
|
||||
str := ""
|
||||
for idx, r := range project.OutgoingReviews {
|
||||
str = str + fmt.Sprintf(" [%s] [green]%d[white] [%s] %s\n", widget.rowColor(idx+len(project.IncomingReviews)), r.Number, widget.rowColor(idx+len(project.IncomingReviews)), r.Subject)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) displayStats(project *GerritProject) string {
|
||||
str := fmt.Sprintf(
|
||||
" Reviews: %d\n",
|
||||
project.ReviewCount,
|
||||
)
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) rowColor(index int) string {
|
||||
if widget.View.HasFocus() && (index == widget.selected) {
|
||||
return wtf.DefaultFocussedRowColor()
|
||||
}
|
||||
return wtf.RowColor("gerrit", index)
|
||||
}
|
||||
|
||||
func (widget *Widget) title(project *GerritProject) string {
|
||||
return fmt.Sprintf("[green]%s [white]", project.Path)
|
||||
}
|
||||
102
modules/gerrit/gerrit_repo.go
Normal file
102
modules/gerrit/gerrit_repo.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package gerrit
|
||||
|
||||
import (
|
||||
glb "github.com/andygrunwald/go-gerrit"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
type GerritProject struct {
|
||||
gerrit *glb.Client
|
||||
Path string
|
||||
|
||||
Changes *[]glb.ChangeInfo
|
||||
ReviewCount int
|
||||
IncomingReviews []glb.ChangeInfo
|
||||
OutgoingReviews []glb.ChangeInfo
|
||||
}
|
||||
|
||||
func NewGerritProject(path string, gerrit *glb.Client) *GerritProject {
|
||||
project := GerritProject{
|
||||
gerrit: gerrit,
|
||||
Path: path,
|
||||
}
|
||||
|
||||
return &project
|
||||
}
|
||||
|
||||
// Refresh reloads the gerrit data via the Gerrit API
|
||||
func (project *GerritProject) Refresh() {
|
||||
username := wtf.Config.UString("wtf.mods.gerrit.username")
|
||||
project.Changes, _ = project.loadChanges()
|
||||
|
||||
project.ReviewCount = project.countReviews(project.Changes)
|
||||
project.IncomingReviews = project.myIncomingReviews(project.Changes, username)
|
||||
project.OutgoingReviews = project.myOutgoingReviews(project.Changes, username)
|
||||
|
||||
}
|
||||
|
||||
/* -------------------- Counts -------------------- */
|
||||
|
||||
func (project *GerritProject) countReviews(changes *[]glb.ChangeInfo) int {
|
||||
if changes == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return len(*changes)
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
// myOutgoingReviews returns a list of my outgoing reviews created by username on this project
|
||||
func (project *GerritProject) myOutgoingReviews(changes *[]glb.ChangeInfo, username string) []glb.ChangeInfo {
|
||||
var ors []glb.ChangeInfo
|
||||
|
||||
if changes == nil {
|
||||
return ors
|
||||
}
|
||||
|
||||
for _, change := range *changes {
|
||||
user := change.Owner
|
||||
|
||||
if user.Username == username {
|
||||
ors = append(ors, change)
|
||||
}
|
||||
}
|
||||
|
||||
return ors
|
||||
}
|
||||
|
||||
// myIncomingReviews returns a list of merge requests for which username has been requested to ChangeInfo
|
||||
func (project *GerritProject) myIncomingReviews(changes *[]glb.ChangeInfo, username string) []glb.ChangeInfo {
|
||||
var irs []glb.ChangeInfo
|
||||
|
||||
if changes == nil {
|
||||
return irs
|
||||
}
|
||||
|
||||
for _, change := range *changes {
|
||||
reviewers := change.Reviewers
|
||||
|
||||
for _, reviewer := range reviewers["REVIEWER"] {
|
||||
if reviewer.Username == username {
|
||||
irs = append(irs, change)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return irs
|
||||
}
|
||||
|
||||
func (project *GerritProject) loadChanges() (*[]glb.ChangeInfo, error) {
|
||||
opt := &glb.QueryChangeOptions{}
|
||||
opt.Query = []string{"(projects:" + project.Path + "+ is:open + owner:self) " + " OR " +
|
||||
"(projects:" + project.Path + " + is:open + ((reviewer:self + -owner:self + -star:ignore) + OR + assignee:self))"}
|
||||
opt.AdditionalFields = []string{"DETAILED_LABELS", "DETAILED_ACCOUNTS"}
|
||||
changes, _, err := project.gerrit.Changes.QueryChanges(opt)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return changes, err
|
||||
}
|
||||
238
modules/gerrit/widget.go
Normal file
238
modules/gerrit/widget.go
Normal file
@@ -0,0 +1,238 @@
|
||||
package gerrit
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
glb "github.com/andygrunwald/go-gerrit"
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
const HelpText = `
|
||||
Keyboard commands for Gerrit:
|
||||
|
||||
/: Show/hide this help window
|
||||
h: Show the previous project
|
||||
l: Show the next project
|
||||
j: Select the next review in the list
|
||||
k: Select the previous review in the list
|
||||
r: Refresh the data
|
||||
|
||||
arrow left: Show the previous project
|
||||
arrow right: Show the next project
|
||||
arrow down: Select the next review in the list
|
||||
arrow up: Select the previous review in the list
|
||||
|
||||
return: Open the selected review in a browser
|
||||
`
|
||||
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.TextWidget
|
||||
|
||||
gerrit *glb.Client
|
||||
|
||||
GerritProjects []*GerritProject
|
||||
Idx int
|
||||
selected int
|
||||
}
|
||||
|
||||
var (
|
||||
GerritURLPattern = regexp.MustCompile(`^(http|https)://(.*)$`)
|
||||
)
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, "Gerrit", "gerrit", true),
|
||||
|
||||
Idx: 0,
|
||||
}
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
|
||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||
widget.unselect()
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
baseURL := wtf.Config.UString("wtf.mods.gerrit.domain")
|
||||
username := wtf.Config.UString("wtf.mods.gerrit.username")
|
||||
|
||||
password := wtf.Config.UString(
|
||||
"wtf.mods.gerrit.password",
|
||||
os.Getenv("WTF_GERRIT_PASSWORD"),
|
||||
)
|
||||
|
||||
verifyServerCertificate := wtf.Config.UBool("wtf.mods.gerrit.verifyServerCertificate", true)
|
||||
|
||||
httpClient := &http.Client{Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: !verifyServerCertificate,
|
||||
},
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
},
|
||||
}
|
||||
|
||||
gerritUrl := baseURL
|
||||
submatches := GerritURLPattern.FindAllStringSubmatch(baseURL, -1)
|
||||
|
||||
if len(submatches) > 0 && len(submatches[0]) > 2 {
|
||||
submatch := submatches[0]
|
||||
gerritUrl = fmt.Sprintf(
|
||||
"%s://%s:%s@%s", submatch[1], username, password, submatch[2])
|
||||
}
|
||||
gerrit, err := glb.NewClient(gerritUrl, httpClient)
|
||||
if err != nil {
|
||||
widget.View.SetWrap(true)
|
||||
widget.View.SetTitle(widget.Name)
|
||||
widget.View.SetText(err.Error())
|
||||
return
|
||||
}
|
||||
widget.gerrit = gerrit
|
||||
widget.GerritProjects = widget.buildProjectCollection(wtf.Config.UList("wtf.mods.gerrit.projects"))
|
||||
|
||||
for _, project := range widget.GerritProjects {
|
||||
project.Refresh()
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) nextProject() {
|
||||
widget.Idx = widget.Idx + 1
|
||||
widget.unselect()
|
||||
if widget.Idx == len(widget.GerritProjects) {
|
||||
widget.Idx = 0
|
||||
}
|
||||
|
||||
widget.unselect()
|
||||
}
|
||||
|
||||
func (widget *Widget) prevProject() {
|
||||
widget.Idx = widget.Idx - 1
|
||||
if widget.Idx < 0 {
|
||||
widget.Idx = len(widget.GerritProjects) - 1
|
||||
}
|
||||
|
||||
widget.unselect()
|
||||
}
|
||||
|
||||
func (widget *Widget) nextReview() {
|
||||
widget.selected++
|
||||
project := widget.GerritProjects[widget.Idx]
|
||||
if widget.selected >= project.ReviewCount {
|
||||
widget.selected = 0
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) prevReview() {
|
||||
widget.selected--
|
||||
project := widget.GerritProjects[widget.Idx]
|
||||
if widget.selected < 0 {
|
||||
widget.selected = project.ReviewCount - 1
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) openReview() {
|
||||
sel := widget.selected
|
||||
project := widget.GerritProjects[widget.Idx]
|
||||
if sel >= 0 && sel < project.ReviewCount {
|
||||
change := glb.ChangeInfo{}
|
||||
if sel < len(project.IncomingReviews) {
|
||||
change = project.IncomingReviews[sel]
|
||||
} else {
|
||||
change = project.OutgoingReviews[sel-len(project.IncomingReviews)]
|
||||
}
|
||||
wtf.OpenFile(fmt.Sprintf("%s/%s/%d", wtf.Config.UString("wtf.mods.gerrit.domain"), "#/c", change.Number))
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *Widget) unselect() {
|
||||
widget.selected = -1
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) buildProjectCollection(projectData []interface{}) []*GerritProject {
|
||||
gerritProjects := []*GerritProject{}
|
||||
|
||||
for _, name := range projectData {
|
||||
project := NewGerritProject(name.(string), widget.gerrit)
|
||||
gerritProjects = append(gerritProjects, project)
|
||||
}
|
||||
|
||||
return gerritProjects
|
||||
}
|
||||
|
||||
func (widget *Widget) currentGerritProject() *GerritProject {
|
||||
if len(widget.GerritProjects) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if widget.Idx < 0 || widget.Idx >= len(widget.GerritProjects) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return widget.GerritProjects[widget.Idx]
|
||||
}
|
||||
|
||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch string(event.Rune()) {
|
||||
case "/":
|
||||
widget.ShowHelp()
|
||||
return nil
|
||||
case "h":
|
||||
widget.prevProject()
|
||||
return nil
|
||||
case "l":
|
||||
widget.nextProject()
|
||||
return nil
|
||||
case "j":
|
||||
widget.nextReview()
|
||||
return nil
|
||||
case "k":
|
||||
widget.prevReview()
|
||||
return nil
|
||||
case "r":
|
||||
widget.Refresh()
|
||||
return nil
|
||||
}
|
||||
|
||||
switch event.Key() {
|
||||
case tcell.KeyLeft:
|
||||
widget.prevProject()
|
||||
return nil
|
||||
case tcell.KeyRight:
|
||||
widget.nextProject()
|
||||
return nil
|
||||
case tcell.KeyDown:
|
||||
widget.nextReview()
|
||||
return nil
|
||||
case tcell.KeyUp:
|
||||
widget.prevReview()
|
||||
return nil
|
||||
case tcell.KeyEnter:
|
||||
widget.openReview()
|
||||
return nil
|
||||
case tcell.KeyEsc:
|
||||
widget.unselect()
|
||||
return event
|
||||
default:
|
||||
return event
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user