Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add "Create Release PR" composite action #7

Merged
merged 10 commits into from
May 31, 2024
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
## Usage
Follow the [instruction](https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#calling-a-reusable-workflow) from Github to use any workflows in this repository.

- [Automate creating the Release pull request](create_release_pull_request/README.md)

## How to contribute
- Refer to the [official guide](https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#creating-a-reusable-workflow) to learn how to define a reusable workflow
- Put the new workflow under `./github/workflows` directory
Expand Down
6 changes: 6 additions & 0 deletions create_release_pull_request/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Automate creating the Release pull request

## Usage

- Create a `create_release_pull_request.yml` workflow, sample [here](sample/workflows/create_release_pull_request.yml).
- Provide a release changelog configuration file, sample [here](sample/workflows/config/changelog-release.json).
102 changes: 102 additions & 0 deletions create_release_pull_request/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
name: Automate creating the Release pull request
description: Automate creating the Release pull request
inputs:
release_version:
description: Release version
required: true
changelog_configuration:
description: The changelog configuration file path, e.g., ".github/workflows/config/changelog-release.json"
required: true
default: ".github/workflows/config/changelog_release.json"
github_token:
description: The GitHub PAT for this action to use
required: false
default: ${{ github.token }}
base_branch:
description: The base branch for the release pull request, e.g., "main"
required: false
default: main
assignee:
description: The assignee for the Release pull request, e.g., bot
required: false
label:
description: 'The label for the Release pull request, e.g., "type : release"'
required: false
default: "type : release"
release_body_url:
description: The URL to put in the release body. If not set, the GitHub Milestone (= release_version) URL will be used.
required: false

runs:
using: composite
steps:
- name: Find the HEAD commit
id: find_head_commit
shell: bash
run: echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT

- name: Find the latest ${{ inputs.base_branch }} commit
id: find_latest_base_commit
shell: bash
run: |
git fetch origin ${{ inputs.base_branch }}
echo "sha=$(git rev-parse origin/${{ inputs.base_branch }})" >> $GITHUB_OUTPUT

- name: Generate changelog
id: generate_changelog
uses: mikepenz/release-changelog-builder-action@v4
with:
token: ${{ inputs.github_token }}
configuration: ${{ inputs.changelog_configuration }}
outputFile: body_content.txt
fromTag: ${{ steps.find_latest_base_commit.outputs.sha }}
toTag: ${{ steps.find_head_commit.outputs.sha }}

- name: Find milestone
id: find_milestone
env:
GH_TOKEN: ${{ inputs.github_token }}
shell: bash
run: |
gh extension install valeriobelli/gh-milestone
MILESTONE=${{ inputs.release_version }}
MILESTONE_URL=$(gh milestone list --query $MILESTONE --json url --jq ".[0].url")
echo "milestone=$MILESTONE" >> $GITHUB_OUTPUT
echo "milestone_url=$MILESTONE_URL" >> $GITHUB_OUTPUT

- name: Prepend Release URL into the changelog
shell: bash
run: |
if [ -n "${{ inputs.release_body_url }}" ]; then
echo -e "${{ inputs.release_body_url }}\n\n$(cat body_content.txt)" > body_content.txt
else
echo -e "${{ steps.find_milestone.outputs.milestone_url }}\n\n$(cat body_content.txt)" > body_content.txt
fi

- name: Create the Release pull request
env:
GH_TOKEN: ${{ inputs.github_token }}
shell: bash
run: |
RELEASE_BRANCH=release/${{ inputs.release_version }}

# Create the release branch
git checkout -b $RELEASE_BRANCH
git push origin $RELEASE_BRANCH -f

# Add milestone if available
if [ -n "${{ steps.find_milestone.outputs.milestone_url }}" ]; then
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: if milestone_url is found, that means the milestone from the release version is available from the repo.

MILESTONE_PARAM="--milestone ${{ steps.find_milestone.outputs.milestone }}"
else
MILESTONE_PARAM=""
fi

# Create the pull request
gh pr create \
--base ${{ inputs.base_branch }} \
--head $RELEASE_BRANCH \
--assignee ${{ inputs.assignee }} \
--title "Release - ${{ inputs.release_version }}" \
--label "${{ inputs.label }}" \
$MILESTONE_PARAM \
--body-file body_content.txt \
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"categories": [
{
"title": "## ✨ Features",
"labels": [
"type : feature"
],
"empty_content": "N/A"
},
{
"title": "## 🐛 Bug fixes",
"labels": [
"type : bug"
],
"empty_content": "N/A"
},
{
"title": "## 🧹 Chores",
"labels": [
"type : chore"
],
"empty_content": "N/A"
},
{
"title": "## Others",
"exclude_labels": [
"type : feature",
"type : bug",
"type : chore",
"type : release"
]
}
],
"max_pull_requests": 200
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Create the Release pull request

on:
workflow_dispatch:

jobs:
create_release_pull_request:
name: Create the Release pull request
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Check out
uses: actions/checkout@v4

- name: Read the current version
id: version
uses: christian-draeger/[email protected]
with:
path: "version.properties"
properties: "version"

- uses: nimblehq/github-actions-workflows/[email protected]
with:
release_version: ${{ steps.version.outputs.version }}
changelog_configuration: ".github/workflows/config/changelog-release.json"
assignee: bot-nimble