Skip to content

Commit

Permalink
Merge pull request #7 from bootjp/develop2
Browse files Browse the repository at this point in the history
add logging
  • Loading branch information
bootjp authored Mar 3, 2021
2 parents 37c5d5c + 93d64a8 commit 56c90da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
40 changes: 29 additions & 11 deletions detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"time"

"github.com/ashwanthkumar/slack-go-webhook"

"github.com/bndr/gojenkins"
"github.com/pkg/errors"
)

const JenkinsResultFail = "FAILURE"
Expand All @@ -21,8 +21,12 @@ const ReasonOverHoursFailedJob = "ErrOverHoursFailedJob"
const ReasonConsecutiveFailJob = "ConsecutiveFailJob"
const ReasonJenkinsError = "jenkins error"

var logger *log.Logger

func main() {

logger = log.New(os.Stderr, "", log.Ldate|log.Ltime)

url := flag.String("url", "", "jenkins server url")
flag.Parse()
if *url == "" {
Expand Down Expand Up @@ -61,38 +65,38 @@ func main() {
summary[job.Reason] = append(summary[job.Reason], job)
}

var errors string
var errs string
for reason, failJobs := range summary {

switch reason {
case ReasonJenkinsError:
errors += fmt.Sprintln("Jobs whose status could not be confirmed due to a Jenkins error")
errs += fmt.Sprintln("Jobs whose status could not be confirmed due to a Jenkins error")
case ReasonOverHoursFailedJob:
errors += fmt.Sprintln("Jobs that have been failed for over an hour")
errs += fmt.Sprintln("Jobs that have been failed for over an hour")
case ReasonConsecutiveFailJob:
errors += fmt.Sprintln("Jobs that have failed more than once in a row")
errs += fmt.Sprintln("Jobs that have failed more than once in a row")
}

for _, failJob := range failJobs {
if failJob.Err != nil {
errors += fmt.Sprintln(failJob.Err)
errs += fmt.Sprintln(failJob.Err)
}

errors += fmt.Sprintln(failJob.JenkinsJob.GetName())
errs += fmt.Sprintln(failJob.JenkinsJob.GetName())

if lfb, err := failJob.JenkinsJob.GetLastFailedBuild(); err == nil && lfb != nil {
errors += fmt.Sprintln(lfb.GetUrl())
errs += fmt.Sprintln(lfb.GetUrl())
}
}

errors += fmt.Sprintln("---")
errs += fmt.Sprintln("---")
}

fmt.Println(errors)
fmt.Println(errs)

if slackWebhookURL != "" && len(detectFailJobs) > 0 {
payload := slack.Payload{
Text: errors,
Text: errs,
Username: "jenkins_consecutive_fail_detector",
IconEmoji: ":warning:",
}
Expand Down Expand Up @@ -156,6 +160,9 @@ func DetectFailJobs(jobs []*gojenkins.Job) []*FailJob {
lastBuild, err := job.GetLastBuild()
if err != nil {

logger.Println("got err GetLastBuild")
logger.Println(errors.WithStack(err))

ej := &FailJob{
JenkinsJob: job,
Err: err,
Expand Down Expand Up @@ -223,6 +230,9 @@ func DetectFailJobs(jobs []*gojenkins.Job) []*FailJob {
func IsOverHoursFailedJob(job *gojenkins.Job) (bool, error) {
latestBuild, err := job.GetLastBuild()
if err != nil {
logger.Println("got err GetLastBuild")
logger.Println(err)

return false, err
}

Expand All @@ -236,11 +246,16 @@ func IsConsecutiveFailJob(job *gojenkins.Job) (bool, error) {
buildIds, err := job.GetAllBuildIds()

if err != nil {
logger.Println("got err GetAllBuildIds")
logger.Println(errors.WithStack(err))

return false, err
}

lastBuild, err := job.GetLastBuild()
if err != nil {
logger.Println("got err GetLastBuild")
logger.Println(errors.WithStack(err))
return false, err
}

Expand All @@ -252,6 +267,9 @@ func IsConsecutiveFailJob(job *gojenkins.Job) (bool, error) {

build, err := job.GetBuild(buildId.Number)
if err != nil {
logger.Println("got err GetBuild")
logger.Println(errors.WithStack(err))

return false, err
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/bndr/gojenkins v1.0.1
github.com/elazarl/goproxy v0.0.0-20201021153353-00ad82a08272 // indirect
github.com/parnurzeal/gorequest v0.2.16 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
Expand Down

0 comments on commit 56c90da

Please sign in to comment.