Skip to content

Commit

Permalink
feat: Added context based execution to POST apis methods (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-exp authored May 22, 2024
1 parent 98da4fb commit bfc0803
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 37 deletions.
86 changes: 86 additions & 0 deletions bitbucket.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package bitbucket

import "context"

type users interface {
Get(username string) (*User, error)
Followers(username string) (interface{}, error)
Expand Down Expand Up @@ -160,6 +162,12 @@ type RepositoryOptions struct {
HasIssues string `json:"has_issues"`
HasWiki string `json:"has_wiki"`
Project string `json:"project"`
ctx context.Context
}

func (ro *RepositoryOptions) WithContext(ctx context.Context) *RepositoryOptions {
ro.ctx = ctx
return ro
}

type RepositoryForkOptions struct {
Expand All @@ -176,6 +184,12 @@ type RepositoryForkOptions struct {
HasIssues string `json:"has_issues"`
HasWiki string `json:"has_wiki"`
Project string `json:"project"`
ctx context.Context
}

func (fo *RepositoryForkOptions) WithContext(ctx context.Context) *RepositoryForkOptions {
fo.ctx = ctx
return fo
}

type RepositoryFilesOptions struct {
Expand Down Expand Up @@ -209,6 +223,12 @@ type RepositoryBlobWriteOptions struct {
Author string `json:"author"`
Message string `json:"message"`
Branch string `json:"branch"`
ctx context.Context
}

func (ro *RepositoryBlobWriteOptions) WithContext(ctx context.Context) *RepositoryBlobWriteOptions {
ro.ctx = ctx
return ro
}

// RepositoryRefOptions represents the options for describing a repository's refs (i.e.
Expand Down Expand Up @@ -294,6 +314,12 @@ type PullRequestsOptions struct {
States []string `json:"states"`
Query string `json:"query"`
Sort string `json:"sort"`
ctx context.Context
}

func (po *PullRequestsOptions) WithContext(ctx context.Context) *PullRequestsOptions {
po.ctx = ctx
return po
}

type PullRequestCommentOptions struct {
Expand All @@ -303,6 +329,12 @@ type PullRequestCommentOptions struct {
Content string `json:"content"`
CommentId string `json:"-"`
Parent *int `json:"parent"`
ctx context.Context
}

func (pco *PullRequestCommentOptions) WithContext(ctx context.Context) *PullRequestCommentOptions {
pco.ctx = ctx
return pco
}

type IssuesOptions struct {
Expand All @@ -321,6 +353,12 @@ type IssuesOptions struct {
Priority string `json:"priority"`
Version string `json:"version"`
Assignee string `json:"assignee"`
ctx context.Context
}

func (io *IssuesOptions) WithContext(ctx context.Context) *IssuesOptions {
io.ctx = ctx
return io
}

type IssueCommentsOptions struct {
Expand Down Expand Up @@ -352,6 +390,12 @@ type CommitsOptions struct {
Exclude string `json:"exclude"`
CommentID string `json:"comment_id"`
Page *int `json:"page"`
ctx context.Context
}

func (cm *CommitsOptions) WithContext(ctx context.Context) *CommitsOptions {
cm.ctx = ctx
return cm
}

type CommitStatusOptions struct {
Expand All @@ -373,6 +417,12 @@ type BranchRestrictionsOptions struct {
FullSlug string `json:"full_slug"`
Name string `json:"name"`
Value interface{} `json:"value"`
ctx context.Context
}

func (b *BranchRestrictionsOptions) WithContext(ctx context.Context) *BranchRestrictionsOptions {
b.ctx = ctx
return b
}

type DiffOptions struct {
Expand Down Expand Up @@ -406,6 +456,12 @@ type WebhooksOptions struct {
Url string `json:"url"`
Active bool `json:"active"`
Events []string `json:"events"` // EX: {'repo:push','issue:created',..} REF: https://bit.ly/3FjRHHu
ctx context.Context
}

func (wo *WebhooksOptions) WithContext(ctx context.Context) *WebhooksOptions {
wo.ctx = ctx
return wo
}

type RepositoryPipelineOptions struct {
Expand Down Expand Up @@ -437,6 +493,12 @@ type RepositoryPipelineVariableOptions struct {
Key string `json:"key"`
Value string `json:"value"`
Secured bool `json:"secured"`
ctx context.Context
}

func (rpvo *RepositoryPipelineVariableOptions) WithContext(ctx context.Context) *RepositoryPipelineVariableOptions {
rpvo.ctx = ctx
return rpvo
}

type RepositoryPipelineVariableDeleteOptions struct {
Expand Down Expand Up @@ -469,6 +531,12 @@ type DownloadsOptions struct {
FilePath string `json:"filepath"`
FileName string `json:"filename"`
Files []File `json:"files"`
ctx context.Context
}

func (do *DownloadsOptions) WithContext(ctx context.Context) *DownloadsOptions {
do.ctx = ctx
return do
}

type PageRes struct {
Expand Down Expand Up @@ -526,6 +594,12 @@ type RepositoryEnvironmentOptions struct {
Name string `json:"name"`
EnvironmentType RepositoryEnvironmentTypeOption `json:"environment_type"`
Rank int `json:"rank"`
ctx context.Context
}

func (reo *RepositoryEnvironmentOptions) WithContext(ctx context.Context) *RepositoryEnvironmentOptions {
reo.ctx = ctx
return reo
}

type RepositoryEnvironmentDeleteOptions struct {
Expand Down Expand Up @@ -553,6 +627,12 @@ type RepositoryDeploymentVariableOptions struct {
Key string `json:"key"`
Value string `json:"value"`
Secured bool `json:"secured"`
ctx context.Context
}

func (rdvo *RepositoryDeploymentVariableOptions) WithContext(ctx context.Context) *RepositoryDeploymentVariableOptions {
rdvo.ctx = ctx
return rdvo
}

type RepositoryDeploymentVariableDeleteOptions struct {
Expand All @@ -568,4 +648,10 @@ type DeployKeyOptions struct {
Id int `json:"id"`
Label string `json:"label"`
Key string `json:"key"`
ctx context.Context
}

func (dk *DeployKeyOptions) WithContext(ctx context.Context) *DeployKeyOptions {
dk.ctx = ctx
return dk
}
2 changes: 1 addition & 1 deletion branchrestrictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (b *BranchRestrictions) Create(bo *BranchRestrictionsOptions) (*BranchRestr
return nil, err
}
urlStr := b.c.requestUrl("/repositories/%s/%s/branch-restrictions", bo.Owner, bo.RepoSlug)
response, err := b.c.execute("POST", urlStr, data)
response, err := b.c.executeWithContext("POST", urlStr, data, bo.ctx)
if err != nil {
return nil, err
}
Expand Down
14 changes: 11 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func (c *Client) executeRaw(method string, urlStr string, text string) (io.ReadC
}

func (c *Client) execute(method string, urlStr string, text string) (interface{}, error) {
return c.executeWithContext(method, urlStr, text, context.Background())
}

func (c *Client) executeWithContext(method string, urlStr string, text string, ctx context.Context) (interface{}, error) {
body := strings.NewReader(text)
req, err := http.NewRequest(method, urlStr, body)
if err != nil {
Expand All @@ -239,7 +243,9 @@ func (c *Client) execute(method string, urlStr string, text string) (interface{}
if text != "" {
req.Header.Set("Content-Type", "application/json")
}

if ctx != nil {
req.WithContext(ctx)
}
c.authenticateRequest(req)
result, err := c.doRequest(req, false)
if err != nil {
Expand Down Expand Up @@ -279,7 +285,7 @@ func (c *Client) executePaginated(method string, urlStr string, text string, pag
return result, nil
}

func (c *Client) executeFileUpload(method string, urlStr string, files []File, filesToDelete []string, params map[string]string) (interface{}, error) {
func (c *Client) executeFileUpload(method string, urlStr string, files []File, filesToDelete []string, params map[string]string, ctx context.Context) (interface{}, error) {
// Prepare a form that you will submit to that URL.
var b bytes.Buffer
w := multipart.NewWriter(&b)
Expand Down Expand Up @@ -326,7 +332,9 @@ func (c *Client) executeFileUpload(method string, urlStr string, files []File, f
}
// Don't forget to set the content type, this will contain the boundary.
req.Header.Set("Content-Type", w.FormDataContentType())

if ctx != nil {
req.WithContext(ctx)
}
c.authenticateRequest(req)
return c.doRequest(req, true)

Expand Down
4 changes: 2 additions & 2 deletions commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (cm *Commits) GetCommitStatus(cmo *CommitsOptions, commitStatusKey string)

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

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

func (cm *Commits) buildCommitsQuery(include, exclude string) string {
Expand Down
2 changes: 1 addition & 1 deletion deploykeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (dk *DeployKeys) Create(opt *DeployKeyOptions) (*DeployKey, error) {
return nil, err
}
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys", opt.Owner, opt.RepoSlug)
response, err := dk.c.execute("POST", urlStr, data)
response, err := dk.c.executeWithContext("POST", urlStr, data, opt.ctx)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (dl *Downloads) Create(do *DownloadsOptions) (interface{}, error) {
Name: do.FileName,
}}
}
return dl.c.executeFileUpload("POST", urlStr, do.Files, []string{}, make(map[string]string))
return dl.c.executeFileUpload("POST", urlStr, do.Files, []string{}, make(map[string]string), do.ctx)
}

func (dl *Downloads) List(do *DownloadsOptions) (interface{}, error) {
Expand Down
6 changes: 3 additions & 3 deletions issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (p *Issues) Create(io *IssuesOptions) (interface{}, error) {
return nil, err
}
urlStr := p.c.requestUrl("/repositories/%s/%s/issues", io.Owner, io.RepoSlug)
return p.c.execute("POST", urlStr, data)
return p.c.executeWithContext("POST", urlStr, data, io.ctx)
}

func (p *Issues) GetVote(io *IssuesOptions) (bool, interface{}, error) {
Expand Down Expand Up @@ -198,7 +198,7 @@ func (p *Issues) CreateComment(ico *IssueCommentsOptions) (interface{}, error) {
return nil, err
}

return p.c.execute("POST", urlStr, data)
return p.c.executeWithContext("POST", urlStr, data, ico.ctx)
}

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

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

return p.c.execute("POST", url.String(), string(data))
return p.c.executeWithContext("POST", url.String(), string(data), ico.ctx)
}

func (p *Issues) GetChange(ico *IssueChangesOptions) (interface{}, error) {
Expand Down
9 changes: 8 additions & 1 deletion project.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bitbucket

import (
"context"
"encoding/json"

"github.com/mitchellh/mapstructure"
Expand All @@ -23,6 +24,12 @@ type ProjectOptions struct {
Key string `json:"key"`
Description string `json:"description"`
IsPrivate bool `json:"is_private"`
ctx context.Context
}

func (po *ProjectOptions) WithContext(ctx context.Context) *ProjectOptions {
po.ctx = ctx
return po
}

func (t *Workspace) GetProject(opt *ProjectOptions) (*Project, error) {
Expand All @@ -41,7 +48,7 @@ func (t *Workspace) CreateProject(opt *ProjectOptions) (*Project, error) {
return nil, err
}
urlStr := t.c.requestUrl("/workspaces/%s/projects", opt.Owner)
response, err := t.c.execute("POST", urlStr, data)
response, err := t.c.executeWithContext("POST", urlStr, data, opt.ctx)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions pullrequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (p *PullRequests) Create(po *PullRequestsOptions) (interface{}, error) {
return nil, err
}
urlStr := p.c.requestUrl("/repositories/%s/%s/pullrequests/", po.Owner, po.RepoSlug)
return p.c.execute("POST", urlStr, data)
return p.c.executeWithContext("POST", urlStr, data, po.ctx)
}

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

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

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

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

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

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

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

func (p *PullRequests) UpdateComment(co *PullRequestCommentOptions) (interface{}, error) {
Expand Down
Loading

0 comments on commit bfc0803

Please sign in to comment.