Skip to content

Commit

Permalink
implement delete pull request endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Zaki Shaikh <[email protected]>
  • Loading branch information
zakisk committed Dec 28, 2024
1 parent d8fc906 commit ee278ef
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions scm/driver/azure/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRe
return convertPullRequest(out), res, err
}

func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}

type prInput struct {
SourceRefName string `json:"sourceRefName"`
TargetRefName string `json:"targetRefName"`
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/bitbucket/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ func (s *pullService) UnrequestReview(ctx context.Context, repo string, number i
return nil, scm.ErrNotSupported
}

func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}

type prSource struct {
Commit struct {
Type string `json:"type"`
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/fake/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,7 @@ func (s *pullService) SetMilestone(ctx context.Context, repo string, prID, numbe
func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}

func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
4 changes: 4 additions & 0 deletions scm/driver/gitea/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func (s *pullService) UnrequestReview(ctx context.Context, repo string, number i
return s.UnassignIssue(ctx, repo, number, logins)
}

func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}

//
// native data structure conversion
//
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/github/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (s *pullService) UnrequestReview(ctx context.Context, repo string, number i
return res, nil
}

func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}

func prepareReviewersBody(logins []string, org string) (prReviewers, error) {
body := prReviewers{}
var errors []error
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/gitlab/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int)
return res, err
}

func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}

type updateMergeRequestOptions struct {
Title *string `json:"title,omitempty"`
Description *string `json:"description,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/gogs/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int)
return nil, scm.ErrNotSupported
}

func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}

//
// native data structures
//
Expand Down
15 changes: 15 additions & 0 deletions scm/driver/stash/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,21 @@ func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int)
return nil, scm.ErrNotSupported
}

func (s *pullService) DeletePullRequest(ctx context.Context, repo string, prID int) (*scm.Response, error) {
namespace, name := scm.Split(repo)
// in Bitbucket server (stash) to delete a pull request `version` of the pull request
// must be provided in body of the request so it's worth fetching pull request via rest api
// to get latest version of the pull request.
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", namespace, name, prID)
out := new(pullRequest)
_, err := s.client.do(ctx, http.MethodGet, path, nil, out)
if err != nil {
return nil, fmt.Errorf("failed to get pull request. err: %v", err)
}
in := map[string]int{"version": out.Version}
return s.client.do(ctx, http.MethodDelete, path, &in, nil)
}

type createPRInput struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Expand Down
25 changes: 25 additions & 0 deletions scm/driver/stash/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,28 @@ func TestPullListLabels(t *testing.T) {
t.Log(diff)
}
}

func TestPullRequestDelete(t *testing.T) {
defer gock.Off()

gock.New("http://example.com:7990").
Get("rest/api/1.0/projects/PRJ/repos/my-repo/pull-requests/1").
Reply(200).
Type("application/json").
File("testdata/pr.json")

gock.New("http://example.com:7990").
Delete("rest/api/1.0/projects/PRJ/repos/my-repo/pull-requests/1").
Reply(204).
Type("application/json")

client, _ := New("http://example.com:7990")
resp, err := client.PullRequests.DeletePullRequest(context.Background(), "PRJ/my-repo", 1)
if err != nil {
t.Error(err)
}

if resp.Status != 204 {
t.Errorf("DeletePullRequest returned %v", resp.Status)
}
}
3 changes: 3 additions & 0 deletions scm/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ type (

// ClearMilestone removes the milestone from a pull request
ClearMilestone(ctx context.Context, repo string, prID int) (*Response, error)

// DeletePullRequest deletes a pull request from a repo.
DeletePullRequest(ctx context.Context, repo string, prID int) (*Response, error)
}
)

Expand Down

0 comments on commit ee278ef

Please sign in to comment.