mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
75
modules/gitlab/display.go
Normal file
75
modules/gitlab/display.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
func (widget *Widget) display() {
|
||||
|
||||
project := widget.currentGitlabProject()
|
||||
if project == nil {
|
||||
widget.View.SetText(" Gitlab project data is unavailable ")
|
||||
return
|
||||
}
|
||||
|
||||
widget.View.SetTitle(fmt.Sprintf("%s- %s", widget.Name, widget.title(project)))
|
||||
|
||||
str := wtf.SigilStr(len(widget.GitlabProjects), widget.Idx, widget.View) + "\n"
|
||||
str = str + " [red]Stats[white]\n"
|
||||
str = str + widget.displayStats(project)
|
||||
str = str + "\n"
|
||||
str = str + " [red]Open Approval Requests[white]\n"
|
||||
str = str + widget.displayMyApprovalRequests(project, wtf.Config.UString("wtf.mods.gitlab.username"))
|
||||
str = str + "\n"
|
||||
str = str + " [red]My Merge Requests[white]\n"
|
||||
str = str + widget.displayMyMergeRequests(project, wtf.Config.UString("wtf.mods.gitlab.username"))
|
||||
|
||||
widget.View.SetText(str)
|
||||
}
|
||||
|
||||
func (widget *Widget) displayMyMergeRequests(project *GitlabProject, username string) string {
|
||||
mrs := project.myMergeRequests(username)
|
||||
|
||||
if len(mrs) == 0 {
|
||||
return " [grey]none[white]\n"
|
||||
}
|
||||
|
||||
str := ""
|
||||
for _, mr := range mrs {
|
||||
str = str + fmt.Sprintf(" [green]%4d[white] %s\n", mr.IID, mr.Title)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) displayMyApprovalRequests(project *GitlabProject, username string) string {
|
||||
mrs := project.myApprovalRequests(username)
|
||||
|
||||
if len(mrs) == 0 {
|
||||
return " [grey]none[white]\n"
|
||||
}
|
||||
|
||||
str := ""
|
||||
for _, mr := range mrs {
|
||||
str = str + fmt.Sprintf(" [green]%4d[white] %s\n", mr.IID, mr.Title)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) displayStats(project *GitlabProject) string {
|
||||
str := fmt.Sprintf(
|
||||
" MRs: %d Issues: %d Stars: %d\n",
|
||||
project.MergeRequestCount(),
|
||||
project.IssueCount(),
|
||||
project.StarCount(),
|
||||
)
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) title(project *GitlabProject) string {
|
||||
return fmt.Sprintf("[green]%s [white]", project.Path)
|
||||
}
|
||||
113
modules/gitlab/gitlab_repo.go
Normal file
113
modules/gitlab/gitlab_repo.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
glb "github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type GitlabProject struct {
|
||||
gitlab *glb.Client
|
||||
Path string
|
||||
|
||||
MergeRequests []*glb.MergeRequest
|
||||
RemoteProject *glb.Project
|
||||
}
|
||||
|
||||
func NewGitlabProject(name string, namespace string, gitlab *glb.Client) *GitlabProject {
|
||||
path := namespace + "/" + name
|
||||
project := GitlabProject{
|
||||
gitlab: gitlab,
|
||||
Path: path,
|
||||
}
|
||||
|
||||
return &project
|
||||
}
|
||||
|
||||
// Refresh reloads the gitlab data via the Gitlab API
|
||||
func (project *GitlabProject) Refresh() {
|
||||
project.MergeRequests, _ = project.loadMergeRequests()
|
||||
project.RemoteProject, _ = project.loadRemoteProject()
|
||||
}
|
||||
|
||||
/* -------------------- Counts -------------------- */
|
||||
|
||||
func (project *GitlabProject) IssueCount() int {
|
||||
if project.RemoteProject == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return project.RemoteProject.OpenIssuesCount
|
||||
}
|
||||
|
||||
func (project *GitlabProject) MergeRequestCount() int {
|
||||
return len(project.MergeRequests)
|
||||
}
|
||||
|
||||
func (project *GitlabProject) StarCount() int {
|
||||
if project.RemoteProject == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return project.RemoteProject.StarCount
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
// myMergeRequests returns a list of merge requests created by username on this project
|
||||
func (project *GitlabProject) myMergeRequests(username string) []*glb.MergeRequest {
|
||||
mrs := []*glb.MergeRequest{}
|
||||
|
||||
for _, mr := range project.MergeRequests {
|
||||
user := mr.Author
|
||||
|
||||
if user.Username == username {
|
||||
mrs = append(mrs, mr)
|
||||
}
|
||||
}
|
||||
|
||||
return mrs
|
||||
}
|
||||
|
||||
// myApprovalRequests returns a list of merge requests for which username has been
|
||||
// requested to approve
|
||||
func (project *GitlabProject) myApprovalRequests(username string) []*glb.MergeRequest {
|
||||
mrs := []*glb.MergeRequest{}
|
||||
|
||||
for _, mr := range project.MergeRequests {
|
||||
approvers, _, err := project.gitlab.MergeRequests.GetMergeRequestApprovals(project.Path, mr.IID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, approver := range approvers.Approvers {
|
||||
if approver.User.Username == username {
|
||||
mrs = append(mrs, mr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mrs
|
||||
}
|
||||
|
||||
func (project *GitlabProject) loadMergeRequests() ([]*glb.MergeRequest, error) {
|
||||
state := "opened"
|
||||
opts := glb.ListProjectMergeRequestsOptions{
|
||||
State: &state,
|
||||
}
|
||||
|
||||
mrs, _, err := project.gitlab.MergeRequests.ListProjectMergeRequests(project.Path, &opts)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mrs, nil
|
||||
}
|
||||
|
||||
func (project *GitlabProject) loadRemoteProject() (*glb.Project, error) {
|
||||
projectsitory, _, err := project.gitlab.Projects.GetProject(project.Path)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return projectsitory, nil
|
||||
}
|
||||
145
modules/gitlab/widget.go
Normal file
145
modules/gitlab/widget.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
glb "github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
const HelpText = `
|
||||
Keyboard commands for Gitlab:
|
||||
|
||||
/: Show/hide this help window
|
||||
h: Previous project
|
||||
l: Next project
|
||||
r: Refresh the data
|
||||
|
||||
arrow left: Previous project
|
||||
arrow right: Next project
|
||||
`
|
||||
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.TextWidget
|
||||
|
||||
gitlab *glb.Client
|
||||
|
||||
GitlabProjects []*GitlabProject
|
||||
Idx int
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
baseURL := wtf.Config.UString("wtf.mods.gitlab.domain")
|
||||
gitlab := glb.NewClient(nil, apiKey())
|
||||
|
||||
if baseURL != "" {
|
||||
gitlab.SetBaseURL(baseURL)
|
||||
}
|
||||
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, "Gitlab", "gitlab", true),
|
||||
|
||||
gitlab: gitlab,
|
||||
|
||||
Idx: 0,
|
||||
}
|
||||
|
||||
widget.GitlabProjects = widget.buildProjectCollection(wtf.Config.UMap("wtf.mods.gitlab.projects"))
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
for _, project := range widget.GitlabProjects {
|
||||
project.Refresh()
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) Next() {
|
||||
widget.Idx = widget.Idx + 1
|
||||
if widget.Idx == len(widget.GitlabProjects) {
|
||||
widget.Idx = 0
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) Prev() {
|
||||
widget.Idx = widget.Idx - 1
|
||||
if widget.Idx < 0 {
|
||||
widget.Idx = len(widget.GitlabProjects) - 1
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func apiKey() string {
|
||||
return wtf.Config.UString(
|
||||
"wtf.mods.gitlab.apiKey",
|
||||
os.Getenv("WTF_GITLAB_TOKEN"),
|
||||
)
|
||||
}
|
||||
|
||||
func (widget *Widget) buildProjectCollection(projectData map[string]interface{}) []*GitlabProject {
|
||||
gitlabProjects := []*GitlabProject{}
|
||||
|
||||
for name, namespace := range projectData {
|
||||
project := NewGitlabProject(name, namespace.(string), widget.gitlab)
|
||||
gitlabProjects = append(gitlabProjects, project)
|
||||
}
|
||||
|
||||
return gitlabProjects
|
||||
}
|
||||
|
||||
func (widget *Widget) currentGitlabProject() *GitlabProject {
|
||||
if len(widget.GitlabProjects) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if widget.Idx < 0 || widget.Idx >= len(widget.GitlabProjects) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return widget.GitlabProjects[widget.Idx]
|
||||
}
|
||||
|
||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch string(event.Rune()) {
|
||||
case "/":
|
||||
widget.ShowHelp()
|
||||
return nil
|
||||
case "h":
|
||||
widget.Prev()
|
||||
return nil
|
||||
case "l":
|
||||
widget.Next()
|
||||
return nil
|
||||
case "r":
|
||||
widget.Refresh()
|
||||
return nil
|
||||
}
|
||||
|
||||
switch event.Key() {
|
||||
case tcell.KeyLeft:
|
||||
widget.Prev()
|
||||
return nil
|
||||
case tcell.KeyRight:
|
||||
widget.Next()
|
||||
return nil
|
||||
default:
|
||||
return event
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user