Skip to content

Commit

Permalink
Add gerrit widget
Browse files Browse the repository at this point in the history
  • Loading branch information
anandsudhir committed Jun 27, 2018
1 parent 3140241 commit 3a58b6a
Show file tree
Hide file tree
Showing 54 changed files with 7,972 additions and 219 deletions.
32 changes: 19 additions & 13 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
name = "github.com/xanzy/go-gitlab"
branch = "master"

[[constraint]]
name = "github.com/andygrunwald/go-gerrit"
branch = "master"

[[constraint]]
name = "github.com/jessevdk/go-flags"
version = "1.4.0"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Many thanks to all these developers.

* [calendar](https://google.golang.org/api/calendar/v3)
* [config](https://github.com/olebedev/config)
* [go-gerrit](https://github.com/andygrunwald/go-gerrit)
* [go-github](https://github.com/google/go-github)
* [goreleaser](https://github.com/goreleaser/goreleaser)
* [newrelic](https://github.com/yfronto/newrelic)
Expand Down
90 changes: 90 additions & 0 deletions _site/content/posts/modules/gerrit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
title: "Gerrit"
date: 2018-06-27T15:55:42-07:00
draft: false
---

<img src="/imgs/modules/gerrit.png" width="640" height="384" alt="gerrit screenshot" />

Displays information about your projects hosted on Gerrit:

#### Open Incoming Reviews

All open reviews that are requesting your approval.

#### My Outgoing Reviews

All open reviews created by you.

## Source Code

```bash
wtf/gerrit/
```

## Required ENV Variables

<span class="caption">Key:</span> `WTF_GERRIT_PASSWORD` <br />
<span class="caption">Action:</span> Your <a href="https://gerrit-review.googlesource.com/Documentation/user-upload.html#http">Gerrit HTTP Password</a>.

## Keyboard Commands

<span class="caption">Key:</span> `/` <br />
<span class="caption">Action:</span> Open/close the widget's help window.

<span class="caption">Key:</span> `h` <br />
<span class="caption">Action:</span> Show the previous project.

<span class="caption">Key:</span> `l` <br />
<span class="caption">Action:</span> Show the next project.

<span class="caption">Key:</span> `` <br />
<span class="caption">Action:</span> Show the previous project.

<span class="caption">Key:</span> `` <br />
<span class="caption">Action:</span> Show the next project.

## Configuration

```yaml
gerrit:
enabled: true
position:
top: 2
left: 3
height: 2
width: 2
refreshInterval: 300
projects:
- org/test-project"
- dotfiles
username: "myname"
```
### Attributes
`enabled` <br />
Determines whether or not this module is executed and if its data displayed onscreen. <br />
Values: `true`, `false`.

`position` <br />
Defines where in the grid this module's widget will be displayed. <br />

`refreshInterval` <br />
How often, in seconds, this module will update its data. <br />
Values: A positive integer, `0..n`.

`domain` <br />
_Optional_. Your Gerrit corporate domain. <br />
Values: A valid URI.

`projects` <br />
A list of Gerrit project names to fetch data for. <br />

`username` <br />
Your Gerrit username. <br />

`verifyServerCertificate` <br />
_Optional_ <br />
Determines whether or not the server's certificate chain and host name are verified. <br />
Values: `true`, `false`.
Binary file added _site/static/imgs/modules/gerrit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions _site/themes/hyde-hyde/layouts/partials/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ <h3 style="color: white;">Content</h3>
<li class="sidebar-list-item-2"><a href="/posts/modules/clocks/">Clocks</a></li>
<li class="sidebar-list-item-2"><a href="/posts/modules/cryptocurrencies/cryptolive/">CryptoLive</a></li>
<li class="sidebar-list-item-2"><a href="/posts/modules/cmdrunner/">CmdRunner</a></li>
<li class="sidebar-list-item-2"><a href="/posts/modules/gerrit/">Gerrit</a></li>
<li class="sidebar-list-item-2"><a href="/posts/modules/git/">Git</a></li>
<li class="sidebar-list-item-2"><a href="/posts/modules/github/">GitHub</a></li>
<li class="sidebar-list-item-2"><a href="/posts/modules/gitlab/">Gitlab</a></li>
Expand Down
73 changes: 73 additions & 0 deletions gerrit/display.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package gerrit

import (
"fmt"

"github.com/senorprogrammer/wtf/wtf"
)

func (widget *Widget) display() {

project := widget.currentGerritProject()
if project == nil {
fmt.Fprintf(widget.View, "%s", " Gerrit project data is unavailable (1)")
return
}

widget.View.SetTitle(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) displayMyOutgoingReviews(project *GerritProject, username string) string {
ors := project.myOutgoingReviews(username)

if len(ors) == 0 {
return " [grey]none[white]\n"
}

str := ""
for _, r := range ors {
str = str + fmt.Sprintf(" [green]%4s[white] %s\n", r.ChangeID, r.Subject)
}

return str
}

func (widget *Widget) displayMyIncomingReviews(project *GerritProject, username string) string {
irs := project.myIncomingReviews(username)

if len(irs) == 0 {
return " [grey]none[white]\n"
}

str := ""
for _, r := range irs {
str = str + fmt.Sprintf(" [green]%4s[white] %s\n", r.ChangeID, r.Subject)
}

return str
}

func (widget *Widget) displayStats(project *GerritProject) string {
str := fmt.Sprintf(
" Reviews: %d\n",
project.ReviewCount(),
)

return str
}

func (widget *Widget) title(project *GerritProject) string {
return fmt.Sprintf("[green]%s [white]", project.Path)
}
100 changes: 100 additions & 0 deletions gerrit/gerrit_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package gerrit

import (
glb "github.com/andygrunwald/go-gerrit"
)

type GerritProject struct {
gerrit *glb.Client
Path string

Changes *[]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() {
project.Changes, _ = project.loadChanges()
}

/* -------------------- Counts -------------------- */

func (project *GerritProject) IssueCount() int {
if project.Changes == nil {
return 0
}

return len(*project.Changes)
}

func (project *GerritProject) ReviewCount() int {
if project.Changes == nil {
return 0
}

return len(*project.Changes)
}

/* -------------------- Unexported Functions -------------------- */

// myOutgoingReviews returns a list of my outgoing reviews created by username on this project
func (project *GerritProject) myOutgoingReviews(username string) []glb.ChangeInfo {
changes := []glb.ChangeInfo{}

if project.Changes == nil {
return changes
}

for _, change := range *project.Changes {
user := change.Owner

if user.Username == username {
changes = append(changes, change)
}
}

return changes
}

// myIncomingReviews returns a list of merge requests for which username has been requested to ChangeInfo
func (project *GerritProject) myIncomingReviews(username string) []glb.ChangeInfo {
changes := []glb.ChangeInfo{}

if project.Changes == nil {
return changes
}

for _, change := range *project.Changes {
reviewers := change.Reviewers

for _, reviewer := range reviewers["REVIEWER"] {
if reviewer.Username == username {
changes = append(changes, change)
}
}
}

return changes
}

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
}
Loading

0 comments on commit 3a58b6a

Please sign in to comment.