|
| 1 | +package ghrn |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/google/go-github/github" |
| 11 | + "golang.org/x/oauth2" |
| 12 | +) |
| 13 | + |
| 14 | +// Config describes configuration for BuildReleaseNotes. |
| 15 | +type Config struct { |
| 16 | + // Org is the name of the GitHub organization. Required. |
| 17 | + Org string |
| 18 | + // Repo is the name of the GitHub repository. Required. |
| 19 | + Repo string |
| 20 | + |
| 21 | + // GitHubToken is a GitHub API access token. |
| 22 | + GitHubToken string |
| 23 | + |
| 24 | + // StopAt is the number of the Pull Request to stop at. |
| 25 | + // Useful for building the notes of PRs since the last release, for example. |
| 26 | + StopAt int |
| 27 | + // IncludeCommits will include commmits messages for each PR. |
| 28 | + IncludeCommits bool |
| 29 | +} |
| 30 | + |
| 31 | +// BuildReleaseNotes lists GitHub Pull Requests and writes formatted release notes |
| 32 | +// to the given writer. |
| 33 | +func BuildReleaseNotes(ctx context.Context, w io.Writer, conf Config) error { |
| 34 | + |
| 35 | + if conf.Org == "" { |
| 36 | + return fmt.Errorf("Config.Org is required") |
| 37 | + } |
| 38 | + if conf.Repo == "" { |
| 39 | + return fmt.Errorf("Config.Repo is required") |
| 40 | + } |
| 41 | + |
| 42 | + var httpClient *http.Client |
| 43 | + if conf.GitHubToken != "" { |
| 44 | + ts := oauth2.StaticTokenSource( |
| 45 | + &oauth2.Token{AccessToken: conf.GitHubToken}, |
| 46 | + ) |
| 47 | + httpClient = oauth2.NewClient(ctx, ts) |
| 48 | + } |
| 49 | + cl := github.NewClient(httpClient) |
| 50 | + |
| 51 | + opt := &github.PullRequestListOptions{ |
| 52 | + ListOptions: github.ListOptions{PerPage: 100}, |
| 53 | + State: "closed", |
| 54 | + } |
| 55 | + |
| 56 | + // Iterate over all PRs |
| 57 | + for { |
| 58 | + prs, resp, err := cl.PullRequests.List(ctx, conf.Org, conf.Repo, opt) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("listing PRs: %s", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Iterate over PRs in this page. |
| 64 | + for _, pr := range prs { |
| 65 | + if *pr.Number == conf.StopAt { |
| 66 | + return nil |
| 67 | + } |
| 68 | + if pr.MergedAt == nil { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + fmt.Fprintf(w, "- PR #%d %s\n", pr.GetNumber(), pr.GetTitle()) |
| 73 | + |
| 74 | + if conf.IncludeCommits { |
| 75 | + // Iterate over all commits in this PR. |
| 76 | + commitOpt := &github.ListOptions{PerPage: 100} |
| 77 | + for { |
| 78 | + |
| 79 | + commits, resp, err := cl.PullRequests.ListCommits(ctx, conf.Org, conf.Repo, pr.GetNumber(), commitOpt) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("listing PR commits: %s", err) |
| 82 | + } |
| 83 | + |
| 84 | + // Iterate over commits in this page. |
| 85 | + for _, commit := range commits { |
| 86 | + sha := *commit.SHA |
| 87 | + msg := *commit.Commit.Message |
| 88 | + |
| 89 | + // Strip multiple lines (i.e. only take first line) |
| 90 | + if i := strings.Index(msg, "\n"); i != -1 { |
| 91 | + msg = msg[:i] |
| 92 | + } |
| 93 | + // Trim long lines |
| 94 | + if len(msg) > 90 { |
| 95 | + msg = msg[:90] + "..." |
| 96 | + } |
| 97 | + msg = strings.TrimSpace(msg) |
| 98 | + |
| 99 | + fmt.Fprintf(w, " - %s %s\n", sha, msg) |
| 100 | + } |
| 101 | + |
| 102 | + if resp.NextPage == 0 { |
| 103 | + break |
| 104 | + } |
| 105 | + commitOpt.Page = resp.NextPage |
| 106 | + } |
| 107 | + fmt.Fprintln(w) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if resp.NextPage == 0 { |
| 112 | + break |
| 113 | + } |
| 114 | + opt.Page = resp.NextPage |
| 115 | + } |
| 116 | + return nil |
| 117 | +} |
0 commit comments