Skip to content

Commit

Permalink
feat: doc-changelog conventional commits (#515)
Browse files Browse the repository at this point in the history
Co-authored-by: Roberto Pastor Muela <[email protected]>
Co-authored-by: Sébastien Morais <[email protected]>
  • Loading branch information
3 people authored Aug 1, 2024
1 parent 3210c92 commit af547f1
Show file tree
Hide file tree
Showing 4 changed files with 472 additions and 53 deletions.
153 changes: 104 additions & 49 deletions doc-changelog/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ inputs:
required: false
type: string

toml-version:
description: >
Toml version used for retrieving the towncrier directory.
default: '0.10.2'
required: false
type: string

use-python-cache:
description: >
Whether to use the Python cache for installing previously downloaded
Expand All @@ -65,6 +72,20 @@ inputs:
default: true
type: boolean

use-conventional-commits:
description: >
Use conventional commits to cateogrize towncrier fragments.
required: false
default: false
type: boolean

use-default-towncrier-config:
description: >
Use the default towncrier configuration in the pyproject.toml file.
required: false
default: false
type: boolean

runs:
using: "composite"
steps:
Expand All @@ -89,9 +110,51 @@ runs:
- name: "Install towncrier"
shell: bash
run: |
python -m pip install --upgrade pip towncrier==${{ inputs.towncrier-version }}
python -m pip install --upgrade pip towncrier==${{ inputs.towncrier-version }} toml==${{ inputs.toml-version }}
- name: "Get first letter of conventional commit type"
if: ${{ inputs.use-conventional-commits == 'true' }}
env:
PR_TITLE: ${{ github.event.pull_request.title }}
shell: python
run: |
import sys
sys.path.insert(1, '${{ github.action_path }}/../doc-changelog/')
from parse_pr_title import get_first_letter_case
pr_title = """${{ env.PR_TITLE }}"""
get_first_letter_case(pr_title)
- name: "Check pull-request title follows conventional commits style"
if: ${{ (inputs.use-conventional-commits == 'true') && (env.FIRST_LETTER == 'lowercase') }}
uses: ansys/actions/commit-style@v6
with:
token: ${{ inputs.token }}

- name: "Check pull-request title follows conventional commits style with upper case"
if: ${{ (inputs.use-conventional-commits == 'true') && (env.FIRST_LETTER == 'uppercase') }}
uses: ansys/actions/commit-style@v6
with:
token: ${{ inputs.token }}
use-upper-case: true

- name: "Get conventional commit type from title"
if: ${{ inputs.use-conventional-commits == 'true' }}
env:
PR_TITLE: ${{ github.event.pull_request.title }}
shell: python
run: |
import sys
sys.path.insert(1, '${{ github.action_path }}/../doc-changelog/')
from parse_pr_title import get_conventional_commit_type
pr_title = """${{ env.PR_TITLE }}"""
get_conventional_commit_type(pr_title)
- name: "Get labels in the pull request"
if: ${{ inputs.use-conventional-commits == 'false' }}
env:
OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
Expand All @@ -106,44 +169,29 @@ runs:
# For example, LABELS="enhancement maintenance"
echo LABELS='"'$pr_labels'"' >> $GITHUB_ENV
- name: "Set CHANGELOG category based on conventional commit type"
if: ${{ inputs.use-conventional-commits == 'true' }}
shell: python
run: |
import sys
sys.path.insert(1, '${{ github.action_path }}/../doc-changelog/')
from parse_pr_title import changelog_category_cc
cc_type = ${{ env.CC_TYPE }}
changelog_category_cc(cc_type)
- name: "Set PR label environment variable"
if: ${{ inputs.use-conventional-commits == 'false' }}
shell: python
run: |
import os
# Create a list of labels found in the pull request
# For example, "enhancement maintenance".split() -> ["enhancement", "maintenance"]
existing_labels = ${{ env.LABELS }}.split()
# Dictionary with the key as a label from .github/workflows/label.yml and
# value as the corresponding section in the changelog
pr_labels = {
"enhancement": "added",
"bug": "fixed",
"dependencies": "dependencies",
"maintenance": "changed"
}
def get_changelog_section(pr_labels, existing_labels):
"""Find the changelog section corresponding to the label in the PR."""
label_type = ""
for key, value in pr_labels.items():
if key in existing_labels:
label_type = value
return label_type
# If no labels are in the PR, it goes into the miscellaneous category
label_type = "miscellaneous"
return label_type
# Get the GITHUB_ENV variable
github_env = os.getenv('GITHUB_ENV')
# Append the PR_LABEL with its value to GITHUB_ENV
# For example, PR_LABEL="added" if the PR had an "enhancement" label
with open(github_env, "a") as f:
f.write(f"PR_LABEL={get_changelog_section(pr_labels, existing_labels)}")
import sys
sys.path.insert(1, '${{ github.action_path }}/../doc-changelog/')
from parse_pr_title import changelog_cateogry_labels
labels = ${{ env.LABELS }}
changelog_cateogry_labels(labels)
- name: "Remove PR fragment file if it already exists"
env:
Expand All @@ -165,23 +213,30 @@ runs:
PR_TITLE: ${{ github.event.pull_request.title }}
shell: python
run: |
import os
import sys
sys.path.insert(1, '${{ github.action_path }}/../doc-changelog/')
# Retrieve title
clean_title = os.getenv('PR_TITLE')
from parse_pr_title import clean_pr_title
pr_title = """${{ env.PR_TITLE }}"""
use_cc = True if "${{ inputs.use-conventional-commits }}" == "true" else False
clean_pr_title(pr_title, use_cc)
- name: "Append towncrier categories to pyproject.toml"
shell: python
run: |
import sys
sys.path.insert(1, '${{ github.action_path }}/../doc-changelog/')
# Remove extra whitespace
clean_title = clean_title.strip()
from parse_pr_title import add_towncrier_config
# Add backslash in front of backtick and double quote
clean_title = clean_title.replace("`", "\`").replace('"', '\\"')
repo_name = "${{ github.event.repository.name }}"
org_name = "${{ github.repository_owner }}"
# Get the GITHUB_ENV variable
github_env = os.getenv('GITHUB_ENV')
default_config = True if "${{ inputs.use-default-towncrier-config }}" == "true" else False
# Append the CLEAN_TITLE with its value to GITHUB_ENV
with open(github_env, "a") as f:
f.write(f"CLEAN_TITLE={clean_title}")
add_towncrier_config(org_name, repo_name, default_config)
- name: "Create and commit towncrier fragment"
env:
Expand All @@ -191,7 +246,7 @@ runs:
run: |
# Changelog fragment file in the following format
# For example, 20.added.md
fragment="${{ env.PR_NUMBER }}.${{ env.PR_LABEL }}.md"
fragment="${{ env.PR_NUMBER }}.${{ env.CHANGELOG_SECTION }}.md"
# Create changelog fragment with towncrier
# Fragment file contains the title of the PR
Expand Down
Loading

0 comments on commit af547f1

Please sign in to comment.