|
1 | 1 | package bitbucket
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "fmt"
|
| 6 | + "io" |
5 | 7 | "io/ioutil"
|
6 | 8 | "net/url"
|
7 | 9 | )
|
@@ -113,3 +115,40 @@ func (p *Pipelines) GetLog(po *PipelinesOptions) (string, error) {
|
113 | 115 |
|
114 | 116 | return string(rawBody), nil
|
115 | 117 | }
|
| 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