-
Notifications
You must be signed in to change notification settings - Fork 42
/
client_impl.go
63 lines (51 loc) · 1.43 KB
/
client_impl.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
package client
import (
"context"
"net/http"
"strings"
"github.com/bndr/gojenkins"
"github.com/innogames/slack-bot/v2/bot/config"
)
// implementation of Client interface. proxies to gojenkins with additional handling for inner jenkins jobs.
type jenkinsClientImpl struct {
client *gojenkins.Jenkins
}
func createJenkinsClient(ctx context.Context, httpClient *http.Client, cfg config.Jenkins) (*jenkinsClientImpl, error) {
var jenkins *gojenkins.Jenkins
if cfg.Username == "" {
jenkins = gojenkins.CreateJenkins(
httpClient,
cfg.Host,
)
} else {
jenkins = gojenkins.CreateJenkins(
httpClient,
cfg.Host,
cfg.Username,
cfg.Password,
)
}
jenkinsClient, err := jenkins.Init(ctx)
if err != nil {
return nil, err
}
client := &jenkinsClientImpl{
client: jenkinsClient,
}
return client, nil
}
func (c *jenkinsClientImpl) GetJob(ctx context.Context, id string) (*gojenkins.Job, error) {
// split jobs id by "/"" to be able to access inner job
jobs := strings.Split(id, "/")
jobsCount := len(jobs)
if jobsCount > 1 {
return c.client.GetJob(ctx, jobs[jobsCount-1], jobs[:jobsCount-1]...)
}
return c.client.GetJob(ctx, id)
}
func (c *jenkinsClientImpl) BuildJob(ctx context.Context, name string, params map[string]string) (int64, error) {
return c.client.BuildJob(ctx, name, params)
}
func (c *jenkinsClientImpl) GetAllNodes(ctx context.Context) ([]*gojenkins.Node, error) {
return c.client.GetAllNodes(ctx)
}