diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 544a3c8..72d1368 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -51,7 +51,7 @@ This file enables Derek usage for `rgee0` and `alexellis`, it also turns on all ### Feature: `release_notes` -Derek will collate closed PRs since the last release and then put together a summary and set it for your release text body. +Derek will collate closed PRs since the previous full release and then put together a summary and set it for your release text body. Example from [openfaas/faas-cli](https://github.com/openfaas/faas-cli/releases/tag/0.9.5): diff --git a/handler/release_handler.go b/handler/release_handler.go index cc7bac4..35ae248 100644 --- a/handler/release_handler.go +++ b/handler/release_handler.go @@ -172,19 +172,26 @@ func buildClosedPRs(client *github.Client, workingReleases WorkingRelease, owner func getWorkingReleases(releases []*github.RepositoryRelease, owner, repo, tag string) WorkingRelease { rel := WorkingRelease{} - var count int - var r *github.RepositoryRelease - - for count, r = range releases { + for position, r := range releases { if r.GetTagName() == tag { rel.CurrentDate = r.CreatedAt.Time rel.CurrentTag = tag rel.CurrentRelease = r - if count+1 < len(releases) { - prior := releases[count+1] - rel.PreviousDate = prior.CreatedAt.Time - rel.PreviousTag = prior.GetTagName() + prevRel := position + 1 + + for prevRel < len(releases) { + + prior := releases[prevRel] + + if !*prior.Prerelease { + rel.PreviousDate = prior.CreatedAt.Time + rel.PreviousTag = prior.GetTagName() + break + } + + prevRel++ + } break diff --git a/handler/release_handler_test.go b/handler/release_handler_test.go index 4961f40..07a790e 100644 --- a/handler/release_handler_test.go +++ b/handler/release_handler_test.go @@ -77,6 +77,7 @@ func Test_getWorkingReleases_TwoReleases(t *testing.T) { repo := "derek" tag := "0.2.0" lastTag := "0.1.0" + prerelease := false releases := []*github.RepositoryRelease{ &github.RepositoryRelease{ @@ -84,12 +85,14 @@ func Test_getWorkingReleases_TwoReleases(t *testing.T) { CreatedAt: &github.Timestamp{ Time: time.Now(), }, + Prerelease: &prerelease, }, &github.RepositoryRelease{ TagName: &lastTag, CreatedAt: &github.Timestamp{ Time: time.Now().Add(time.Hour * -1), }, + Prerelease: &prerelease, }, } workingReleases := getWorkingReleases(releases, owner, repo, tag) @@ -120,6 +123,7 @@ func Test_getWorkingReleases_OneRelease(t *testing.T) { owner := "alexellis" repo := "derek" tag := "0.2.0" + prerelease := false releases := []*github.RepositoryRelease{ &github.RepositoryRelease{ @@ -127,6 +131,7 @@ func Test_getWorkingReleases_OneRelease(t *testing.T) { CreatedAt: &github.Timestamp{ Time: time.Now(), }, + Prerelease: &prerelease, }, } workingReleases := getWorkingReleases(releases, owner, repo, tag) @@ -203,3 +208,59 @@ func Test_includeCommit_WithinCurrentRange(t *testing.T) { t.Fail() } } + +func Test_getWorkingReleases_ThreeReleasesOnePreRelease(t *testing.T) { + owner := "alexellis" + repo := "derek" + tag := "0.2.0" + midTag := "0.1.5" + midPreRelease := true + lastTag := "0.1.0" + preRelease := false + + releases := []*github.RepositoryRelease{ + &github.RepositoryRelease{ + TagName: &tag, + CreatedAt: &github.Timestamp{ + Time: time.Now(), + }, + Prerelease: &preRelease, + }, + &github.RepositoryRelease{ + TagName: &midTag, + CreatedAt: &github.Timestamp{ + Time: time.Now().Add(time.Hour * -1), + }, + Prerelease: &midPreRelease, + }, + &github.RepositoryRelease{ + TagName: &lastTag, + CreatedAt: &github.Timestamp{ + Time: time.Now().Add(time.Hour * -2), + }, + Prerelease: &preRelease, + }, + } + workingReleases := getWorkingReleases(releases, owner, repo, tag) + + gotCurrentDate := workingReleases.CurrentDate + wantCurrentDate := releases[0].GetCreatedAt().Time + + if gotCurrentDate != wantCurrentDate { + t.Errorf("current date, got: %s, want: %s", gotCurrentDate, wantCurrentDate) + } + + gotPreviousDate := workingReleases.PreviousDate + wantPreviousDate := releases[2].GetCreatedAt().Time + + if gotPreviousDate != wantPreviousDate { + t.Errorf("previous date, got: %s, want: %s", gotPreviousDate, wantPreviousDate) + } + + gotPreviousTag := workingReleases.PreviousTag + wantPreviousTag := *releases[2].TagName + + if gotPreviousTag != wantPreviousTag { + t.Errorf("previous tag, got: %s, want: %s", gotPreviousTag, wantPreviousTag) + } +}