Skip to content

Commit 1b911b2

Browse files
committed
v0.9.60-beta - added context support
1 parent 800e45b commit 1b911b2

12 files changed

+127
-26
lines changed

bitbucket.go

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

3+
import "context"
4+
35
type users interface {
46
Get(username string) (*User, error)
57
Followers(username string) (interface{}, error)
@@ -158,6 +160,12 @@ type RepositoryOptions struct {
158160
HasIssues string `json:"has_issues"`
159161
HasWiki string `json:"has_wiki"`
160162
Project string `json:"project"`
163+
ctx context.Context
164+
}
165+
166+
func (ro *RepositoryOptions) WithContext(ctx context.Context) *RepositoryOptions {
167+
ro.ctx = ctx
168+
return ro
161169
}
162170

163171
type RepositoryForkOptions struct {
@@ -174,6 +182,12 @@ type RepositoryForkOptions struct {
174182
HasIssues string `json:"has_issues"`
175183
HasWiki string `json:"has_wiki"`
176184
Project string `json:"project"`
185+
ctx context.Context
186+
}
187+
188+
func (fo *RepositoryForkOptions) WithContext(ctx context.Context) *RepositoryForkOptions {
189+
fo.ctx = ctx
190+
return fo
177191
}
178192

179193
type RepositoryFilesOptions struct {
@@ -200,6 +214,12 @@ type RepositoryBlobWriteOptions struct {
200214
Author string `json:"author"`
201215
Message string `json:"message"`
202216
Branch string `json:"branch"`
217+
ctx context.Context
218+
}
219+
220+
func (ro *RepositoryBlobWriteOptions) WithContext(ctx context.Context) *RepositoryBlobWriteOptions {
221+
ro.ctx = ctx
222+
return ro
203223
}
204224

205225
// RepositoryRefOptions represents the options for describing a repository's refs (i.e.
@@ -285,6 +305,12 @@ type PullRequestsOptions struct {
285305
States []string `json:"states"`
286306
Query string `json:"query"`
287307
Sort string `json:"sort"`
308+
ctx context.Context
309+
}
310+
311+
func (po *PullRequestsOptions) WithContext(ctx context.Context) *PullRequestsOptions {
312+
po.ctx = ctx
313+
return po
288314
}
289315

290316
type PullRequestCommentOptions struct {
@@ -293,6 +319,12 @@ type PullRequestCommentOptions struct {
293319
PullRequestID string `json:"id"`
294320
Content string `json:"content"`
295321
CommentId string `json:"-"`
322+
ctx context.Context
323+
}
324+
325+
func (pco *PullRequestCommentOptions) WithContext(ctx context.Context) *PullRequestCommentOptions {
326+
pco.ctx = ctx
327+
return pco
296328
}
297329

298330
type IssuesOptions struct {
@@ -311,6 +343,12 @@ type IssuesOptions struct {
311343
Priority string `json:"priority"`
312344
Version string `json:"version"`
313345
Assignee string `json:"assignee"`
346+
ctx context.Context
347+
}
348+
349+
func (io *IssuesOptions) WithContext(ctx context.Context) *IssuesOptions {
350+
io.ctx = ctx
351+
return io
314352
}
315353

316354
type IssueCommentsOptions struct {
@@ -342,6 +380,12 @@ type CommitsOptions struct {
342380
Exclude string `json:"exclude"`
343381
CommentID string `json:"comment_id"`
344382
Page *int `json:"page"`
383+
ctx context.Context
384+
}
385+
386+
func (cm *CommitsOptions) WithContext(ctx context.Context) *CommitsOptions {
387+
cm.ctx = ctx
388+
return cm
345389
}
346390

347391
type CommitStatusOptions struct {
@@ -363,6 +407,12 @@ type BranchRestrictionsOptions struct {
363407
FullSlug string `json:"full_slug"`
364408
Name string `json:"name"`
365409
Value interface{} `json:"value"`
410+
ctx context.Context
411+
}
412+
413+
func (b *BranchRestrictionsOptions) WithContext(ctx context.Context) *BranchRestrictionsOptions {
414+
b.ctx = ctx
415+
return b
366416
}
367417

368418
type DiffOptions struct {
@@ -393,6 +443,12 @@ type WebhooksOptions struct {
393443
Url string `json:"url"`
394444
Active bool `json:"active"`
395445
Events []string `json:"events"` // EX: {'repo:push','issue:created',..} REF: https://bit.ly/3FjRHHu
446+
ctx context.Context
447+
}
448+
449+
func (wo *WebhooksOptions) WithContext(ctx context.Context) *WebhooksOptions {
450+
wo.ctx = ctx
451+
return wo
396452
}
397453

398454
type RepositoryPipelineOptions struct {
@@ -424,6 +480,12 @@ type RepositoryPipelineVariableOptions struct {
424480
Key string `json:"key"`
425481
Value string `json:"value"`
426482
Secured bool `json:"secured"`
483+
ctx context.Context
484+
}
485+
486+
func (rpvo *RepositoryPipelineVariableOptions) WithContext(ctx context.Context) *RepositoryPipelineVariableOptions {
487+
rpvo.ctx = ctx
488+
return rpvo
427489
}
428490

429491
type RepositoryPipelineVariableDeleteOptions struct {
@@ -455,6 +517,12 @@ type DownloadsOptions struct {
455517
RepoSlug string `json:"repo_slug"`
456518
FilePath string `json:"filepath"`
457519
FileName string `json:"filename"`
520+
ctx context.Context
521+
}
522+
523+
func (do *DownloadsOptions) WithContext(ctx context.Context) *DownloadsOptions {
524+
do.ctx = ctx
525+
return do
458526
}
459527

460528
type PageRes struct {
@@ -512,6 +580,12 @@ type RepositoryEnvironmentOptions struct {
512580
Name string `json:"name"`
513581
EnvironmentType RepositoryEnvironmentTypeOption `json:"environment_type"`
514582
Rank int `json:"rank"`
583+
ctx context.Context
584+
}
585+
586+
func (reo *RepositoryEnvironmentOptions) WithContext(ctx context.Context) *RepositoryEnvironmentOptions {
587+
reo.ctx = ctx
588+
return reo
515589
}
516590

517591
type RepositoryEnvironmentDeleteOptions struct {
@@ -539,6 +613,12 @@ type RepositoryDeploymentVariableOptions struct {
539613
Key string `json:"key"`
540614
Value string `json:"value"`
541615
Secured bool `json:"secured"`
616+
ctx context.Context
617+
}
618+
619+
func (rdvo *RepositoryDeploymentVariableOptions) WithContext(ctx context.Context) *RepositoryDeploymentVariableOptions {
620+
rdvo.ctx = ctx
621+
return rdvo
542622
}
543623

544624
type RepositoryDeploymentVariableDeleteOptions struct {
@@ -554,4 +634,10 @@ type DeployKeyOptions struct {
554634
Id int `json:"id"`
555635
Label string `json:"label"`
556636
Key string `json:"key"`
637+
ctx context.Context
638+
}
639+
640+
func (dk *DeployKeyOptions) WithContext(ctx context.Context) *DeployKeyOptions {
641+
dk.ctx = ctx
642+
return dk
557643
}

branchrestrictions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (*BranchRestr
2626
return nil, err
2727
}
2828
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
29-
response, err := b.c.execute("POST", urlStr, data)
29+
response, err := b.c.executeWithContext("POST", urlStr, data, bo.ctx)
3030
if err != nil {
3131
return nil, err
3232
}

client.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ func (c *Client) executeRaw(method string, urlStr string, text string) (io.ReadC
231231
}
232232

233233
func (c *Client) execute(method string, urlStr string, text string) (interface{}, error) {
234+
return c.executeWithContext(method, urlStr, text, context.Background())
235+
}
236+
237+
func (c *Client) executeWithContext(method string, urlStr string, text string, ctx context.Context) (interface{}, error) {
234238
body := strings.NewReader(text)
235239
req, err := http.NewRequest(method, urlStr, body)
236240
if err != nil {
@@ -239,7 +243,9 @@ func (c *Client) execute(method string, urlStr string, text string) (interface{}
239243
if text != "" {
240244
req.Header.Set("Content-Type", "application/json")
241245
}
242-
246+
if ctx != nil {
247+
req.WithContext(ctx)
248+
}
243249
c.authenticateRequest(req)
244250
result, err := c.doRequest(req, false)
245251
if err != nil {
@@ -279,7 +285,7 @@ func (c *Client) executePaginated(method string, urlStr string, text string, pag
279285
return result, nil
280286
}
281287

282-
func (c *Client) executeFileUpload(method string, urlStr string, filePath string, fileName string, fieldname string, params map[string]string) (interface{}, error) {
288+
func (c *Client) executeFileUpload(method string, urlStr string, filePath string, fileName string, fieldname string, params map[string]string, ctx context.Context) (interface{}, error) {
283289
fileReader, err := os.Open(filePath)
284290
if err != nil {
285291
return nil, err
@@ -317,7 +323,9 @@ func (c *Client) executeFileUpload(method string, urlStr string, filePath string
317323
}
318324
// Don't forget to set the content type, this will contain the boundary.
319325
req.Header.Set("Content-Type", w.FormDataContentType())
320-
326+
if ctx != nil {
327+
req.WithContext(ctx)
328+
}
321329
c.authenticateRequest(req)
322330
return c.doRequest(req, true)
323331

commits.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (cm *Commits) GetCommitStatus(cmo *CommitsOptions, commitStatusKey string)
4242

4343
func (cm *Commits) GiveApprove(cmo *CommitsOptions) (interface{}, error) {
4444
urlStr := cm.c.requestUrl("/repositories/%s/%s/commit/%s/approve", cmo.Owner, cmo.RepoSlug, cmo.Revision)
45-
return cm.c.execute("POST", urlStr, "")
45+
return cm.c.executeWithContext("POST", urlStr, "", cmo.ctx)
4646
}
4747

4848
func (cm *Commits) RemoveApprove(cmo *CommitsOptions) (interface{}, error) {
@@ -56,7 +56,7 @@ func (cm *Commits) CreateCommitStatus(cmo *CommitsOptions, cso *CommitStatusOpti
5656
if err != nil {
5757
return nil, err
5858
}
59-
return cm.c.execute("POST", urlStr, string(data))
59+
return cm.c.executeWithContext("POST", urlStr, string(data), cmo.ctx)
6060
}
6161

6262
func (cm *Commits) buildCommitsQuery(include, exclude string) string {

deploykeys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (dk *DeployKeys) Create(opt *DeployKeyOptions) (*DeployKey, error) {
5252
return nil, err
5353
}
5454
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys", opt.Owner, opt.RepoSlug)
55-
response, err := dk.c.execute("POST", urlStr, data)
55+
response, err := dk.c.executeWithContext("POST", urlStr, data, opt.ctx)
5656
if err != nil {
5757
return nil, err
5858
}

downloads.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Downloads struct {
66

77
func (dl *Downloads) Create(do *DownloadsOptions) (interface{}, error) {
88
urlStr := dl.c.requestUrl("/repositories/%s/%s/downloads", do.Owner, do.RepoSlug)
9-
return dl.c.executeFileUpload("POST", urlStr, do.FilePath, do.FileName, "files", make(map[string]string))
9+
return dl.c.executeFileUpload("POST", urlStr, do.FilePath, do.FileName, "files", make(map[string]string), do.ctx)
1010
}
1111

1212
func (dl *Downloads) List(do *DownloadsOptions) (interface{}, error) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/ktrysmt/go-bitbucket
1+
module github.com/devtron-labs/go-bitbucket
22

33
go 1.14
44

issues.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (p *Issues) Create(io *IssuesOptions) (interface{}, error) {
6565
return nil, err
6666
}
6767
urlStr := p.c.requestUrl("/repositories/%s/%s/issues", io.Owner, io.RepoSlug)
68-
return p.c.execute("POST", urlStr, data)
68+
return p.c.executeWithContext("POST", urlStr, data, io.ctx)
6969
}
7070

7171
func (p *Issues) GetVote(io *IssuesOptions) (bool, interface{}, error) {
@@ -198,7 +198,7 @@ func (p *Issues) CreateComment(ico *IssueCommentsOptions) (interface{}, error) {
198198
return nil, err
199199
}
200200

201-
return p.c.execute("POST", urlStr, data)
201+
return p.c.executeWithContext("POST", urlStr, data, ico.ctx)
202202
}
203203

204204
func (p *Issues) GetComment(ico *IssueCommentsOptions) (interface{}, error) {
@@ -288,7 +288,7 @@ func (p *Issues) CreateChange(ico *IssueChangesOptions) (interface{}, error) {
288288

289289
fmt.Printf("data %s", data)
290290

291-
return p.c.execute("POST", url.String(), string(data))
291+
return p.c.executeWithContext("POST", url.String(), string(data), ico.ctx)
292292
}
293293

294294
func (p *Issues) GetChange(ico *IssueChangesOptions) (interface{}, error) {

project.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bitbucket
22

33
import (
4+
"context"
45
"encoding/json"
56

67
"github.com/mitchellh/mapstructure"
@@ -23,6 +24,12 @@ type ProjectOptions struct {
2324
Key string `json:"key"`
2425
Description string `json:"description"`
2526
IsPrivate bool `json:"is_private"`
27+
ctx context.Context
28+
}
29+
30+
func (po *ProjectOptions) WithContext(ctx context.Context) *ProjectOptions {
31+
po.ctx = ctx
32+
return po
2633
}
2734

2835
func (t *Workspace) GetProject(opt *ProjectOptions) (*Project, error) {
@@ -41,7 +48,7 @@ func (t *Workspace) CreateProject(opt *ProjectOptions) (*Project, error) {
4148
return nil, err
4249
}
4350
urlStr := t.c.requestUrl("/workspaces/%s/projects", opt.Owner)
44-
response, err := t.c.execute("POST", urlStr, data)
51+
response, err := t.c.executeWithContext("POST", urlStr, data, opt.ctx)
4552
if err != nil {
4653
return nil, err
4754
}

pullrequests.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (p *PullRequests) Create(po *PullRequestsOptions) (interface{}, error) {
1515
return nil, err
1616
}
1717
urlStr := p.c.requestUrl("/repositories/%s/%s/pullrequests/", po.Owner, po.RepoSlug)
18-
return p.c.execute("POST", urlStr, data)
18+
return p.c.executeWithContext("POST", urlStr, data, po.ctx)
1919
}
2020

2121
func (p *PullRequests) Update(po *PullRequestsOptions) (interface{}, error) {
@@ -104,7 +104,7 @@ func (p *PullRequests) Merge(po *PullRequestsOptions) (interface{}, error) {
104104
return nil, err
105105
}
106106
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/merge"
107-
return p.c.execute("POST", urlStr, data)
107+
return p.c.executeWithContext("POST", urlStr, data, po.ctx)
108108
}
109109

110110
func (p *PullRequests) Decline(po *PullRequestsOptions) (interface{}, error) {
@@ -113,12 +113,12 @@ func (p *PullRequests) Decline(po *PullRequestsOptions) (interface{}, error) {
113113
return nil, err
114114
}
115115
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/decline"
116-
return p.c.execute("POST", urlStr, data)
116+
return p.c.executeWithContext("POST", urlStr, data, po.ctx)
117117
}
118118

119119
func (p *PullRequests) Approve(po *PullRequestsOptions) (interface{}, error) {
120120
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/approve"
121-
return p.c.execute("POST", urlStr, "")
121+
return p.c.executeWithContext("POST", urlStr, "", po.ctx)
122122
}
123123

124124
func (p *PullRequests) UnApprove(po *PullRequestsOptions) (interface{}, error) {
@@ -128,7 +128,7 @@ func (p *PullRequests) UnApprove(po *PullRequestsOptions) (interface{}, error) {
128128

129129
func (p *PullRequests) RequestChanges(po *PullRequestsOptions) (interface{}, error) {
130130
urlStr := p.c.GetApiBaseURL() + "/repositories/" + po.Owner + "/" + po.RepoSlug + "/pullrequests/" + po.ID + "/request-changes"
131-
return p.c.execute("POST", urlStr, "")
131+
return p.c.executeWithContext("POST", urlStr, "", po.ctx)
132132
}
133133

134134
func (p *PullRequests) UnRequestChanges(po *PullRequestsOptions) (interface{}, error) {
@@ -143,7 +143,7 @@ func (p *PullRequests) AddComment(co *PullRequestCommentOptions) (interface{}, e
143143
}
144144

145145
urlStr := p.c.requestUrl("/repositories/%s/%s/pullrequests/%s/comments", co.Owner, co.RepoSlug, co.PullRequestID)
146-
return p.c.execute("POST", urlStr, data)
146+
return p.c.executeWithContext("POST", urlStr, data, co.ctx)
147147
}
148148

149149
func (p *PullRequests) UpdateComment(co *PullRequestCommentOptions) (interface{}, error) {

0 commit comments

Comments
 (0)