-
Notifications
You must be signed in to change notification settings - Fork 3
/
jobs.go
143 lines (130 loc) · 4.02 KB
/
jobs.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
package main
/*
Package tjob - Test job management utility
Copyright (c) 2014 Ohmu Ltd.
Licensed under the Apache License, Version 2.0 (see LICENSE)
*/
import (
"fmt"
"github.com/ohmu/tjob/config"
"github.com/ohmu/tjob/jenkins"
"github.com/ohmu/tjob/pipeline"
"time"
)
func init() {
// TODO: long descs
globalParser().AddCommand("run", "Start a job", "Start a job",
&runJobCmd{})
globalParser().AddCommand("restart", "Restart jobs", "Restart jobs",
&restartJobCmd{})
globalParser().AddCommand("list", "List jobs", "List jobs",
&listJobsCmd{})
globalParser().AddCommand("remove", "Remove jobs", "Remove jobs",
&removeJobsCmd{})
}
func handleErrors(errors <-chan error) error {
count := 0
for err := range errors {
fmt.Println("error:", err)
}
if count > 0 {
return fmt.Errorf("there were %d errors", count)
}
return nil
}
type displayOptions struct {
ShowTestDetails bool `short:"d" long:"error-detail" description:"Show detailed test results"`
ShowTestTraceback bool `short:"s" long:"stack-trace" description:"Show failed test stack traces"`
ShowTestOutput bool `short:"p" long:"output" description:"Show failed test stdout/stderr"`
ShowBuildURL bool `short:"l" long:"url" description:"Show build URL link"`
ShowBuilder bool `long:"builder" description:"Show builder"`
ShowTags bool `long:"tags" description:"Show tags"`
ShowUser bool `short:"u" long:"username" description:"Show username"`
ShowCommitID bool `short:"c" long:"commit-id" description:"Show version-control commit-id"`
ShowBranch bool `short:"B" long:"branch" description:"Show version-control branch"`
NoSorting bool `long:"no-sort" description:"CSV format, no output sorting, saves memory in large queries"`
FailedTestSummary bool `long:"failed-summary" description:"Show a summary of failed test counts"`
SinceDuration time.Duration `long:"since" description:"Show only builds started since X duration ago"` // TODO: better desc
}
type remoteJobQuery struct {
pipeline.Node
conf *config.Config
flags *filterFlags
Input chan *config.Job
Output chan *config.Job
}
func (node *remoteJobQuery) Run() error {
defer close(node.Output)
for runnerName, runner := range node.conf.Runners {
jenk := jenkins.MakeJenkins(runnerName, runner.URL,
runner.Insecure, nil) // JobCache not needed here
jobs, err := jenk.QueryJobs()
if err != nil {
return node.AbortWithError(err)
}
for _, jobName := range jobs {
// TODO: filterJob is only created for filtering, something better?
filterJob := config.Job{
Runner: runnerName, JobName: jobName,
BuildNumber: "", Options: map[string]string{},
Tags: []string{}}
matched, err := filterByJobName(node.flags, &filterJob)
if err != nil {
return node.AbortWithError(err)
} else if !matched {
continue
}
// TODO: concurrent requests, channels
builds, err := jenk.QueryJobBuilds(jobName)
if err != nil {
return node.AbortWithError(err)
}
for _, buildNumber := range builds {
select {
case <-node.AbortChannel():
return nil
case node.Output <- &config.Job{
Runner: runnerName, JobName: jobName,
BuildNumber: buildNumber, Options: map[string]string{},
Tags: []string{}}:
}
}
}
}
return nil
}
type configJobSender struct {
pipeline.Node
conf *config.Config
Input chan *config.Job
Output chan *config.Job
}
func (node *configJobSender) Run() error {
defer close(node.Output)
for _, job := range node.conf.Jobs {
select {
case <-node.AbortChannel():
return nil
case node.Output <- job:
}
}
return nil
}
type jobCopier struct {
pipeline.Node
Options map[string]string
Tags []string
Input chan *JobStatus
Output chan *config.Job
}
func (node *jobCopier) Run() error {
defer close(node.Output)
for oldJob := range node.Input {
select {
case <-node.AbortChannel():
return nil
case node.Output <- oldJob.Copy(node.Options, node.Tags):
}
}
return nil
}