-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitxjenkins.go
358 lines (291 loc) · 9.84 KB
/
gitxjenkins.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"github.com/google/go-github/github"
"fmt"
"github.com/bndr/gojenkins"
"github.com/anthonydahanne/stash"
"net/url"
"strings"
"bytes"
"gopkg.in/xmlpath.v1"
"gopkg.in/yaml.v2"
"io/ioutil"
"sort"
"time"
"log"
"html/template"
"os"
"bufio"
)
type Jenkins struct {
Name string
Url string
Username string
Password string
}
type Github struct {
Organization string
Username string
Password string
}
type Stash struct {
Prefix string
Url string
Username string
Password string
}
type Configuration struct {
Jenkins []Jenkins
Github []Github
Stash []Stash
}
type Job struct {
JenkinsName string
Name string
Url string
ScmUrl string
LastBuildStatus int
}
type GitRepository struct {
Project string
ProjectUrl string
Name string
Url string
ScmUrls []string
Jobs []Job
}
type ByProjectAndName []GitRepository
func (a ByProjectAndName) Len() int { return len(a) }
func (a ByProjectAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByProjectAndName) Less(i, j int) bool { return a[i].Project + a[i].Name < a[j].Project + a[j].Name}
func main() {
fmt.Printf("Loading configuration...\n")
configuration, err := readConfiguration();
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("Finished loading configuration.\n")
var allJobs []Job
var allRepos[]GitRepository
for _, jenkins := range configuration.Jenkins {
fmt.Printf("Loading jobs from %v...\n", jenkins.Name)
resultJobs, err := jenkinsParser(jenkins.Name, jenkins.Url, jenkins.Username, jenkins.Password)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("Finished loading jobs from %v\n", jenkins.Name)
allJobs = append(allJobs, resultJobs...)
}
for _, github := range configuration.Github {
fmt.Printf("Loading repos from %v...\n", github.Organization)
resultRepos, err := githubParser(github.Organization, github.Username, github.Password)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("Finished loading repos from %v\n", github.Organization)
allRepos = append(allRepos, resultRepos...)
}
for _, stash := range configuration.Stash {
fmt.Printf("Loading repos from %v...\n", stash.Url)
resultRepos, err := stashParser(stash.Url, stash.Prefix, stash.Username, stash.Password)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("Finished loading repos with project prefixed with %v, from %v", stash.Prefix, stash.Url)
allRepos = append(allRepos, resultRepos...)
}
sort.Sort(ByProjectAndName((allRepos)))
attachJobsToRepos(allRepos, allJobs)
printResultsAsText(allRepos)
printResultsAsHtml(allRepos, configuration)
}
func printResultsAsText(allRepos []GitRepository) {
fmt.Printf("Starting generating text output \n")
for _, repo := range allRepos {
if (len(repo.Jobs) > 0) {
jobsAsStrings := ""
for _, job := range repo.Jobs {
if (len(jobsAsStrings) > 0) {
jobsAsStrings += " and "
}
jobsAsStrings += job.Name + " at " + job.Url
}
fmt.Printf("%v/%v is built by %v \n", repo.Project, repo.Name, jobsAsStrings)
} else {
fmt.Printf("%v/%v is an orphan repo\n", repo.Project, repo.Name)
}
}
fmt.Printf("Text output finished\n\n")
}
func printResultsAsHtml(allRepos []GitRepository, configuration Configuration) {
fmt.Printf("Starting generating html file, output.html \n")
currentTime := time.Now().Format(time.RFC3339)
check := func(err error) {
if err != nil {
log.Fatal(err)
}
}
templateAsString,err := FSString(false, "/template.html")
check(err)
t, err := template.New("template").Parse(templateAsString)
type ConfigElement struct {
Name string
Url string
}
configElements := []ConfigElement{}
for _, jenkins := range configuration.Jenkins {
configElements = append(configElements, ConfigElement{jenkins.Username + " @ " + jenkins.Name, jenkins.Url})
}
for _, github := range configuration.Github {
configElements = append(configElements, ConfigElement{github.Organization, "https://github.com/" + github.Organization })
}
for _, stash := range configuration.Stash {
configElements = append(configElements, ConfigElement{stash.Prefix, stash.Url})
}
for _, repo := range allRepos {
if (len(repo.Jobs) > 0) {
jobsAsStrings := ""
for _, job := range repo.Jobs {
if (len(jobsAsStrings) > 0) {
jobsAsStrings += " and "
}
jobsAsStrings += job.Name + " at " + job.Url
}
}
}
data := struct {
Title string
Date string
ConfigElements []ConfigElement
ReposAndJobs []GitRepository
}{
Title: "GitXJenkins report",
Date: currentTime,
ConfigElements: configElements,
ReposAndJobs: allRepos,
}
f, err := os.Create("output.html")
check(err)
defer f.Close()
w := bufio.NewWriter(f)
err = t.Execute(w, data)
check(err)
w.Flush()
fmt.Printf("html file, output.html is generated\n")
}
func attachJobsToRepos(allRepos []GitRepository, allJobs []Job) {
for i, repo := range allRepos {
for _, job := range allJobs {
for _, repoScmUrl := range repo.ScmUrls {
if (job.ScmUrl == repoScmUrl) {
allRepos[i].Jobs = append(allRepos[i].Jobs, job)
}
}
}
}
}
func jenkinsParser(jenkinsName string, jenkinsURL string, username string, password string) ([]Job, error) {
var resultJobs []Job
var jenkins *gojenkins.Jenkins
var err error
if (username != "") {
jenkins, err = gojenkins.CreateJenkins(jenkinsURL, username, password).Init()
} else {
jenkins, err = gojenkins.CreateJenkins(jenkinsURL).Init()
}
if err != nil {
return nil, err
}
fmt.Printf("%s: Connected\n", jenkinsName)
status, err := jenkins.Poll();
if(status != 200) {
log.Fatalf("Jenkins replied with status %v ; check your credentials for %v", status, jenkinsURL)
return nil, err
}
fmt.Printf("%s: Polled\n", jenkinsName)
jobs, err := jenkins.GetAllJobs();
if err != nil {
return nil, err
}
fmt.Printf("%s: GetAllJobs done (%d jobs)\n", jenkinsName, len(jobs))
path := xmlpath.MustCompile("//scm/userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/url")
for _, job := range jobs {
jobConfig, err := job.GetConfig();
if err != nil {
fmt.Printf("error: %v", err)
continue
}
b := bytes.NewBufferString(jobConfig)
root, err := xmlpath.Parse(b)
if err != nil {
fmt.Printf("error: %v", err)
continue
}
if scmUrl, ok := path.String(root); ok {
build, _ := job.GetLastBuild();
var lastBuildStatus int;
if(build == nil) {
lastBuildStatus = 2;
} else {
if(build.IsGood()) {
lastBuildStatus = 0;
} else {
lastBuildStatus = 1;
}
}
resultJobs = append(resultJobs, Job{JenkinsName: jenkinsName, Name:job.GetName(), Url:job.GetDetails().URL, ScmUrl:scmUrl, LastBuildStatus:lastBuildStatus})
}
}
return resultJobs, nil
}
func stashParser(stashBaseUrl string, projectPrefix string, username string, password string) ([]GitRepository, error) {
var resultGitRepositories []GitRepository
stashUrl, err := url.Parse(stashBaseUrl)
if err != nil {
return resultGitRepositories, err
}
stashClient := stash.NewClient(username, password, stashUrl)
repository, err := stashClient.GetRepositories()
if err != nil {
return resultGitRepositories, err
}
for _, element := range repository {
if ( strings.HasPrefix(element.Project.Key, projectPrefix)) {
var urls []string
urls = append(urls, element.SshUrl())
repoUrl := stashBaseUrl + "/projects/" + element.Project.Key + "/repos/" + element.Name + "/browse"
resultGitRepositories = append(resultGitRepositories, GitRepository{Project: element.Project.Key, Name: element.Name, Url: repoUrl, ScmUrls:urls})
}
}
return resultGitRepositories, nil
}
func githubParser(githubOrganization string, username string, password string) ([]GitRepository, error) {
var resultGitRepositories []GitRepository
client := github.NewClient(nil)
orgs, _, _ := client.Repositories.List(githubOrganization, nil)
for _, element := range orgs {
var urls []string
urls = append(urls, *element.GitURL)
urls = append(urls, *element.CloneURL)
url := strings.Replace(*element.URL, "api.", "", -1)
url = strings.Replace(url, "repos/", "", -1)
resultGitRepositories = append(resultGitRepositories, GitRepository{Project:githubOrganization, ProjectUrl:"https://github.com/" + githubOrganization, Name:*element.Name, Url:url, ScmUrls:urls})
}
return resultGitRepositories, nil
}
func readConfiguration() (Configuration, error) {
t := Configuration{}
dat, err := ioutil.ReadFile("config.yml")
if err != nil {
return t, err
}
err = yaml.Unmarshal(dat, &t)
if err != nil {
return t, err
}
return t, nil
}