-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (110 loc) · 3.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/bndr/gojenkins"
"github.com/go-redis/redis"
"github.com/google/go-github/github"
flags "github.com/jessevdk/go-flags"
"golang.org/x/oauth2"
)
const (
gitHubOwner = "revdotcom"
gitHubRepo = "revdotcom"
noColor = "\033[0m"
colorGreen = "\033[0;32m"
colorRed = "\033[0;31m"
)
type opts struct {
GithubToken string `short:"t" long:"token" description:"Github Api Token" required:"true"`
JenkinsUsername string `short:"u" long:"user" description:"Jenkins username" required:"true"`
JenkinsPassword string `short:"p" long:"password" description:"Jenkins password" required:"true"`
}
func getGitHubClient(gitHubToken string) (*github.Client, *context.Context) {
ctx := context.Background()
tokenService := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: gitHubToken},
)
tokenClient := oauth2.NewClient(ctx, tokenService)
ghClient := github.NewClient(tokenClient)
return ghClient, &ctx
}
func getJenkinsClient(jenkinsUsername, jenkinsPassword string) *gojenkins.Jenkins {
client := gojenkins.CreateJenkins(nil, "https://ci.rev.com", jenkinsUsername, jenkinsPassword)
if _, err := client.Init(); err != nil {
panic("Something went wrong")
}
return client
}
func getRedisClient() *redis.Client {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
if _, err := client.Ping().Result(); err != nil {
panic("can't connect to Redis")
}
return client
}
func getRedisKey(sha string) string {
return "github:" + gitHubRepo + ":" + sha
}
func main() {
args := opts{}
parser := flags.NewParser(&args, flags.Default)
if _, err := parser.Parse(); err != nil {
os.Exit(1)
}
ghClient, ctx := getGitHubClient(args.GithubToken)
jenkinsClient := getJenkinsClient(args.JenkinsUsername, args.JenkinsPassword)
redisClient := getRedisClient()
jobs, errj := jenkinsClient.GetAllJobs()
if errj != nil {
panic("can't fetch jobs")
}
var waitGroup sync.WaitGroup
waitGroup.Add(len(jobs))
defer waitGroup.Wait()
for _, job := range jobs {
go func(j gojenkins.Job) {
defer waitGroup.Done()
jobName := j.GetName()
if strings.HasPrefix(jobName, "Rev.com-build-feature_") {
build, _ := j.GetLastBuild()
sha := build.GetRevision()
var status string
if build.IsRunning() {
status = "pending"
} else {
if build.IsGood() {
status = "success"
} else {
status = "failure"
}
}
buildURL := build.GetUrl() + "console" // Point to the console log directly
statusColor := colorGreen
if status != "success" {
statusColor = colorRed
}
rStatus, rErr := redisClient.Get(getRedisKey(sha)).Result()
if rErr != nil || rStatus != status {
repoStatus := github.RepoStatus{
State: &status,
TargetURL: &buildURL,
}
ghClient.Repositories.CreateStatus(*ctx, gitHubOwner, gitHubRepo, sha, &repoStatus)
redisClient.Set(getRedisKey(sha), status, time.Hour*24*14)
fmt.Printf("%vsha: %v with status: %v%v\n", noColor, sha, statusColor, status)
} else {
fmt.Printf("%vsha[Cache]: \033[1;30m%v %vwith status: %v%v\n", noColor, sha, noColor, statusColor, status)
}
}
}(*job)
}
}