Skip to content

Commit

Permalink
api: deprecate CreateChange using ChangeInfo (use ChangeInput instead)
Browse files Browse the repository at this point in the history
The API docs refer to the ChangeInput entity for create-change, see
Documentation/rest-api-changes.html#change-input

Rename the existing CreateChange(ChangeInfo) method to
CreateChangeDeprecated and add a new CreateChange(ChangeInput)
implementation. Also added a test that ensures that none of the JSON
payload strings are empty strings (gerrit does not like those).
  • Loading branch information
wwade committed Sep 15, 2021
1 parent cc4e14e commit 7b2c737
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
36 changes: 34 additions & 2 deletions changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ type AttentionSetInfo struct {
// the edits to span newlines.
type DiffIntralineInfo [][2]int

// ChangeInput entity contains information about creating a new change.
type ChangeInput struct {
Project string `json:"project"`
Branch string `json:"branch"`
Subject string `json:"subject"`
Topic string `json:"topic,omitempty"`
Status string `json:"status,omitempty"`
}

// ChangeInfo entity contains information about a change.
type ChangeInfo struct {
ID string `json:"id"`
Expand Down Expand Up @@ -703,15 +712,38 @@ func (s *ChangesService) getCommentInfoMapSliceResponse(u string) (*map[string][
}

// CreateChange creates a new change.
// The change info ChangeInfo entity must be provided in the request body.
//
// The change input ChangeInput entity must be provided in the request body.
//
// Only the following attributes are honored: project, branch, subject, status and topic.
// The first three attributes are mandatory.
//
// Valid values for status are: DRAFT and NEW.
//
// As response a ChangeInfo entity is returned that describes the resulting change.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#create-change
func (s *ChangesService) CreateChange(input *ChangeInfo) (*ChangeInfo, *Response, error) {
func (s *ChangesService) CreateChange(input *ChangeInput) (*ChangeInfo, *Response, error) {
u := "changes/"

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 err != nil {
return nil, resp, err
}

return v, resp, err
}

// CreateChangeDeprecated creates a new change. (DEPRECATED)
//
// Use CreateChange instead.
func (s *ChangesService) CreateChangeDeprecated(input *ChangeInfo) (*ChangeInfo, *Response, error) {
u := "changes/"

req, err := s.client.NewRequest("POST", u, input)
Expand Down
63 changes: 63 additions & 0 deletions changes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gerrit_test

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -105,6 +106,68 @@ func ExampleChangesService_PublishChangeEdit() {
}
}

func TestChangesService_CreateChange(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var payload map[string]interface{}
if err := decoder.Decode(&payload); err != nil {
t.Error(err)
}

jsonStr, err := json.MarshalIndent(payload, "", " ")
if err != nil {
t.Error(err)
}
t.Logf("Request payload:\n%s", jsonStr)

required := func(field string) string {
value, ok := payload[field]
strVal := value.(string)
if !ok {
t.Errorf("Missing required field %q", field)
}
return strVal
}
project := required("project")
branch := required("branch")
subject := required("subject")

for field, generic := range payload {
switch value := generic.(type) {
case string:
if len(value) == 0 {
t.Errorf("Empty value for field %q", field)
}
}
}

if r.URL.Path != "/changes/" {
t.Errorf("%s != /changes/", r.URL.Path)
}
if r.Method != "POST" {
t.Errorf("%s != POST", r.Method)
}
fmt.Fprintf(w, `{ "id": "abc1234", "project": "%s", "branch": "%s", "subject": "%s"}`, project, branch, subject)
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
if err != nil {
t.Error(err)
}
info, _, err := client.Changes.CreateChange(&gerrit.ChangeInput{
Project: "myProject",
Branch: "main",
Subject: "test change",
})
if err != nil {
t.Error(err)
}
if info.ID != "abc1234" {
t.Error("Invalid id")
}
}

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" {
Expand Down

0 comments on commit 7b2c737

Please sign in to comment.