mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
73
modules/jenkins/client.go
Normal file
73
modules/jenkins/client.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package jenkins
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
func Create(jenkinsURL string, username string, apiKey string) (*View, error) {
|
||||
const apiSuffix = "api/json?pretty=true"
|
||||
parsedSuffix, err := url.Parse(apiSuffix)
|
||||
if err != nil {
|
||||
return &View{}, err
|
||||
}
|
||||
|
||||
parsedJenkinsURL, err := url.Parse(ensureLastSlash(jenkinsURL))
|
||||
if err != nil {
|
||||
return &View{}, err
|
||||
}
|
||||
jenkinsAPIURL := parsedJenkinsURL.ResolveReference(parsedSuffix)
|
||||
|
||||
req, _ := http.NewRequest("GET", jenkinsAPIURL.String(), nil)
|
||||
req.SetBasicAuth(username, apiKey)
|
||||
|
||||
verifyServerCertificate := wtf.Config.UBool("wtf.mods.jenkins.verifyServerCertificate", true)
|
||||
httpClient := &http.Client{Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: !verifyServerCertificate,
|
||||
},
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
},
|
||||
}
|
||||
resp, err := httpClient.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return &View{}, err
|
||||
}
|
||||
|
||||
view := &View{}
|
||||
parseJson(view, resp.Body)
|
||||
|
||||
return view, nil
|
||||
}
|
||||
|
||||
func ensureLastSlash(URL string) string {
|
||||
return strings.TrimRight(URL, "/") + "/"
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func parseJson(obj interface{}, text io.Reader) {
|
||||
jsonStream, err := ioutil.ReadAll(text)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(bytes.NewReader(jsonStream))
|
||||
|
||||
for {
|
||||
if err := decoder.Decode(obj); err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
8
modules/jenkins/job.go
Normal file
8
modules/jenkins/job.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package jenkins
|
||||
|
||||
type Job struct {
|
||||
Class string `json:"_class"`
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
Color string `json:"color"`
|
||||
}
|
||||
10
modules/jenkins/view.go
Normal file
10
modules/jenkins/view.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package jenkins
|
||||
|
||||
type View struct {
|
||||
Class string `json:"_class"`
|
||||
Description string `json:"description"`
|
||||
Jobs []Job `json:"jobs"`
|
||||
Name string `json:"name"`
|
||||
Property []string `json:"property"`
|
||||
Url string `json:"url"`
|
||||
}
|
||||
193
modules/jenkins/widget.go
Normal file
193
modules/jenkins/widget.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package jenkins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const HelpText = `
|
||||
Keyboard commands for Jenkins:
|
||||
|
||||
/: Show/hide this help window
|
||||
j: Select the next job in the list
|
||||
k: Select the previous job in the list
|
||||
r: Refresh the data
|
||||
|
||||
arrow down: Select the next job in the list
|
||||
arrow up: Select the previous job in the list
|
||||
|
||||
return: Open the selected job in a browser
|
||||
`
|
||||
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.TextWidget
|
||||
|
||||
view *View
|
||||
selected int
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
TextWidget: wtf.NewTextWidget(app, "Jenkins", "jenkins", true),
|
||||
}
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
widget.unselect()
|
||||
|
||||
widget.View.SetScrollable(true)
|
||||
widget.View.SetRegions(true)
|
||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
if widget.Disabled() {
|
||||
return
|
||||
}
|
||||
|
||||
view, err := Create(
|
||||
wtf.Config.UString("wtf.mods.jenkins.url"),
|
||||
wtf.Config.UString("wtf.mods.jenkins.user"),
|
||||
widget.apiKey(),
|
||||
)
|
||||
widget.view = view
|
||||
|
||||
if err != nil {
|
||||
widget.View.SetWrap(true)
|
||||
widget.View.SetTitle(widget.ContextualTitle(widget.Name))
|
||||
widget.View.SetText(err.Error())
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) display() {
|
||||
if widget.view == nil {
|
||||
return
|
||||
}
|
||||
|
||||
widget.View.SetWrap(false)
|
||||
|
||||
widget.View.Clear()
|
||||
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s: [red]%s", widget.Name, widget.view.Name)))
|
||||
widget.View.SetText(widget.contentFrom(widget.view))
|
||||
widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
|
||||
}
|
||||
|
||||
func (widget *Widget) apiKey() string {
|
||||
return wtf.Config.UString(
|
||||
"wtf.mods.jenkins.apiKey",
|
||||
os.Getenv("WTF_JENKINS_API_KEY"),
|
||||
)
|
||||
}
|
||||
|
||||
func (widget *Widget) contentFrom(view *View) string {
|
||||
var str string
|
||||
for idx, job := range view.Jobs {
|
||||
str = str + fmt.Sprintf(
|
||||
`["%d"][""][%s] [%s]%-6s[white]`,
|
||||
idx,
|
||||
widget.rowColor(idx),
|
||||
widget.jobColor(&job),
|
||||
job.Name,
|
||||
)
|
||||
|
||||
str = str + "\n"
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) rowColor(idx int) string {
|
||||
if widget.View.HasFocus() && (idx == widget.selected) {
|
||||
return wtf.DefaultFocussedRowColor()
|
||||
}
|
||||
|
||||
return wtf.DefaultRowColor()
|
||||
}
|
||||
|
||||
func (widget *Widget) jobColor(job *Job) string {
|
||||
switch job.Color {
|
||||
case "blue":
|
||||
return "blue"
|
||||
case "red":
|
||||
return "red"
|
||||
default:
|
||||
return "white"
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *Widget) next() {
|
||||
widget.selected++
|
||||
if widget.view != nil && widget.selected >= len(widget.view.Jobs) {
|
||||
widget.selected = 0
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) prev() {
|
||||
widget.selected--
|
||||
if widget.selected < 0 && widget.view != nil {
|
||||
widget.selected = len(widget.view.Jobs) - 1
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) openJob() {
|
||||
sel := widget.selected
|
||||
if sel >= 0 && widget.view != nil && sel < len(widget.view.Jobs) {
|
||||
job := &widget.view.Jobs[widget.selected]
|
||||
wtf.OpenFile(job.Url)
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *Widget) unselect() {
|
||||
widget.selected = -1
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
switch string(event.Rune()) {
|
||||
case "/":
|
||||
widget.ShowHelp()
|
||||
case "j":
|
||||
widget.next()
|
||||
return nil
|
||||
case "k":
|
||||
widget.prev()
|
||||
return nil
|
||||
case "r":
|
||||
widget.Refresh()
|
||||
return nil
|
||||
}
|
||||
|
||||
switch event.Key() {
|
||||
case tcell.KeyDown:
|
||||
widget.next()
|
||||
return nil
|
||||
case tcell.KeyEnter:
|
||||
widget.openJob()
|
||||
return nil
|
||||
case tcell.KeyEsc:
|
||||
widget.unselect()
|
||||
return event
|
||||
case tcell.KeyUp:
|
||||
widget.prev()
|
||||
return nil
|
||||
default:
|
||||
return event
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user