mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
implemented azure devops widget
This commit is contained in:
59
modules/azuredevops/client.go
Normal file
59
modules/azuredevops/client.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package azuredevops
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
azrBuild "github.com/microsoft/azure-devops-go-api/azuredevops/build"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (widget *Widget) getBuildStats() string {
|
||||
projName := widget.settings.projectName
|
||||
statusFilter := azrBuild.BuildStatusValues.All
|
||||
top := widget.settings.maxRows
|
||||
builds, err := widget.cli.GetBuilds(widget.ctx, azrBuild.GetBuildsArgs{Project: &projName, StatusFilter: &statusFilter, Top: &top})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not get builds").Error()
|
||||
}
|
||||
|
||||
result := ""
|
||||
for _, build := range builds.Value {
|
||||
num := *build.BuildNumber
|
||||
branch := *build.SourceBranch
|
||||
reason := *build.Reason
|
||||
triggers := *build.TriggerInfo
|
||||
if reason == azrBuild.BuildReasonValues.PullRequest {
|
||||
branch = triggers["pr.sourceBranch"]
|
||||
}
|
||||
branch = strings.TrimPrefix(branch, "refs/heads/")
|
||||
status := *build.Status
|
||||
statusDisplay := "[white:grey]unknown"
|
||||
if status == azrBuild.BuildStatusValues.InProgress {
|
||||
statusDisplay = "[white:blue]in progress"
|
||||
} else if status == azrBuild.BuildStatusValues.Cancelling {
|
||||
statusDisplay = "[white:orange]in cancelling"
|
||||
} else if (status == azrBuild.BuildStatusValues.Postponed) || (status == azrBuild.BuildStatusValues.NotStarted) {
|
||||
statusDisplay = "[white:blue]waiting"
|
||||
} else if status == azrBuild.BuildStatusValues.Completed {
|
||||
buildResult := *build.Result
|
||||
if buildResult == azrBuild.BuildResultValues.Succeeded {
|
||||
statusDisplay = "[white:green]succeeded"
|
||||
} else if buildResult == azrBuild.BuildResultValues.Failed {
|
||||
statusDisplay = "[white:red]failed"
|
||||
} else if buildResult == azrBuild.BuildResultValues.Canceled {
|
||||
statusDisplay = "[white:darkgrey]cancelled"
|
||||
} else if buildResult == azrBuild.BuildResultValues.PartiallySucceeded {
|
||||
statusDisplay = "[white:magenta]partially"
|
||||
}
|
||||
}
|
||||
|
||||
result += fmt.Sprintf("%s[-:-:-] #%s %s (%s) \n", statusDisplay, num, branch, reason)
|
||||
}
|
||||
|
||||
if result == "" {
|
||||
result = "no builds found"
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
42
modules/azuredevops/example-conf.yml
Normal file
42
modules/azuredevops/example-conf.yml
Normal file
@@ -0,0 +1,42 @@
|
||||
wtf:
|
||||
colors:
|
||||
# background: black
|
||||
# foreground: blue
|
||||
border:
|
||||
focusable: darkslateblue
|
||||
focused: orange
|
||||
normal: gray
|
||||
checked: yellow
|
||||
highlight:
|
||||
fore: black
|
||||
back: gray
|
||||
rows:
|
||||
even: yellow
|
||||
odd: white
|
||||
grid:
|
||||
# How _wide_ the columns are, in terminal characters. In this case we have
|
||||
# four columns, each of which are 35 characters wide.
|
||||
# columns: [50, ]
|
||||
# How _high_ the rows are, in terminal lines. In this case we have four rows
|
||||
# that support ten line of text and one of four.
|
||||
# rows: [50]
|
||||
refreshInterval: 1
|
||||
openFileUtil: "open"
|
||||
mods:
|
||||
azuredevops:
|
||||
type: azuredevops
|
||||
title: "💻"
|
||||
enabled: true
|
||||
position:
|
||||
top: 0
|
||||
left: 0
|
||||
height: 3
|
||||
width: 3
|
||||
refreshInterval: 1
|
||||
labelColor: lightblue # title label color (optional / default: white)
|
||||
apiToken: "mysecret api token" # api key (required)
|
||||
orgUrl: "https://dev.azure.com/myawesomecompany/" # url to your azure devops project (required)
|
||||
prjectName: "the awesome project" # name of your project (required)
|
||||
maxRows: 3 #max rows to show (optional / default 3)
|
||||
|
||||
|
||||
32
modules/azuredevops/settings.go
Normal file
32
modules/azuredevops/settings.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package azuredevops
|
||||
|
||||
import (
|
||||
"github.com/olebedev/config"
|
||||
"github.com/wtfutil/wtf/cfg"
|
||||
)
|
||||
|
||||
const defaultTitle = "azuredevops"
|
||||
|
||||
// Settings defines the configuration options for this module
|
||||
type Settings struct {
|
||||
common *cfg.Common
|
||||
labelColor string
|
||||
apiToken string
|
||||
orgURL string
|
||||
projectName string
|
||||
maxRows int
|
||||
}
|
||||
|
||||
// NewSettingsFromYAML creates and returns an instance of Settings with configuration options populated
|
||||
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
|
||||
settings := Settings{
|
||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig),
|
||||
labelColor: ymlConfig.UString("labelColor", "white"),
|
||||
apiToken: ymlConfig.UString("apiToken", "api token not specified"),
|
||||
orgURL: ymlConfig.UString("orgURL", "org url not specified"),
|
||||
projectName: ymlConfig.UString("projectName", "project name not specified"),
|
||||
maxRows: ymlConfig.UInt("maxRows", 3),
|
||||
}
|
||||
|
||||
return &settings
|
||||
}
|
||||
66
modules/azuredevops/widget.go
Normal file
66
modules/azuredevops/widget.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package azuredevops
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
azr "github.com/microsoft/azure-devops-go-api/azuredevops"
|
||||
azrBuild "github.com/microsoft/azure-devops-go-api/azuredevops/build"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/view"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
view.TextWidget
|
||||
cli *azrBuild.Client
|
||||
settings *Settings
|
||||
displayBuffer string
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: view.NewTextWidget(app, settings.common, false),
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.View.SetScrollable(true)
|
||||
connection := azr.NewPatConnection(settings.orgURL, settings.apiToken)
|
||||
ctx := context.Background()
|
||||
|
||||
cli, err := azrBuild.NewClient(ctx, connection)
|
||||
if err != nil {
|
||||
widget.displayBuffer = errors.Wrap(err, "could not create client 2").Error()
|
||||
} else {
|
||||
widget.cli = cli
|
||||
widget.ctx = ctx
|
||||
}
|
||||
|
||||
widget.refreshDisplayBuffer()
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
widget.refreshDisplayBuffer()
|
||||
widget.Redraw(widget.display)
|
||||
}
|
||||
|
||||
func (widget *Widget) display() (string, string, bool) {
|
||||
return widget.CommonSettings().Title, widget.displayBuffer, true
|
||||
}
|
||||
|
||||
func (widget *Widget) refreshDisplayBuffer() {
|
||||
if widget.cli == nil {
|
||||
return
|
||||
}
|
||||
|
||||
widget.displayBuffer = ""
|
||||
|
||||
widget.displayBuffer += fmt.Sprintf("[%s::bul] build status - %s\n",
|
||||
widget.settings.labelColor,
|
||||
widget.settings.projectName)
|
||||
|
||||
widget.displayBuffer += widget.getBuildStats()
|
||||
}
|
||||
Reference in New Issue
Block a user