Skip to content

Commit

Permalink
feat: fetching release details using GH GraphQL
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmajsak committed Sep 19, 2019
1 parent c8624ff commit 1004941
Show file tree
Hide file tree
Showing 10 changed files with 432 additions and 75 deletions.
58 changes: 58 additions & 0 deletions Gopkg.lock

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

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
version = "=v0.0.5"

[[constraint]]
name = "github.com/google/go-github"
version = "=v28.1.1"
name = "github.com/shurcooL/githubv4"
revision = "4ba037080260cb26279c05337b38064b520a7f9e"

# Workaround for https://github.com/golang/dep/issues/1799
# Otherwise ginkgo/gomega won't be pulled in
Expand Down
79 changes: 6 additions & 73 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,83 +1,16 @@
package main

import (
"fmt"
"strings"
"time"

"github.com/bartoszmajsak/github-changelog-generator/pkg/check"
"github.com/bartoszmajsak/github-changelog-generator/pkg/cmd"
"github.com/bartoszmajsak/github-changelog-generator/pkg/cmd/generate"
"github.com/bartoszmajsak/github-changelog-generator/pkg/cmd/version"
"github.com/bartoszmajsak/github-changelog-generator/pkg/config"
"github.com/bartoszmajsak/github-changelog-generator/pkg/format"
v "github.com/bartoszmajsak/github-changelog-generator/version"

"github.com/spf13/cobra"
)

func main() {
rootCmd := newCmd()

rootCmd.AddCommand(version.NewCmd())

rootCmd := cmd.NewCmd()
rootCmd.AddCommand(generate.NewCmd(), version.NewCmd())
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}

func newCmd() *cobra.Command {
var configFile string
releaseInfo := make(chan string, 1)

rootCmd := &cobra.Command{
Use: "cmd",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { //nolint[:unparam]
if v.Released() {
go func() {
latestRelease, _ := version.LatestRelease()
if !version.IsLatestRelease(latestRelease) {
releaseInfo <- fmt.Sprintf("WARN: you are using %s which is not the latest release (newest is %s).\n"+
"Follow release notes for update info https://github.com/Maistra/istio-workspace/releases/latest", v.Version, latestRelease)
} else {
releaseInfo <- ""
}
}()
}
return config.SetupConfigSources(configFile, cmd.Flag("config").Changed)
},
RunE: func(cmd *cobra.Command, args []string) error { //nolint[:unparam]
shouldPrintVersion, _ := cmd.Flags().GetBool("version")
if shouldPrintVersion {
version.PrintVersion()
} else {
fmt.Print(cmd.UsageString())
}
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
if v.Released() {
timer := time.NewTimer(2 * time.Second)
select {
case release := <-releaseInfo:
fmt.Println(release)
case <-timer.C:
// do nothing, just timeout
}
}
close(releaseInfo)
return nil
},
}

rootCmd.PersistentFlags().
StringVarP(&configFile, "config", "c", ".ike.config.yaml",
fmt.Sprintf("config file (supported formats: %s)", strings.Join(config.SupportedExtensions(), ", ")))
rootCmd.Flags().Bool("version", false, "prints the version number of ike cli")
rootCmd.PersistentFlags().String("help-format", "standard", "prints help in asciidoc table")
if err := rootCmd.PersistentFlags().MarkHidden("help-format"); err != nil {
fmt.Printf("failed while trying to hide a flag: %s\n", err)
check.IfError(err)
}

format.EnhanceHelper(rootCmd)
format.RegisterTemplateFuncs()

return rootCmd
}
24 changes: 24 additions & 0 deletions pkg/check/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package check

import (
"fmt"
"os"
"strings"
)

// IfError checks if error occurred, logs it and exits
func IfError(err error) {
if err == nil {
return
}
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
os.Exit(1)
}

func RepoFormat(repo string) {
repoParts := strings.Split(repo, "/")
if len(repoParts) != 2 {
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("wrong repo format: %s. please make sure it's in owner/repo format", repo))
os.Exit(1)
}
}
63 changes: 63 additions & 0 deletions pkg/cmd/generate/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package generate

import (
"os"
"strings"
"text/template"

"github.com/bartoszmajsak/github-changelog-generator/pkg/github"

"github.com/bartoszmajsak/github-changelog-generator/pkg/check"

"github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
var (
from,
repo,
tag string
)
generateCmd := &cobra.Command{
Use: "generate",
Short: "Generates changelog for a given from",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { //nolint[:unparam]
pullRequests := fetchRelatedPRs(repo, from)
t, err := template.New("changelog").Parse(Default)
if err != nil {
return err
}
if err := t.Execute(os.Stdout, &Changelog{Release: tag, PullRequests: pullRequests}); err != nil {
return err
}
return nil
},
}

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

_ = generateCmd.MarkFlagRequired("for")
_ = generateCmd.MarkFlagRequired("tag")
_ = generateCmd.MarkFlagRequired("repository")
return generateCmd
}

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

matchingCommit := github.FindMatchingCommit(client, repo, ref)
prs := github.FindAssociatedPRs(client, repo, matchingCommit)

prsByLabels := make(map[string][]github.PullRequest)
for i := range prs {
pr := prs[i]
prsByLabels[pr.Labels[0]] = append(prsByLabels[pr.Labels[0]], pr)
}

return prsByLabels
}
52 changes: 52 additions & 0 deletions pkg/cmd/generate/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package generate

import "github.com/bartoszmajsak/github-changelog-generator/pkg/github"

type Changelog struct {
Release string
PullRequests map[string][]github.PullRequest
}

const Default = `
# Highlights of {{.Release }} release
## New features
{{- with $prs := (index .PullRequests "enhancement") -}}
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
{{- end -}}
{{ end }}
## Bug fixes
{{- with $prs := (index .PullRequests "bug") -}}
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
{{- end -}}
{{ end }}
## Dependencies update
{{- with $prs := (index .PullRequests "dependencies") -}}
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}}))
{{- end -}}
{{ end }}
## Project infrastructure
{{- with $prs := (index .PullRequests "infra") -}}
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
{{- end -}}
{{ end }}
## Testing
{{- with $prs := (index .PullRequests "test-infra") -}}
{{range $pr := $prs }}
* {{$pr.Title}} ([#{{$pr.Number}}]({{$pr.Permalink}})), by [@{{$pr.Author}}](https://github.com/{{$pr.Author}})
{{- end -}}
{{ end }}
`
Loading

0 comments on commit 1004941

Please sign in to comment.