Skip to content

Commit

Permalink
feat: fetches changes since last gh release and thus emoving from flag
Browse files Browse the repository at this point in the history
The reason for that is following:

- from is not feasible for automated release, as at this point, when
triggered by a tag it only knows current release tag. Therefore lookup
for previous tag is required which turns out to be quite cumbersome with
gh graphql. Querying with cursore and (first: 1, before: "cursor", ...)
cannot be based on gh oid (hash), even though query is not invalid
(cursor is accepted). But it always returns first ever tags. Technically
we could retrieve all tags (or all releases) and just iterate to find
previous tag, but that seems to be an overhead and fits only marginal
usecases.
- most of the times usage of the tool will be for the latest release, so
no need for lookup previous tags, but last release

Not working yet - first use for the first release - no releases on GH
yet. This will currently fail - we have to lookup the very first commit
  • Loading branch information
bartoszmajsak committed Dec 7, 2019
1 parent 5ab151d commit af41135
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 4 additions & 7 deletions pkg/cmd/generate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

func NewCmd() *cobra.Command {
var (
from,
repo,
format,
tag string
Expand All @@ -25,7 +24,7 @@ func NewCmd() *cobra.Command {
Short: "Generates changelog for a given from",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { //nolint[:unparam]
pullRequests := fetchRelatedPRs(repo, from)
pullRequests := fetchPRsSinceLastRelease(repo)
sortDependencyPRs(pullRequests)
tpl := Default
if cmd.Flag("format").Value.String() == "adoc" {
Expand All @@ -44,20 +43,18 @@ func NewCmd() *cobra.Command {

generateCmd.Flags().StringVarP(&tag, "tag", "t", "UNRELEASED", "tag used for current release")
generateCmd.Flags().StringVar(&format, "format", "md", "format of generated release notes")
generateCmd.Flags().StringVarP(&from, "from", "f", "", "from for which changelog should be generated")
generateCmd.Flags().StringVarP(&repo, "repository", "r", "", "repository URL")

_ = generateCmd.MarkFlagRequired("repository")
_ = generateCmd.MarkFlagRequired("from")
return generateCmd
}

func fetchRelatedPRs(repoName, ref string) map[string][]github.PullRequest {
func fetchPRsSinceLastRelease(repoName string) map[string][]github.PullRequest {
check.RepoFormat(repoName)
repo := strings.Split(repoName, "/")
client := github.CreateClient()

matchingCommit := github.FindMatchingCommit(client, repo, ref)
previousRelease, _ := github.LatestReleaseOf(repo[0], repo[1])
matchingCommit := github.FindMatchingCommit(client, repo, previousRelease)
prs := github.FindAssociatedPRs(client, repo, matchingCommit)

prsByLabels := make(map[string][]github.PullRequest)
Expand Down
6 changes: 5 additions & 1 deletion pkg/github/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
)

func LatestRelease() (string, error) {
return LatestReleaseOf("bartoszmajsak", "github-changelog-generator")
}

func LatestReleaseOf(owner, repo string) (string, error) {
httpClient := http.Client{}
defer httpClient.CloseIdleConnections()

client := github.NewClient(&httpClient)
latestRelease, _, err := client.Repositories.
GetLatestRelease(context.Background(), "bartoszmajsak", "github-changelog-generator")
GetLatestRelease(context.Background(), owner, repo)
if err != nil {
return "", err
}
Expand Down

0 comments on commit af41135

Please sign in to comment.