Skip to content

Commit bfc0803

Browse files
authored
feat: Added context based execution to POST apis methods (#285)
1 parent 98da4fb commit bfc0803

11 files changed

+136
-37
lines changed

bitbucket.go

+86
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)
@@ -160,6 +162,12 @@ type RepositoryOptions struct {
160162
HasIssues string `json:"has_issues"`
161163
HasWiki string `json:"has_wiki"`
162164
Project string `json:"project"`
165+
ctx context.Context
166+
}
167+
168+
func (ro *RepositoryOptions) WithContext(ctx context.Context) *RepositoryOptions {
169+
ro.ctx = ctx
170+
return ro
163171
}
164172

165173
type RepositoryForkOptions struct {
@@ -176,6 +184,12 @@ type RepositoryForkOptions struct {
176184
HasIssues string `json:"has_issues"`
177185
HasWiki string `json:"has_wiki"`
178186
Project string `json:"project"`
187+
ctx context.Context
188+
}
189+
190+
func (fo *RepositoryForkOptions) WithContext(ctx context.Context) *RepositoryForkOptions {
191+
fo.ctx = ctx
192+
return fo
179193
}
180194

181195
type RepositoryFilesOptions struct {
@@ -209,6 +223,12 @@ type RepositoryBlobWriteOptions struct {
209223
Author string `json:"author"`
210224
Message string `json:"message"`
211225
Branch string `json:"branch"`
226+
ctx context.Context
227+
}
228+
229+
func (ro *RepositoryBlobWriteOptions) WithContext(ctx context.Context) *RepositoryBlobWriteOptions {
230+
ro.ctx = ctx
231+
return ro
212232
}
213233

214234
// RepositoryRefOptions represents the options for describing a repository's refs (i.e.
@@ -294,6 +314,12 @@ type PullRequestsOptions struct {
294314
States []string `json:"states"`
295315
Query string `json:"query"`
296316
Sort string `json:"sort"`
317+
ctx context.Context
318+
}
319+
320+
func (po *PullRequestsOptions) WithContext(ctx context.Context) *PullRequestsOptions {
321+
po.ctx = ctx
322+
return po
297323
}
298324

299325
type PullRequestCommentOptions struct {
@@ -303,6 +329,12 @@ type PullRequestCommentOptions struct {
303329
Content string `json:"content"`
304330
CommentId string `json:"-"`
305331
Parent *int `json:"parent"`
332+
ctx context.Context
333+
}
334+
335+
func (pco *PullRequestCommentOptions) WithContext(ctx context.Context) *PullRequestCommentOptions {
336+
pco.ctx = ctx
337+
return pco
306338
}
307339

308340
type IssuesOptions struct {
@@ -321,6 +353,12 @@ type IssuesOptions struct {
321353
Priority string `json:"priority"`
322354
Version string `json:"version"`
323355
Assignee string `json:"assignee"`
356+
ctx context.Context
357+
}
358+
359+
func (io *IssuesOptions) WithContext(ctx context.Context) *IssuesOptions {
360+
io.ctx = ctx
361+
return io
324362
}
325363

326364
type IssueCommentsOptions struct {
@@ -352,6 +390,12 @@ type CommitsOptions struct {
352390
Exclude string `json:"exclude"`
353391
CommentID string `json:"comment_id"`
354392
Page *int `json:"page"`
393+
ctx context.Context
394+
}
395+
396+
func (cm *CommitsOptions) WithContext(ctx context.Context) *CommitsOptions {
397+
cm.ctx = ctx
398+
return cm
355399
}
356400

357401
type CommitStatusOptions struct {
@@ -373,6 +417,12 @@ type BranchRestrictionsOptions struct {
373417
FullSlug string `json:"full_slug"`
374418
Name string `json:"name"`
375419
Value interface{} `json:"value"`
420+
ctx context.Context
421+
}
422+
423+
func (b *BranchRestrictionsOptions) WithContext(ctx context.Context) *BranchRestrictionsOptions {
424+
b.ctx = ctx
425+
return b
376426
}
377427

378428
type DiffOptions struct {
@@ -406,6 +456,12 @@ type WebhooksOptions struct {
406456
Url string `json:"url"`
407457
Active bool `json:"active"`
408458
Events []string `json:"events"` // EX: {'repo:push','issue:created',..} REF: https://bit.ly/3FjRHHu
459+
ctx context.Context
460+
}
461+
462+
func (wo *WebhooksOptions) WithContext(ctx context.Context) *WebhooksOptions {
463+
wo.ctx = ctx
464+
return wo
409465
}
410466

411467
type RepositoryPipelineOptions struct {
@@ -437,6 +493,12 @@ type RepositoryPipelineVariableOptions struct {
437493
Key string `json:"key"`
438494
Value string `json:"value"`
439495
Secured bool `json:"secured"`
496+
ctx context.Context
497+
}
498+
499+
func (rpvo *RepositoryPipelineVariableOptions) WithContext(ctx context.Context) *RepositoryPipelineVariableOptions {
500+
rpvo.ctx = ctx
501+
return rpvo
440502
}
441503

442504
type RepositoryPipelineVariableDeleteOptions struct {
@@ -469,6 +531,12 @@ type DownloadsOptions struct {
469531
FilePath string `json:"filepath"`
470532
FileName string `json:"filename"`
471533
Files []File `json:"files"`
534+
ctx context.Context
535+
}
536+
537+
func (do *DownloadsOptions) WithContext(ctx context.Context) *DownloadsOptions {
538+
do.ctx = ctx
539+
return do
472540
}
473541

474542
type PageRes struct {
@@ -526,6 +594,12 @@ type RepositoryEnvironmentOptions struct {
526594
Name string `json:"name"`
527595
EnvironmentType RepositoryEnvironmentTypeOption `json:"environment_type"`
528596
Rank int `json:"rank"`
597+
ctx context.Context
598+
}
599+
600+
func (reo *RepositoryEnvironmentOptions) WithContext(ctx context.Context) *RepositoryEnvironmentOptions {
601+
reo.ctx = ctx
602+
return reo
529603
}
530604

531605
type RepositoryEnvironmentDeleteOptions struct {
@@ -553,6 +627,12 @@ type RepositoryDeploymentVariableOptions struct {
553627
Key string `json:"key"`
554628
Value string `json:"value"`
555629
Secured bool `json:"secured"`
630+
ctx context.Context
631+
}
632+
633+
func (rdvo *RepositoryDeploymentVariableOptions) WithContext(ctx context.Context) *RepositoryDeploymentVariableOptions {
634+
rdvo.ctx = ctx
635+
return rdvo
556636
}
557637

558638
type RepositoryDeploymentVariableDeleteOptions struct {
@@ -568,4 +648,10 @@ type DeployKeyOptions struct {
568648
Id int `json:"id"`
569649
Label string `json:"label"`
570650
Key string `json:"key"`
651+
ctx context.Context
652+
}
653+
654+
func (dk *DeployKeyOptions) WithContext(ctx context.Context) *DeployKeyOptions {
655+
dk.ctx = ctx
656+
return dk
571657
}

branchrestrictions.go

+1-1
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

+11-3
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, files []File, filesToDelete []string, params map[string]string) (interface{}, error) {
288+
func (c *Client) executeFileUpload(method string, urlStr string, files []File, filesToDelete []string, params map[string]string, ctx context.Context) (interface{}, error) {
283289
// Prepare a form that you will submit to that URL.
284290
var b bytes.Buffer
285291
w := multipart.NewWriter(&b)
@@ -326,7 +332,9 @@ func (c *Client) executeFileUpload(method string, urlStr string, files []File, f
326332
}
327333
// Don't forget to set the content type, this will contain the boundary.
328334
req.Header.Set("Content-Type", w.FormDataContentType())
329-
335+
if ctx != nil {
336+
req.WithContext(ctx)
337+
}
330338
c.authenticateRequest(req)
331339
return c.doRequest(req, true)
332340

commits.go

+2-2
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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (dl *Downloads) Create(do *DownloadsOptions) (interface{}, error) {
1818
Name: do.FileName,
1919
}}
2020
}
21-
return dl.c.executeFileUpload("POST", urlStr, do.Files, []string{}, make(map[string]string))
21+
return dl.c.executeFileUpload("POST", urlStr, do.Files, []string{}, make(map[string]string), do.ctx)
2222
}
2323

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

issues.go

+3-3
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

+8-1
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

+6-6
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)