Skip to content

Commit

Permalink
Create a release pull request with only commits
Browse files Browse the repository at this point in the history
  • Loading branch information
odanado committed Nov 17, 2024
1 parent 62be2d6 commit fbf1fba
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 15 deletions.
13 changes: 13 additions & 0 deletions git-pr-release.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
Release {{date}}
# PRs

{{#pull_requests}}
- #{{number}}
{{/pull_requests}}
{{^pull_requests}}
Nothing.
{{/pull_requests}}

# Commits

{{#commits}}
- [{{commit.message}}]({{sha}})
{{/commits}}
{{^commits}}
Nothing.
{{/commits}}
45 changes: 45 additions & 0 deletions github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,51 @@ func (c *GithubClient) FetchPullRequests(ctx context.Context, prNumbers []int) (
return pullRequests, nil
}

func (c *GithubClient) FetchChanges(ctx context.Context, from string, to string) (int, []github.PullRequest, []github.RepositoryCommit, error) {
commitsComparison, _, err := c.client.Repositories.CompareCommits(ctx, c.owner, c.repo, to, from, nil)

if err != nil {
return 0, nil, nil, err
}

if *commitsComparison.TotalCommits == 0 {
return 0, nil, nil, nil
}

pullRequests := []github.PullRequest{}
commits := []github.RepositoryCommit{}

for i := 0; i < len(commitsComparison.Commits); i++ {
commit := commitsComparison.Commits[i]

_pullRequests, _, err := c.client.PullRequests.ListPullRequestsWithCommit(ctx, c.owner, c.repo, commit.GetSHA(), nil)
if err != nil {
return 0, nil, nil, err
}

if len(_pullRequests) > 0 {
for j := 0; j < len(_pullRequests); j++ {
pullRequests = append(pullRequests, *_pullRequests[j])
}
} else {
commits = append(commits, *commit)
}
}

slices.SortFunc(pullRequests, func(a, b github.PullRequest) int {
return a.MergedAt.Compare(b.MergedAt.Time)
})
uniquePullRequests := slices.CompactFunc(pullRequests, func(a, b github.PullRequest) bool {
return a.MergedAt.Equal(*b.MergedAt)
})

slices.SortFunc(commits, func(a, b github.RepositoryCommit) int {
return a.Commit.Author.Date.Compare(b.Commit.Author.Date.Time)
})

return *commitsComparison.TotalCommits, uniquePullRequests, commits, nil
}

func (c *GithubClient) CreatePullRequest(ctx context.Context, title, body, from, to string) (*github.PullRequest, bool, error) {
prs, _, err := c.client.PullRequests.List(ctx, c.owner, c.repo, &github.PullRequestListOptions{
Base: to,
Expand Down
16 changes: 6 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,24 @@ func run(options Options) (*Result, error) {

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

prNumbers, err := client.FetchPullRequestNumbers(ctx, from, to)
totalCommits, pullRequests, commits, err := client.FetchChanges(ctx, from, to)
if err != nil {
return nil, err
}

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

logger.Println("Found pull requests: ", prNumbers)

pullRequests, err := client.FetchPullRequests(ctx, prNumbers)

if err != nil {
return nil, err
}
logger.Println("Found pull requests: ", len(pullRequests))
logger.Println("Found commits: ", len(commits))

currentTime := time.Now()
date := currentTime.Format("2006-01-02")
renderTemplateData := RenderTemplateData{
PullRequests: pullRequests,
Commits: commits,
Date: date,
From: from,
To: to,
Expand Down
11 changes: 6 additions & 5 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ func readTemplate(filename *string) (string, error) {
}

type RenderTemplateData struct {
PullRequests []github.PullRequest `json:"pull_requests"`
Date string `json:"date"`
From string `json:"from"`
To string `json:"to"`
CustomParameters any `json:"custom_parameters"`
PullRequests []github.PullRequest `json:"pull_requests"`
Commits []github.RepositoryCommit `json:"commits"`
Date string `json:"date"`
From string `json:"from"`
To string `json:"to"`
CustomParameters any `json:"custom_parameters"`
}

func convertJson(data RenderTemplateData) (any, error) {
Expand Down
5 changes: 5 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ func TestRenderTemplateWithFilename(t *testing.T) {
Number: github.Int(2),
},
},
Commits: []github.RepositoryCommit{
{
SHA: github.String("1234567890"),
},
},
Date: "2021-01-01",
}

Expand Down

0 comments on commit fbf1fba

Please sign in to comment.