Commit c00cef4b authored by starblind's avatar starblind Committed by Sander van Harmelen

add support for creating releases in tags api (#373)

* add support for creating releases in tags api

* add support for updating release notes
parent 42565bf1
......@@ -33,13 +33,15 @@ type TagsService struct {
//
// GitLab API docs: https://docs.gitlab.com/ce/api/tags.html
type Tag struct {
Commit *Commit `json:"commit"`
Release struct {
TagName string `json:"tag_name"`
Description string `json:"description"`
} `json:"release"`
Name string `json:"name"`
Message string `json:"message"`
Commit *Commit `json:"commit"`
Release *Release `json:"release"`
Name string `json:"name"`
Message string `json:"message"`
}
type Release struct {
TagName string `json:"tag_name"`
Description string `json:"description"`
}
func (t Tag) String() string {
......@@ -158,3 +160,70 @@ func (s *TagsService) DeleteTag(pid interface{}, tag string, options ...OptionFu
return s.client.Do(req, nil)
}
// CreateReleaseOptions represents the available CreateRelease() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/tags.html#create-a-new-release
type CreateReleaseOptions struct {
Description *string `url:"description:omitempty" json:"description,omitempty"`
}
// CreateRelease Add release notes to the existing git tag.
// If there already exists a release for the given tag, status code 409 is returned.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/tags.html#create-a-new-release
func (s *TagsService) CreateRelease(pid interface{}, tag string, opt *CreateReleaseOptions, options ...OptionFunc) (*Release, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/tags/%s/release", url.QueryEscape(project), tag)
req, err := s.client.NewRequest("POST", u, opt, options)
if err != nil {
return nil, nil, err
}
r := new(Release)
resp, err := s.client.Do(req, r)
if err != nil {
return nil, resp, err
}
return r, resp, err
}
// UpdateReleaseOptions represents the available UpdateRelease() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/tags.html#update-a-release
type UpdateReleaseOptions struct {
Description *string `url:"description:omitempty" json:"description,omitempty"`
}
// UpdateRelease Updates the release notes of a given release.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/tags.html#update-a-release
func (s *TagsService) UpdateRelease(pid interface{}, tag string, opt *UpdateReleaseOptions, options ...OptionFunc) (*Release, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/repository/tags/%s/release", url.QueryEscape(project), tag)
req, err := s.client.NewRequest("PUT", u, opt, options)
if err != nil {
return nil, nil, err
}
r := new(Release)
resp, err := s.client.Do(req, r)
if err != nil {
return nil, resp, err
}
return r, resp, err
}
......@@ -27,4 +27,48 @@ func TestListTags(t *testing.T) {
if !reflect.DeepEqual(want, tags) {
t.Errorf("Tags.ListTags returned %+v, want %+v", tags, want)
}
}
\ No newline at end of file
}
func TestCreateRelease(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1/repository/tags/1.0.0/release", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprint(w, `{"tag_name": "1.0.0", "description": "Amazing release. Wow"}`)
})
opt := &CreateReleaseOptions{Description: String("Amazing release. Wow")}
release, _, err := client.Tags.CreateRelease(1, "1.0.0", opt)
if err != nil {
t.Errorf("Tags.CreateRelease returned error: %v", err)
}
want := &Release{TagName: "1.0.0", Description: "Amazing release. Wow"}
if !reflect.DeepEqual(want, release) {
t.Errorf("Tags.CreateRelease returned %+v, want %+v", release, want)
}
}
func TestUpdateRelease(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1/repository/tags/1.0.0/release", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
fmt.Fprint(w, `{"tag_name": "1.0.0", "description": "Amazing release. Wow!"}`)
})
opt := &UpdateReleaseOptions{Description: String("Amazing release. Wow!")}
release, _, err := client.Tags.UpdateRelease(1, "1.0.0", opt)
if err != nil {
t.Errorf("Tags.UpdateRelease returned error: %v", err)
}
want := &Release{TagName: "1.0.0", Description: "Amazing release. Wow!"}
if !reflect.DeepEqual(want, release) {
t.Errorf("Tags.UpdateRelease returned %+v, want %+v", release, want)
}
}
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