This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
91 lines (80 loc) · 1.89 KB
/
client.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
package ci
import (
"errors"
"log"
"github.com/ericaro/sbr/format"
)
var (
ErrUnreachable = errors.New("Cannot reach CI server")
)
type Client struct {
*format.ProtoClient
}
func NewClient(server string) Client {
return Client{format.NewClient(server)}
}
//turn a proto request into a response, error: dealing with protocol errors, and response error.
// return ErrUnreachable if this there was a communication error
// returns the protocol error otherwise
func (c Client) Request(req *format.Request) (resp *format.Response, err error) {
resp, err = c.Proto(req)
if err != nil {
log.Printf("%v: %v", ErrUnreachable, err)
return nil, ErrUnreachable
}
if resp.Error != nil {
return resp, errors.New(resp.GetError())
}
return resp, nil
}
//AddJob add a job by it's name, remote and branch
func (c Client) AddJob(jobname, remote, branch string) (err error) {
_, err = c.Request(
&format.Request{
Add: &format.AddRequest{
Id: &format.Jobid{
Name: &jobname,
Remote: &remote,
Branch: &branch,
},
},
})
return
}
//Remove a job by its jobname
func (c Client) RemoveJob(jobname string) (err error) {
_, err = c.Request(
&format.Request{
Remove: &format.RemoveRequest{
Jobname: &jobname,
},
})
return
}
//ListJobs return the list of all jobs.
//
// when 'refresh' (resp 'build') is true, the refresh (resp. build) log
// is sent too.
func (c Client) ListJobs(refresh, build bool) (jobs []*format.Job, err error) {
resp, err := c.Request(
&format.Request{
List: &format.ListRequest{
RefreshResult: &refresh,
BuildResult: &build,
},
})
if err != nil {
return nil, err
}
return resp.List.Jobs, nil
}
func (c Client) JobDetails(jobname string) (job *format.Job, err error) {
resp, err := c.Request(
&format.Request{
Log: &format.LogRequest{Jobname: &jobname},
})
if err != nil {
return nil, err
}
return resp.Log.Job, nil
}