Skip to content

Commit ee278ef

Browse files
committed
implement delete pull request endpoint
Signed-off-by: Zaki Shaikh <[email protected]>
1 parent d8fc906 commit ee278ef

File tree

10 files changed

+71
-0
lines changed

10 files changed

+71
-0
lines changed

scm/driver/azure/pr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRe
169169
return convertPullRequest(out), res, err
170170
}
171171

172+
func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
173+
return nil, scm.ErrNotSupported
174+
}
175+
172176
type prInput struct {
173177
SourceRefName string `json:"sourceRefName"`
174178
TargetRefName string `json:"targetRefName"`

scm/driver/bitbucket/pr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ func (s *pullService) UnrequestReview(ctx context.Context, repo string, number i
284284
return nil, scm.ErrNotSupported
285285
}
286286

287+
func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
288+
return nil, scm.ErrNotSupported
289+
}
290+
287291
type prSource struct {
288292
Commit struct {
289293
Type string `json:"type"`

scm/driver/fake/pr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,7 @@ func (s *pullService) SetMilestone(ctx context.Context, repo string, prID, numbe
376376
func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int) (*scm.Response, error) {
377377
return nil, scm.ErrNotSupported
378378
}
379+
380+
func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
381+
return nil, scm.ErrNotSupported
382+
}

scm/driver/gitea/pr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ func (s *pullService) UnrequestReview(ctx context.Context, repo string, number i
144144
return s.UnassignIssue(ctx, repo, number, logins)
145145
}
146146

147+
func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
148+
return nil, scm.ErrNotSupported
149+
}
150+
147151
//
148152
// native data structure conversion
149153
//

scm/driver/github/pr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ func (s *pullService) UnrequestReview(ctx context.Context, repo string, number i
158158
return res, nil
159159
}
160160

161+
func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
162+
return nil, scm.ErrNotSupported
163+
}
164+
161165
func prepareReviewersBody(logins []string, org string) (prReviewers, error) {
162166
body := prReviewers{}
163167
var errors []error

scm/driver/gitlab/pr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int)
266266
return res, err
267267
}
268268

269+
func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
270+
return nil, scm.ErrNotSupported
271+
}
272+
269273
type updateMergeRequestOptions struct {
270274
Title *string `json:"title,omitempty"`
271275
Description *string `json:"description,omitempty"`

scm/driver/gogs/pr.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int)
111111
return nil, scm.ErrNotSupported
112112
}
113113

114+
func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
115+
return nil, scm.ErrNotSupported
116+
}
117+
114118
//
115119
// native data structures
116120
//

scm/driver/stash/pr.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,21 @@ func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int)
305305
return nil, scm.ErrNotSupported
306306
}
307307

308+
func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
309+
namespace, name := scm.Split(repo)
310+
// in Bitbucket server (stash) to delete a pull request `version` of the pull request
311+
// must be provided in body of the request so it's worth fetching pull request via rest api
312+
// to get latest version of the pull request.
313+
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", namespace, name, prID)
314+
out := new(pullRequest)
315+
_, err := s.client.do(ctx, http.MethodGet, path, nil, out)
316+
if err != nil {
317+
return nil, fmt.Errorf("failed to get pull request. err: %v", err)
318+
}
319+
in := map[string]int{"version": out.Version}
320+
return s.client.do(ctx, http.MethodDelete, path, &in, nil)
321+
}
322+
308323
type createPRInput struct {
309324
Title string `json:"title,omitempty"`
310325
Description string `json:"description,omitempty"`

scm/driver/stash/pr_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,28 @@ func TestPullListLabels(t *testing.T) {
440440
t.Log(diff)
441441
}
442442
}
443+
444+
func TestPullRequestDelete(t *testing.T) {
445+
defer gock.Off()
446+
447+
gock.New("http://example.com:7990").
448+
Get("rest/api/1.0/projects/PRJ/repos/my-repo/pull-requests/1").
449+
Reply(200).
450+
Type("application/json").
451+
File("testdata/pr.json")
452+
453+
gock.New("http://example.com:7990").
454+
Delete("rest/api/1.0/projects/PRJ/repos/my-repo/pull-requests/1").
455+
Reply(204).
456+
Type("application/json")
457+
458+
client, _ := New("http://example.com:7990")
459+
resp, err := client.PullRequests.DeletePullRequest(context.Background(), "PRJ/my-repo", 1)
460+
if err != nil {
461+
t.Error(err)
462+
}
463+
464+
if resp.Status != 204 {
465+
t.Errorf("DeletePullRequest returned %v", resp.Status)
466+
}
467+
}

scm/pr.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ type (
193193

194194
// ClearMilestone removes the milestone from a pull request
195195
ClearMilestone(ctx context.Context, repo string, prID int) (*Response, error)
196+
197+
// DeletePullRequest deletes a pull request from a repo.
198+
DeletePullRequest(ctx context.Context, repo string, prID int) (*Response, error)
196199
}
197200
)
198201

0 commit comments

Comments
 (0)