Skip to content

Commit

Permalink
Add generate release notes support (#2114)
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 authored Oct 12, 2021
1 parent e87b9aa commit d1858a3
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
24 changes: 24 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion github/github-stringify_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions github/repos_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type RepositoryRelease struct {
Draft *bool `json:"draft,omitempty"`
Prerelease *bool `json:"prerelease,omitempty"`
DiscussionCategoryName *string `json:"discussion_category_name,omitempty"`
GenerateReleaseNotes *bool `json:"generate_release_notes,omitempty"`

// The following fields are not used in CreateRelease or EditRelease:
ID *int64 `json:"id,omitempty"`
Expand All @@ -46,6 +47,19 @@ func (r RepositoryRelease) String() string {
return Stringify(r)
}

// RepositoryReleaseNotes represents a GitHub-generated release notes.
type RepositoryReleaseNotes struct {
Name string `json:"name"`
Body string `json:"body"`
}

// GenerateNotesOptions represents the options to generate release notes.
type GenerateNotesOptions struct {
TagName string `json:"tag_name"`
PreviousTagName *string `json:"previous_tag_name,omitempty"`
TargetCommitish *string `json:"target_commitish,omitempty"`
}

// ReleaseAsset represents a GitHub release asset in a repository.
type ReleaseAsset struct {
ID *int64 `json:"id,omitempty"`
Expand Down Expand Up @@ -114,6 +128,25 @@ func (s *RepositoriesService) GetReleaseByTag(ctx context.Context, owner, repo,
return s.getSingleRelease(ctx, u)
}

// GenerateReleaseNotes generates the release notes for the given tag.
// TODO: api docs
// GitHub API docs:
func (s *RepositoriesService) GenerateReleaseNotes(ctx context.Context, owner, repo string, opts *GenerateNotesOptions) (*RepositoryReleaseNotes, *Response, error) {
u := fmt.Sprintf("repos/%s/%s/releases/generate-notes", owner, repo)
req, err := s.client.NewRequest("POST", u, opts)
if err != nil {
return nil, nil, err
}

r := new(RepositoryReleaseNotes)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}

return r, resp, nil
}

func (s *RepositoriesService) getSingleRelease(ctx context.Context, url string) (*RepositoryRelease, *Response, error) {
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
Expand Down Expand Up @@ -141,6 +174,7 @@ type repositoryReleaseRequest struct {
Body *string `json:"body,omitempty"`
Draft *bool `json:"draft,omitempty"`
Prerelease *bool `json:"prerelease,omitempty"`
GenerateReleaseNotes *bool `json:"generate_release_notes,omitempty"`
DiscussionCategoryName *string `json:"discussion_category_name,omitempty"`
}

Expand All @@ -161,6 +195,7 @@ func (s *RepositoriesService) CreateRelease(ctx context.Context, owner, repo str
Draft: release.Draft,
Prerelease: release.Prerelease,
DiscussionCategoryName: release.DiscussionCategoryName,
GenerateReleaseNotes: release.GenerateReleaseNotes,
}

req, err := s.client.NewRequest("POST", u, releaseReq)
Expand Down Expand Up @@ -193,6 +228,7 @@ func (s *RepositoriesService) EditRelease(ctx context.Context, owner, repo strin
Draft: release.Draft,
Prerelease: release.Prerelease,
DiscussionCategoryName: release.DiscussionCategoryName,
GenerateReleaseNotes: release.GenerateReleaseNotes,
}

req, err := s.client.NewRequest("PATCH", u, releaseReq)
Expand Down
45 changes: 45 additions & 0 deletions github/repos_releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,47 @@ func TestRepositoriesService_ListReleases(t *testing.T) {
})
}

func TestRepositoriesService_GenerateReleaseNotes(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/releases/generate-notes", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testBody(t, r, `{"tag_name":"v1.0.0"}`+"\n")
fmt.Fprint(w, `{"name":"v1.0.0","body":"**Full Changelog**: https://github.com/o/r/compare/v0.9.0...v1.0.0"}`)
})

opt := &GenerateNotesOptions{
TagName: "v1.0.0",
}
ctx := context.Background()
releases, _, err := client.Repositories.GenerateReleaseNotes(ctx, "o", "r", opt)
if err != nil {
t.Errorf("Repositories.GenerateReleaseNotes returned error: %v", err)
}
want := &RepositoryReleaseNotes{
Name: "v1.0.0",
Body: "**Full Changelog**: https://github.com/o/r/compare/v0.9.0...v1.0.0",
}
if !cmp.Equal(releases, want) {
t.Errorf("Repositories.GenerateReleaseNotes returned %+v, want %+v", releases, want)
}

const methodName = "GenerateReleaseNotes"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.GenerateReleaseNotes(ctx, "\n", "\n", opt)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.GenerateReleaseNotes(ctx, "o", "r", opt)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestRepositoriesService_GetRelease(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down Expand Up @@ -167,6 +208,7 @@ func TestRepositoriesService_CreateRelease(t *testing.T) {
input := &RepositoryRelease{
Name: String("v1.0"),
DiscussionCategoryName: String("General"),
GenerateReleaseNotes: Bool(true),
// Fields to be removed:
ID: Int64(2),
CreatedAt: &Timestamp{referenceTime},
Expand All @@ -190,6 +232,7 @@ func TestRepositoriesService_CreateRelease(t *testing.T) {
want := &repositoryReleaseRequest{
Name: String("v1.0"),
DiscussionCategoryName: String("General"),
GenerateReleaseNotes: Bool(true),
}
if !cmp.Equal(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
Expand Down Expand Up @@ -230,6 +273,7 @@ func TestRepositoriesService_EditRelease(t *testing.T) {
input := &RepositoryRelease{
Name: String("n"),
DiscussionCategoryName: String("General"),
GenerateReleaseNotes: Bool(true),
// Fields to be removed:
ID: Int64(2),
CreatedAt: &Timestamp{referenceTime},
Expand All @@ -253,6 +297,7 @@ func TestRepositoriesService_EditRelease(t *testing.T) {
want := &repositoryReleaseRequest{
Name: String("n"),
DiscussionCategoryName: String("General"),
GenerateReleaseNotes: Bool(true),
}
if !cmp.Equal(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
Expand Down

0 comments on commit d1858a3

Please sign in to comment.