Skip to content

Commit fbf1fba

Browse files
committed
Create a release pull request with only commits
1 parent 62be2d6 commit fbf1fba

5 files changed

+75
-15
lines changed

git-pr-release.mustache

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
Release {{date}}
22
# PRs
3+
34
{{#pull_requests}}
45
- #{{number}}
56
{{/pull_requests}}
7+
{{^pull_requests}}
8+
Nothing.
9+
{{/pull_requests}}
10+
11+
# Commits
12+
13+
{{#commits}}
14+
- [{{commit.message}}]({{sha}})
15+
{{/commits}}
16+
{{^commits}}
17+
Nothing.
18+
{{/commits}}

github_client.go

+45
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,51 @@ func (c *GithubClient) FetchPullRequests(ctx context.Context, prNumbers []int) (
8585
return pullRequests, nil
8686
}
8787

88+
func (c *GithubClient) FetchChanges(ctx context.Context, from string, to string) (int, []github.PullRequest, []github.RepositoryCommit, error) {
89+
commitsComparison, _, err := c.client.Repositories.CompareCommits(ctx, c.owner, c.repo, to, from, nil)
90+
91+
if err != nil {
92+
return 0, nil, nil, err
93+
}
94+
95+
if *commitsComparison.TotalCommits == 0 {
96+
return 0, nil, nil, nil
97+
}
98+
99+
pullRequests := []github.PullRequest{}
100+
commits := []github.RepositoryCommit{}
101+
102+
for i := 0; i < len(commitsComparison.Commits); i++ {
103+
commit := commitsComparison.Commits[i]
104+
105+
_pullRequests, _, err := c.client.PullRequests.ListPullRequestsWithCommit(ctx, c.owner, c.repo, commit.GetSHA(), nil)
106+
if err != nil {
107+
return 0, nil, nil, err
108+
}
109+
110+
if len(_pullRequests) > 0 {
111+
for j := 0; j < len(_pullRequests); j++ {
112+
pullRequests = append(pullRequests, *_pullRequests[j])
113+
}
114+
} else {
115+
commits = append(commits, *commit)
116+
}
117+
}
118+
119+
slices.SortFunc(pullRequests, func(a, b github.PullRequest) int {
120+
return a.MergedAt.Compare(b.MergedAt.Time)
121+
})
122+
uniquePullRequests := slices.CompactFunc(pullRequests, func(a, b github.PullRequest) bool {
123+
return a.MergedAt.Equal(*b.MergedAt)
124+
})
125+
126+
slices.SortFunc(commits, func(a, b github.RepositoryCommit) int {
127+
return a.Commit.Author.Date.Compare(b.Commit.Author.Date.Time)
128+
})
129+
130+
return *commitsComparison.TotalCommits, uniquePullRequests, commits, nil
131+
}
132+
88133
func (c *GithubClient) CreatePullRequest(ctx context.Context, title, body, from, to string) (*github.PullRequest, bool, error) {
89134
prs, _, err := c.client.PullRequests.List(ctx, c.owner, c.repo, &github.PullRequestListOptions{
90135
Base: to,

main.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,24 @@ func run(options Options) (*Result, error) {
110110

111111
client := NewClient(GithubClientOptions{owner: options.owner, repo: options.repo, githubToken: options.gitHubToken, apiUrl: options.apiUrl})
112112

113-
prNumbers, err := client.FetchPullRequestNumbers(ctx, from, to)
113+
totalCommits, pullRequests, commits, err := client.FetchChanges(ctx, from, to)
114114
if err != nil {
115115
return nil, err
116116
}
117117

118-
if len(prNumbers) == 0 {
119-
logger.Println("No pull requests were found for the release. Nothing to do.")
118+
if totalCommits == 0 {
119+
logger.Println("No pull requests or commits were found for the release. Nothing to do.")
120120
return nil, nil
121121
}
122122

123-
logger.Println("Found pull requests: ", prNumbers)
124-
125-
pullRequests, err := client.FetchPullRequests(ctx, prNumbers)
126-
127-
if err != nil {
128-
return nil, err
129-
}
123+
logger.Println("Found pull requests: ", len(pullRequests))
124+
logger.Println("Found commits: ", len(commits))
130125

131126
currentTime := time.Now()
132127
date := currentTime.Format("2006-01-02")
133128
renderTemplateData := RenderTemplateData{
134129
PullRequests: pullRequests,
130+
Commits: commits,
135131
Date: date,
136132
From: from,
137133
To: to,

template.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ func readTemplate(filename *string) (string, error) {
2727
}
2828

2929
type RenderTemplateData struct {
30-
PullRequests []github.PullRequest `json:"pull_requests"`
31-
Date string `json:"date"`
32-
From string `json:"from"`
33-
To string `json:"to"`
34-
CustomParameters any `json:"custom_parameters"`
30+
PullRequests []github.PullRequest `json:"pull_requests"`
31+
Commits []github.RepositoryCommit `json:"commits"`
32+
Date string `json:"date"`
33+
From string `json:"from"`
34+
To string `json:"to"`
35+
CustomParameters any `json:"custom_parameters"`
3536
}
3637

3738
func convertJson(data RenderTemplateData) (any, error) {

template_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func TestRenderTemplateWithFilename(t *testing.T) {
151151
Number: github.Int(2),
152152
},
153153
},
154+
Commits: []github.RepositoryCommit{
155+
{
156+
SHA: github.String("1234567890"),
157+
},
158+
},
154159
Date: "2021-01-01",
155160
}
156161

0 commit comments

Comments
 (0)