Skip to content

Commit 75d549b

Browse files
committed
create internal function to consolidate code
1 parent b512feb commit 75d549b

File tree

1 file changed

+17
-69
lines changed

1 file changed

+17
-69
lines changed

changes.go

Lines changed: 17 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -691,21 +691,16 @@ func (s *ChangesService) FixChange(changeID string, input *FixInput) (*ChangeInf
691691
return v, resp, err
692692
}
693693

694-
// SubmitChange submits a change.
695-
//
696-
// The request body only needs to include a SubmitInput entity if submitting on behalf of another user.
697-
//
698-
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#submit-change
699-
func (s *ChangesService) SubmitChange(changeID string, input *SubmitInput) (*ChangeInfo, *Response, error) {
700-
u := fmt.Sprintf("changes/%s/submit", changeID)
701-
694+
// change is an internal function to consolidate code used by SubmitChange,
695+
// AbandonChange and other similar functions.
696+
func (s *ChangesService) change(tail string, changeID string, input interface{}) (*ChangeInfo, *Response, error) {
697+
u := fmt.Sprintf("changes/%s/%s", changeID, tail)
702698
req, err := s.client.NewRequest("POST", u, input)
703699
if err != nil {
704700
return nil, nil, err
705701
}
706702

707703
v := new(ChangeInfo)
708-
709704
resp, err := s.client.Do(req, v)
710705
if resp.StatusCode == http.StatusConflict {
711706
body, _ := ioutil.ReadAll(resp.Body)
@@ -714,28 +709,23 @@ func (s *ChangesService) SubmitChange(changeID string, input *SubmitInput) (*Cha
714709
return v, resp, err
715710
}
716711

712+
// SubmitChange submits a change.
713+
//
714+
// The request body only needs to include a SubmitInput entity if submitting on behalf of another user.
715+
//
716+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#submit-change
717+
func (s *ChangesService) SubmitChange(changeID string, input *SubmitInput) (*ChangeInfo, *Response, error) {
718+
return s.change("submit", changeID, input)
719+
}
720+
717721
// AbandonChange abandons a change.
718722
//
719723
// The request body does not need to include a AbandonInput entity if no review
720724
// comment is added.
721725
//
722726
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#abandon-change
723727
func (s *ChangesService) AbandonChange(changeID string, input *AbandonInput) (*ChangeInfo, *Response, error) {
724-
u := fmt.Sprintf("changes/%s/abandon", changeID)
725-
726-
req, err := s.client.NewRequest("POST", u, input)
727-
if err != nil {
728-
return nil, nil, err
729-
}
730-
731-
v := new(ChangeInfo)
732-
733-
resp, err := s.client.Do(req, v)
734-
if resp.StatusCode == http.StatusConflict {
735-
body, _ := ioutil.ReadAll(resp.Body)
736-
err = errors.New(string(body[:]))
737-
}
738-
return v, resp, err
728+
return s.change("abandon", changeID, input)
739729
}
740730

741731
// RebaseChange rebases a change.
@@ -745,21 +735,7 @@ func (s *ChangesService) AbandonChange(changeID string, input *AbandonInput) (*C
745735
//
746736
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#rebase-change
747737
func (s *ChangesService) RebaseChange(changeID string, input *RebaseInput) (*ChangeInfo, *Response, error) {
748-
u := fmt.Sprintf("changes/%s/rebase", changeID)
749-
750-
req, err := s.client.NewRequest("POST", u, input)
751-
if err != nil {
752-
return nil, nil, err
753-
}
754-
755-
v := new(ChangeInfo)
756-
757-
resp, err := s.client.Do(req, v)
758-
if resp.StatusCode == http.StatusConflict {
759-
body, _ := ioutil.ReadAll(resp.Body)
760-
err = errors.New(string(body[:]))
761-
}
762-
return v, resp, err
738+
return s.change("rebase", changeID, input)
763739
}
764740

765741
// RestoreChange restores a change.
@@ -769,21 +745,7 @@ func (s *ChangesService) RebaseChange(changeID string, input *RebaseInput) (*Cha
769745
//
770746
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#restore-change
771747
func (s *ChangesService) RestoreChange(changeID string, input *RestoreInput) (*ChangeInfo, *Response, error) {
772-
u := fmt.Sprintf("changes/%s/restore", changeID)
773-
774-
req, err := s.client.NewRequest("POST", u, input)
775-
if err != nil {
776-
return nil, nil, err
777-
}
778-
779-
v := new(ChangeInfo)
780-
781-
resp, err := s.client.Do(req, v)
782-
if resp.StatusCode == http.StatusConflict {
783-
body, _ := ioutil.ReadAll(resp.Body)
784-
err = errors.New(string(body[:]))
785-
}
786-
return v, resp, err
748+
return s.change("restore", changeID, input)
787749
}
788750

789751
// RevertChange reverts a change.
@@ -793,19 +755,5 @@ func (s *ChangesService) RestoreChange(changeID string, input *RestoreInput) (*C
793755
//
794756
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#revert-change
795757
func (s *ChangesService) RevertChange(changeID string, input *RevertInput) (*ChangeInfo, *Response, error) {
796-
u := fmt.Sprintf("changes/%s/revert", changeID)
797-
798-
req, err := s.client.NewRequest("POST", u, input)
799-
if err != nil {
800-
return nil, nil, err
801-
}
802-
803-
v := new(ChangeInfo)
804-
805-
resp, err := s.client.Do(req, v)
806-
if resp.StatusCode == http.StatusConflict {
807-
body, _ := ioutil.ReadAll(resp.Body)
808-
err = errors.New(string(body[:]))
809-
}
810-
return v, resp, err
758+
return s.change("revert", changeID, input)
811759
}

0 commit comments

Comments
 (0)