Skip to content

Commit

Permalink
jenkins: show number of running jobs in "wait until jenkins is idle" …
Browse files Browse the repository at this point in the history
…command (#296)
  • Loading branch information
brainexe authored Mar 7, 2022
1 parent f055a63 commit 7afa723
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 134 deletions.
23 changes: 14 additions & 9 deletions command/jenkins/inform_idle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jenkins

import (
"context"
"fmt"
"time"

"github.com/bndr/gojenkins"
Expand Down Expand Up @@ -51,7 +52,8 @@ func (c *idleWatcherCommand) checkAllNodes(match matcher.Result, message msg.Mes
}

func (c *idleWatcherCommand) check(message msg.Message, nodeFilter func(node *gojenkins.Node) bool) {
if !c.hasRunningBuild(message, nodeFilter) {
buildCount := c.countRunningBuild(message, nodeFilter)
if buildCount == 0 {
c.AddReaction(doneReaction, message)
c.SendMessage(
message,
Expand All @@ -60,6 +62,11 @@ func (c *idleWatcherCommand) check(message msg.Message, nodeFilter func(node *go
return
}

c.SendMessage(
message,
fmt.Sprintf("There are %d builds running...", buildCount),
)

runningCommand := queue.AddRunningCommand(
message,
message.Text,
Expand All @@ -71,7 +78,7 @@ func (c *idleWatcherCommand) check(message msg.Message, nodeFilter func(node *go
defer timer.Stop()

for range timer.C {
if c.hasRunningBuild(message, nodeFilter) {
if c.countRunningBuild(message, nodeFilter) != 0 {
// still builds running...
continue
}
Expand All @@ -92,27 +99,25 @@ func (c *idleWatcherCommand) check(message msg.Message, nodeFilter func(node *go
}

// query all executors from jenkins with one request and check of any executor is busy
func (c *idleWatcherCommand) hasRunningBuild(ref msg.Ref, nodeFilter func(build *gojenkins.Node) bool) bool {
func (c *idleWatcherCommand) countRunningBuild(ref msg.Ref, nodeFilter func(build *gojenkins.Node) bool) int {
ctx := context.Background()
nodes, err := c.jenkins.GetAllNodes(ctx)
if err != nil {
c.ReplyError(ref, err)
return false
return 0
}

buildCount := 0
for _, node := range nodes {
if !nodeFilter(node) {
// current command is not interested in this node...
continue
}

if countBusyExecutors(node) > 0 {
// there is something running!
return true
}
buildCount += countBusyExecutors(node)
}

return false
return buildCount
}

func (c *idleWatcherCommand) GetHelp() []bot.Help {
Expand Down
1 change: 1 addition & 0 deletions command/jenkins/inform_idle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestInformIdle(t *testing.T) {
getNodeWithExecutors(0, 2, "swarm2"),
}, nil).Once()

mocks.AssertSlackMessage(slackClient, message, "There are 1 builds running...")
mocks.AssertReaction(slackClient, waitingReaction, message)
mocks.AssertRemoveReaction(slackClient, waitingReaction, message)
mocks.AssertSlackMessage(slackClient, message, "No job is running anymore")
Expand Down
27 changes: 14 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module github.com/innogames/slack-bot/v2
go 1.17

require (
github.com/alicebob/miniredis/v2 v2.17.0
github.com/andygrunwald/go-jira v1.15.0
github.com/andygrunwald/go-jira v1.15.1
github.com/aws/aws-sdk-go v1.43.12
github.com/bndr/gojenkins v1.1.0
github.com/brainexe/viper v1.8.2
github.com/gen2brain/dlgs v0.0.0-20211108104213-bade24837f0b
github.com/gfleury/go-bitbucket-v1 v0.0.0-20220301131131-8e7ed04b843e
github.com/go-redis/redis/v7 v7.4.1
github.com/google/go-github v17.0.0+incompatible
Expand All @@ -20,7 +21,7 @@ require (
github.com/slack-go/slack v0.10.2
github.com/stretchr/testify v1.7.0
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c
github.com/xanzy/go-gitlab v0.57.0
github.com/xanzy/go-gitlab v0.58.0
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
golang.org/x/text v0.3.7
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
Expand All @@ -30,40 +31,40 @@ require (
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/golang-jwt/jwt/v4 v4.3.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-hclog v0.12.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.8.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/trivago/tgo v1.0.7 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

require (
github.com/aws/aws-sdk-go v1.43.2
github.com/alicebob/miniredis/v2 v2.18.0
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gopherjs/gopherjs v0.0.0-20220221023154-0b2280d3ff96 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/spf13/afero v1.8.1 // indirect
github.com/spf13/viper v1.9.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/stretchr/objx v0.3.0 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
)
Loading

0 comments on commit 7afa723

Please sign in to comment.