Skip to content

Commit

Permalink
Merge pull request #41 from andygrunwald/add-missing-endpoints
Browse files Browse the repository at this point in the history
Add missing AbandonChange, RebaseChange, RestoreChange and RevertChange
  • Loading branch information
opalmer authored Sep 14, 2017
2 parents a2e6113 + 6c23053 commit 9cd8431
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ first. For more complete details see

## Versions

### 0.5.1

* Added the `AbandonChange`, `RebaseChange`, `RestoreChange` and
`RevertChange` functions.

### 0.5.0

**WARNING**: This release includes breaking changes.
Expand Down
78 changes: 61 additions & 17 deletions changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ type GitPersonInfo struct {
TZ int `json:"tz"`
}

// NotifyInfo entity contains detailed information about who should be
// notified about an update
type NotifyInfo struct {
Accounts []AccountInfo `json:"accounts"`
}

// AbandonInput entity contains information for abandoning a change.
type AbandonInput struct {
Message string `json:"message,omitempty"`
Message string `json:"message,omitempty"`
Notify string `json:"notify"`
NotifyDetails []NotifyInfo `json:"notify_details"`
}

// ApprovalInfo entity contains information about an approval from a user for a label on a change.
Expand Down Expand Up @@ -683,21 +691,16 @@ func (s *ChangesService) FixChange(changeID string, input *FixInput) (*ChangeInf
return v, resp, err
}

// SubmitChange submits a change.
//
// The request body only needs to include a SubmitInput entity if submitting on behalf of another user.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#submit-change
func (s *ChangesService) SubmitChange(changeID string, input *SubmitInput) (*ChangeInfo, *Response, error) {
u := fmt.Sprintf("changes/%s/submit", changeID)

// change is an internal function to consolidate code used by SubmitChange,
// AbandonChange and other similar functions.
func (s *ChangesService) change(tail string, changeID string, input interface{}) (*ChangeInfo, *Response, error) {
u := fmt.Sprintf("changes/%s/%s", changeID, tail)
req, err := s.client.NewRequest("POST", u, input)
if err != nil {
return nil, nil, err
}

v := new(ChangeInfo)

resp, err := s.client.Do(req, v)
if resp.StatusCode == http.StatusConflict {
body, _ := ioutil.ReadAll(resp.Body)
Expand All @@ -706,10 +709,51 @@ func (s *ChangesService) SubmitChange(changeID string, input *SubmitInput) (*Cha
return v, resp, err
}

/*
Missing Change Endpoints
Abandon Change
Restore Change
Rebase Change
Revert Change
*/
// SubmitChange submits a change.
//
// The request body only needs to include a SubmitInput entity if submitting on behalf of another user.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#submit-change
func (s *ChangesService) SubmitChange(changeID string, input *SubmitInput) (*ChangeInfo, *Response, error) {
return s.change("submit", changeID, input)
}

// AbandonChange abandons a change.
//
// The request body does not need to include a AbandonInput entity if no review
// comment is added.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#abandon-change
func (s *ChangesService) AbandonChange(changeID string, input *AbandonInput) (*ChangeInfo, *Response, error) {
return s.change("abandon", changeID, input)
}

// RebaseChange rebases a change.
//
// Optionally, the parent revision can be changed to another patch set through
// the RebaseInput entity.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#rebase-change
func (s *ChangesService) RebaseChange(changeID string, input *RebaseInput) (*ChangeInfo, *Response, error) {
return s.change("rebase", changeID, input)
}

// RestoreChange restores a change.
//
// The request body does not need to include a RestoreInput entity if no review
// comment is added.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#restore-change
func (s *ChangesService) RestoreChange(changeID string, input *RestoreInput) (*ChangeInfo, *Response, error) {
return s.change("restore", changeID, input)
}

// RevertChange reverts a change.
//
// The request body does not need to include a RevertInput entity if no
// review comment is added.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#revert-change
func (s *ChangesService) RevertChange(changeID string, input *RevertInput) (*ChangeInfo, *Response, error) {
return s.change("revert", changeID, input)
}
191 changes: 191 additions & 0 deletions changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/andygrunwald/go-gerrit"
)
Expand Down Expand Up @@ -77,3 +78,193 @@ func ExampleChangesService_PublishChangeEdit() {
panic(err)
}
}

func TestChangesService_SubmitChange(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/changes/123/submit" {
t.Errorf("%s != /changes/123/submit", r.URL.Path)
}
fmt.Fprint(w, `{"id": "123"}`)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
info, _, err := client.Changes.SubmitChange("123", nil)
if err != nil {
t.Error(err)
}
if info.ID != "123" {
t.Error("Invalid id")
}
}

func TestChangesService_SubmitChange_Conflict(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusConflict)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
_, response, _ := client.Changes.SubmitChange("123", nil)
if response.StatusCode != http.StatusConflict {
t.Error("Expected 409 code")
}
}

func TestChangesService_AbandonChange(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/changes/123/abandon" {
t.Errorf("%s != /changes/123/abandon", r.URL.Path)
}
fmt.Fprint(w, `{"id": "123"}`)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
info, _, err := client.Changes.AbandonChange("123", nil)
if err != nil {
t.Error(err)
}
if info.ID != "123" {
t.Error("Invalid id")
}
}

func TestChangesService_AbandonChange_Conflict(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusConflict)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
_, response, _ := client.Changes.AbandonChange("123", nil)
if response.StatusCode != http.StatusConflict {
t.Error("Expected 409 code")
}
}

func TestChangesService_RebaseChange(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/changes/123/rebase" {
t.Errorf("%s != /changes/123/rebase", r.URL.Path)
}
fmt.Fprint(w, `{"id": "123"}`)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
info, _, err := client.Changes.RebaseChange("123", nil)
if err != nil {
t.Error(err)
}
if info.ID != "123" {
t.Error("Invalid id")
}
}

func TestChangesService_RebaseChange_Conflict(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusConflict)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
_, response, _ := client.Changes.RebaseChange("123", nil)
if response.StatusCode != http.StatusConflict {
t.Error("Expected 409 code")
}
}

func TestChangesService_RestoreChange(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/changes/123/restore" {
t.Errorf("%s != /changes/123/restore", r.URL.Path)
}
fmt.Fprint(w, `{"id": "123"}`)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
info, _, err := client.Changes.RestoreChange("123", nil)
if err != nil {
t.Error(err)
}
if info.ID != "123" {
t.Error("Invalid id")
}
}

func TestChangesService_RestoreChange_Conflict(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusConflict)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
_, response, _ := client.Changes.RestoreChange("123", nil)
if response.StatusCode != http.StatusConflict {
t.Error("Expected 409 code")
}
}

func TestChangesService_RevertChange(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/changes/123/revert" {
t.Errorf("%s != /changes/123/revert", r.URL.Path)
}
fmt.Fprint(w, `{"id": "123"}`)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
info, _, err := client.Changes.RevertChange("123", nil)
if err != nil {
t.Error(err)
}
if info.ID != "123" {
t.Error("Invalid id")
}
}

func TestChangesService_RevertChange_Conflict(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusConflict)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
_, response, _ := client.Changes.RevertChange("123", nil)
if response.StatusCode != http.StatusConflict {
t.Error("Expected 409 code")
}
}

0 comments on commit 9cd8431

Please sign in to comment.