Skip to content

Commit

Permalink
Merge pull request #8 from odanado/main
Browse files Browse the repository at this point in the history
Release new version: v0.2.0
  • Loading branch information
odanado authored Nov 17, 2024
2 parents b192a9d + 3f57377 commit 00dd33d
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 15 deletions.
88 changes: 88 additions & 0 deletions .github/workflows/check-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Check Release
on:
pull_request:
branches:
- release/cli
types:
- labeled
- unlabeled
- synchronize
- opened

jobs:
check-label:
runs-on: ubuntu-latest
outputs:
level: ${{ steps.release-label.outputs.level }}
steps:
- uses: actions-ecosystem/action-release-label@v1
id: release-label
with:
label_prefix: "bump:"

find-comment:
runs-on: ubuntu-latest
outputs:
comment-id: ${{ steps.fc.outputs.comment-id }}
steps:
- name: Find Comment
uses: peter-evans/find-comment@v3
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "🤖 **Check Release**"

notify-new-version:
runs-on: ubuntu-latest
needs: [check-label, find-comment]
if: ${{ needs.check-label.outputs.level != null }}
steps:
- uses: actions/checkout@v4
- uses: actions-ecosystem/action-get-latest-tag@v1
id: get-latest-tag
- uses: actions-ecosystem/action-bump-semver@v1
id: bump-semver
with:
current_version: ${{ steps.get-latest-tag.outputs.tag }}
level: ${{needs.check-label.outputs.level }}

- name: Create or update comment
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ needs.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
🤖 **Check Release**
If you merge this pull request, the version of the CLI tool will be released.
- Current Version: ${{ steps.get-latest-tag.outputs.tag }}
- New Version: ${{ steps.bump-semver.outputs.new_version }}
edit-mode: replace

- name: Update title of pull request
run: |
gh pr edit ${{ github.event.pull_request.number }} --title "Release new version: ${{ steps.bump-semver.outputs.new_version }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

notify-please-add-label:
runs-on: ubuntu-latest
needs: [check-label, find-comment]
if: ${{ needs.check-label.outputs.level == null }}
steps:
- name: Create or update comment
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ needs.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
🤖 **Check Release**
You forgot to add a label to bump the version.
Please add a label `bump:patch`, `bump:minor`, or `bump:major` to this pull request.
edit-mode: replace

- name: Update title of pull request
run: |
gh pr edit ${{ github.event.pull_request.number }} --title "Release new version"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 changes: 28 additions & 0 deletions .github/workflows/create-release-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Create Release Pull Request
on:
push:
branches:
- main

jobs:
create-release-pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: stable
- name: Build
run: go build -v ./...

- uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.PRIVATE_KEY }}

- run: ./git-pr-release-go --from main --to release/cli
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
48 changes: 48 additions & 0 deletions .github/workflows/new-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Release

on:
push:
branches:
- release/cli

jobs:
get-release-level:
runs-on: ubuntu-latest
outputs:
value: ${{ steps.release-label.outputs.level }}
steps:
- uses: actions/checkout@v4

- uses: actions-ecosystem/action-get-merged-pull-request@v1
id: get-merged-pull-request
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- uses: actions-ecosystem/action-release-label@v1
id: release-label
with:
labels: ${{ steps.get-merged-pull-request.outputs.labels }}
label_prefix: "bump:"

goreleaser:
runs-on: ubuntu-latest
needs: [get-release-level]
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: stable

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
version: ${{ needs.get-release-level.outputs.value }}
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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 00dd33d

Please sign in to comment.