forked from ktrysmt/go-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipelines.go
133 lines (112 loc) · 3.14 KB
/
pipelines.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
package bitbucket
import (
"encoding/json"
"fmt"
"io"
"net/url"
)
type Pipelines struct {
c *Client
}
func (p *Pipelines) List(po *PipelinesOptions) (interface{}, error) {
urlStr := p.c.requestUrl("/repositories/%s/%s/pipelines/", po.Owner, po.RepoSlug)
if po.Query != "" {
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := parsed.Query()
query.Set("q", po.Query)
parsed.RawQuery = query.Encode()
urlStr = parsed.String()
}
if po.Sort != "" {
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := parsed.Query()
query.Set("sort", po.Sort)
parsed.RawQuery = query.Encode()
urlStr = parsed.String()
}
if po.Page != 0 {
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := parsed.Query()
query.Set("page", fmt.Sprint(po.Page))
parsed.RawQuery = query.Encode()
urlStr = parsed.String()
}
return p.c.executePaginated("GET", urlStr, "", nil)
}
func (p *Pipelines) Get(po *PipelinesOptions) (interface{}, error) {
urlStr := p.c.requestUrl("/repositories/%s/%s/pipelines/%s", po.Owner, po.RepoSlug, po.IDOrUuid)
return p.c.execute("GET", urlStr, "")
}
func (p *Pipelines) ListSteps(po *PipelinesOptions) (interface{}, error) {
urlStr := p.c.requestUrl("/repositories/%s/%s/pipelines/%s/steps/", po.Owner, po.RepoSlug, po.IDOrUuid)
if po.Query != "" {
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := parsed.Query()
query.Set("q", po.Query)
parsed.RawQuery = query.Encode()
urlStr = parsed.String()
}
if po.Sort != "" {
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := parsed.Query()
query.Set("sort", po.Sort)
parsed.RawQuery = query.Encode()
urlStr = parsed.String()
}
if po.Page != 0 {
parsed, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := parsed.Query()
query.Set("page", fmt.Sprint(po.Page))
parsed.RawQuery = query.Encode()
urlStr = parsed.String()
}
return p.c.executePaginated("GET", urlStr, "", nil)
}
func (p *Pipelines) GetStep(po *PipelinesOptions) (interface{}, error) {
urlStr := p.c.requestUrl("/repositories/%s/%s/pipelines/%s/steps/%s", po.Owner, po.RepoSlug, po.IDOrUuid, po.StepUuid)
return p.c.execute("GET", urlStr, "")
}
func (p *Pipelines) GetLog(po *PipelinesOptions) (string, error) {
urlStr := p.c.requestUrl("/repositories/%s/%s/pipelines/%s/steps/%s/log", po.Owner, po.RepoSlug, po.IDOrUuid, po.StepUuid)
responseBody, err := p.c.executeRaw("GET", urlStr, "")
if err != nil {
return "", err
}
defer responseBody.Close()
rawBody, err := io.ReadAll(responseBody)
if err != nil {
return "", err
}
return string(rawBody), nil
}
func (p *Pipelines) TriggerPipeline(po *PipelinesOptions, body *TriggerPipelineRequestBody) (interface{}, error) {
urlStr := p.c.requestUrl("/repositories/%s/%s/pipelines/", po.Owner, po.RepoSlug)
b, err := json.Marshal(body)
if err != nil {
return "failed to parse body", err
}
data := string(b)
responseBody, err := p.c.execute("POST", urlStr, data)
if err != nil {
return "failed to trigger bitbucket pipeline", err
}
return responseBody, nil
}