Skip to content

Commit 3a322c6

Browse files
committed
add TriggerPipeline method
1 parent 31b02ab commit 3a322c6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

pipelines.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package bitbucket
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"io"
57
"io/ioutil"
68
"net/url"
79
)
@@ -113,3 +115,40 @@ func (p *Pipelines) GetLog(po *PipelinesOptions) (string, error) {
113115

114116
return string(rawBody), nil
115117
}
118+
119+
type BitbucketTrigerPipelineRequestBody struct {
120+
Target struct {
121+
RefType string `json:"ref_type"`
122+
Type string `json:"type"`
123+
RefName string `json:"ref_name"`
124+
Selector struct {
125+
Type string `json:"type"`
126+
Pattern string `json:"pattern"`
127+
} `json:"selector"`
128+
Variables string `json:"variables"`
129+
} `json:"target"`
130+
}
131+
132+
func (p *Pipelines) TriggerPipeline(po *PipelinesOptions, body *BitbucketTrigerPipelineRequestBody) (string, error) {
133+
urlStr := p.c.requestUrl("/repositories/%s/%s/pipelines/", po.Owner, po.RepoSlug)
134+
135+
b, err := json.Marshal(body)
136+
if err != nil {
137+
return "failed to parse body", err
138+
}
139+
data := string(b)
140+
141+
p.c.execute("POST", urlStr, data)
142+
responseBody, err := p.c.executeRaw("GET", urlStr, "")
143+
if err != nil {
144+
return "failed to trigger bitbucket pipeline", err
145+
}
146+
defer responseBody.Close()
147+
148+
rawBody, err := io.ReadAll(responseBody)
149+
if err != nil {
150+
return "", err
151+
}
152+
153+
return string(rawBody), nil
154+
}

0 commit comments

Comments
 (0)