Skip to content

Commit

Permalink
add: exclude jobname
Browse files Browse the repository at this point in the history
  • Loading branch information
bootjp committed Apr 26, 2021
1 parent 2bb69df commit b80a871
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
33 changes: 33 additions & 0 deletions detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"time"

"github.com/ashwanthkumar/slack-go-webhook"
Expand Down Expand Up @@ -40,6 +41,7 @@ func main() {
jenkinsPassword := os.Getenv("JENKINS_PASSWORD")
slackWebhookURL := os.Getenv("SLACK_WEBHOOK_URL")
slackUsername := os.Getenv("SLACK_USERNAME")
ignoreJobNamePat := os.Getenv("IGNORE_JOB_NAME_PATTERN")

// Versions lower than v0.0.5 have incorrect settings,
// so load with typo for compatibility
Expand All @@ -66,6 +68,21 @@ func main() {
log.Fatalln(err)
}

for _, job := range jobs {
fmt.Println(job.GetName())
}
if ignoreJobNamePat != "" {
jobs, err = ExcludeJobNamePattern(jobs, ignoreJobNamePat)
if err != nil {
log.Fatalln(err)
}
}
fmt.Println("--")

for _, job := range jobs {
fmt.Println(job.GetName())
}

detectFailJobs := DetectFailJobs(jobs)
summary := map[string][]*FailJob{}
for _, job := range detectFailJobs {
Expand Down Expand Up @@ -146,6 +163,22 @@ func JenkinsInit(url string, auth ...string) *gojenkins.Jenkins {

}

func ExcludeJobNamePattern(jobs []*gojenkins.Job, pattern string) ([]*gojenkins.Job, error) {
re, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}

filtered := make([]*gojenkins.Job, 0)
for _, job := range jobs {
if !re.MatchString(job.GetName()) {
filtered = append(filtered, job)
}
}

return filtered, nil
}

type FailJob struct {
JenkinsJob *gojenkins.Job
Err error
Expand Down
28 changes: 27 additions & 1 deletion detector_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "testing"
import (
"fmt"
"regexp"
"testing"
)

func TestIsConsecutiveFailJob(t *testing.T) {

Expand All @@ -9,3 +13,25 @@ func TestIsConsecutiveFailJob(t *testing.T) {
func TestIsOverHoursFailedJob(t *testing.T) {

}

func TestExcludeJobNamePattern(t *testing.T) {

jobs := []string{"aa_bn", "ccc", "d", "eeee", "e"}

re, err := regexp.Compile("e")
if err != nil {
t.Fatal(err)
}

filterd := make([]string, len(jobs))

for _, job := range jobs {
if !re.MatchString(job) {
filterd = append(filterd, job)
fmt.Println("un match", job)
}
}

fmt.Println(filterd)

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
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
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384
moul.io/http2curl v1.0.0 // indirect
)

Expand Down

0 comments on commit b80a871

Please sign in to comment.