Commit 5dabb35f authored by Marcin Tojek's avatar Marcin Tojek

Add support for revert a commit

parent 9809d8e1
......@@ -515,3 +515,34 @@ func (s *CommitsService) CherryPickCommit(pid interface{}, sha string, opt *Cher
return c, resp, err
}
// RevertCommitOptions represents the available RevertCommit() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#revert-a-commit
type RevertCommitOptions struct {
Branch *string `url:"branch,omitempty" json:"branch,omitempty"`
}
// RevertCommit reverts a commit in a given branch.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#revert-a-commit
func (s *CommitsService) RevertCommit(pid interface{}, sha string, opt *RevertCommitOptions, options ...OptionFunc) (*Commit, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/commits/%s/revert", pathEscape(project), sha)
req, err := s.client.NewRequest("POST", u, opt, options)
if err != nil {
return nil, nil, err
}
var c *Commit
resp, err := s.client.Do(req, &c)
if err != nil {
return nil, resp, err
}
return c, resp, err
}
......@@ -9,6 +9,8 @@ import (
"github.com/stretchr/testify/assert"
)
var testRevertCommitTargetBranch = "release"
func TestGetCommit(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
......@@ -91,3 +93,84 @@ func TestSetCommitStatus(t *testing.T) {
t.Errorf("Commits.SetCommitStatus returned %+v, want %+v", status, want)
}
}
func TestRevertCommit_NoOptions(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/repository/commits/b0b3a907f41409829b307a28b82fdbd552ee5a27/revert", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
mustWriteHTTPResponse(t, w, "testdata/get_commit.json")
})
commit, resp, err := client.Commits.RevertCommit("1", "b0b3a907f41409829b307a28b82fdbd552ee5a27", nil)
if err != nil {
t.Fatalf("Commits.RevertCommit returned error: %v, response: %v", err, resp)
}
want := &Commit{
ID: "6104942438c14ec7bd21c6cd5bd995272b3faff6",
ShortID: "6104942438c",
Title: "Sanitize for network graph",
AuthorName: "randx",
AuthorEmail: "dmitriy.zaporozhets@gmail.com",
CommitterName: "Dmitriy",
CommitterEmail: "dmitriy.zaporozhets@gmail.com",
Message: "Sanitize for network graph",
ParentIDs: []string{"ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"},
Stats: &CommitStats{Additions: 15, Deletions: 10, Total: 25},
Status: BuildState(Running),
LastPipeline: &PipelineInfo{
ID: 8,
Ref: "master",
SHA: "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
Status: "created",
WebURL: "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416",
},
ProjectID: 13083,
}
assert.Equal(t, want, commit)
}
func TestRevertCommit_WithOptions(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/repository/commits/b0b3a907f41409829b307a28b82fdbd552ee5a27/revert", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testBody(t, r, `{"branch":"release"}`)
mustWriteHTTPResponse(t, w, "testdata/get_commit.json")
})
commit, resp, err := client.Commits.RevertCommit("1", "b0b3a907f41409829b307a28b82fdbd552ee5a27", &RevertCommitOptions{
Branch: &testRevertCommitTargetBranch,
})
if err != nil {
t.Fatalf("Commits.RevertCommit returned error: %v, response: %v", err, resp)
}
want := &Commit{
ID: "6104942438c14ec7bd21c6cd5bd995272b3faff6",
ShortID: "6104942438c",
Title: "Sanitize for network graph",
AuthorName: "randx",
AuthorEmail: "dmitriy.zaporozhets@gmail.com",
CommitterName: "Dmitriy",
CommitterEmail: "dmitriy.zaporozhets@gmail.com",
Message: "Sanitize for network graph",
ParentIDs: []string{"ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"},
Stats: &CommitStats{Additions: 15, Deletions: 10, Total: 25},
Status: BuildState(Running),
LastPipeline: &PipelineInfo{
ID: 8,
Ref: "master",
SHA: "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
Status: "created",
WebURL: "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416",
},
ProjectID: 13083,
}
assert.Equal(t, want, commit)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment