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

feat: doc-changelog conventional commits #515

Merged
merged 53 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
c98b8b2
test conventional commits
klmcadams Jul 5, 2024
2f30057
reorder conventional commit checks
klmcadams Jul 5, 2024
5806c13
remove failure condition
klmcadams Jul 5, 2024
6967d44
check outcomes of steps
klmcadams Jul 5, 2024
f4feb38
add shell line
klmcadams Jul 5, 2024
a4668e3
set changelog section based on conventional commit type
klmcadams Jul 5, 2024
54b3bef
add quotes around pr_title
klmcadams Jul 5, 2024
05c6fe7
add print statements, fix syntax, and make cc_type lowercase
klmcadams Jul 5, 2024
184952d
fix cc_type?
klmcadams Jul 5, 2024
ece8be8
double quotes around cc_type env var
klmcadams Jul 5, 2024
a54e98c
fix quotes
klmcadams Jul 5, 2024
026b5b8
fix boolean
klmcadams Jul 5, 2024
eed2cb3
move quotes
klmcadams Jul 5, 2024
58f8eaa
call env var directly
klmcadams Jul 5, 2024
101b42c
double curly brackets
klmcadams Jul 5, 2024
3e3b9e9
test
klmcadams Jul 5, 2024
c9032c0
use cut command instead
klmcadams Jul 5, 2024
3283fd1
pr title directly in bash
klmcadams Jul 5, 2024
9c497e7
change bash to python for the env step
klmcadams Jul 5, 2024
98f412a
change to colon_index
klmcadams Jul 5, 2024
760cc02
wrap in double quotes
klmcadams Jul 5, 2024
c077b26
wrap string in bool
klmcadams Jul 5, 2024
6539c90
use colon_index
klmcadams Jul 5, 2024
6c77a94
add print statements
klmcadams Jul 5, 2024
e121d16
remove print statements
klmcadams Jul 5, 2024
a29eb8d
remove colon
klmcadams Jul 5, 2024
502cbc4
update sections and doc-changelog docs
klmcadams Jul 8, 2024
07b2b52
add use-labels to doc-changelog example yml file
klmcadams Jul 8, 2024
32c6eff
use python script in action
klmcadams Jul 9, 2024
e825200
add backslashes to pr_title
klmcadams Jul 9, 2024
026c3b3
import os
klmcadams Jul 9, 2024
e53b485
import functions from python script
klmcadams Jul 9, 2024
3ffa3fe
add doc-changelog to path
klmcadams Jul 9, 2024
127fda5
add sys path to all python sections
klmcadams Jul 9, 2024
92a7169
remove quotes around labels and cc_type
klmcadams Jul 9, 2024
f8a05ff
fix import statement
klmcadams Jul 9, 2024
b09e253
add quotes around use_labels
klmcadams Jul 9, 2024
6090532
remove argparse
klmcadams Jul 9, 2024
a8e5c6c
add towncrier config template, use-conventional-commits, and web-rele…
klmcadams Jul 10, 2024
b950aaa
add docstrings & separate function into smaller functions
klmcadams Jul 11, 2024
5bc1ffc
remove if statement
klmcadams Jul 11, 2024
8004d85
simplify logic
klmcadams Jul 11, 2024
b2546f2
pip install toml
klmcadams Jul 11, 2024
fc6f7ec
quotes around env
klmcadams Jul 11, 2024
68d1da7
add quotes
klmcadams Jul 11, 2024
cc91e0e
fix repo owner?
klmcadams Jul 11, 2024
f616d52
only write [tool.towncrier] section under certain conditions
klmcadams Jul 11, 2024
f3832c7
replace use-labels with use-conventional-commits
klmcadams Jul 11, 2024
c9eff2d
always check for existing types
klmcadams Jul 11, 2024
9b34ce0
check if towncrier is not DNE
klmcadams Jul 11, 2024
c67525c
Merge branch 'main' into feat/doc-changelog-cc
klmcadams Jul 29, 2024
9b98932
Merge branch 'main' into feat/doc-changelog-cc
RobPasMue Jul 31, 2024
14b9fba
feat: doc-changelog allow breaking changes symbol in conventional com…
klmcadams Aug 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 81 additions & 50 deletions doc-changelog/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ inputs:
default: true
type: boolean

use-labels:
description: >
Whether to use labels to categorize towncrier fragments or conventional
commits.
required: false
default: false
type: boolean

runs:
using: "composite"
steps:
Expand All @@ -91,7 +99,49 @@ runs:
run: |
python -m pip install --upgrade pip towncrier==${{ inputs.towncrier-version }}

- name: "Get first letter of conventional commit type"
if: ${{ inputs.use-labels == 'false' }}
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)
SMoraisAnsys marked this conversation as resolved.
Show resolved Hide resolved

- name: "Check pull-request title follows conventional commits style"
if: ${{ 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: ${{ env.FIRST_LETTER == 'uppercase' }}
uses: ansys/actions/commit-style@v6
with:
token: ${{ inputs.token }}
use-upper-case: true
klmcadams marked this conversation as resolved.
Show resolved Hide resolved

- name: "Get conventional commit type from title"
if: ${{ inputs.use-labels == 'false' }}
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-labels == 'true' }}
env:
OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
Expand All @@ -106,44 +156,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-labels == 'false' }}
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-labels == 'true' }}
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/')

klmcadams marked this conversation as resolved.
Show resolved Hide resolved
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 +200,19 @@ runs:
PR_TITLE: ${{ github.event.pull_request.title }}
shell: python
run: |
import os

# Retrieve title
clean_title = os.getenv('PR_TITLE')

# Remove extra whitespace
clean_title = clean_title.strip()
import sys
sys.path.insert(1, '${{ github.action_path }}/../doc-changelog/')

# Add backslash in front of backtick and double quote
clean_title = clean_title.replace("`", "\`").replace('"', '\\"')
from parse_pr_title import clean_pr_title

# Get the GITHUB_ENV variable
github_env = os.getenv('GITHUB_ENV')
pr_title = """${{ env.PR_TITLE }}"""
use_labels = "${{ inputs.use-labels }}"
use_labels = use_labels[0].upper() + use_labels[1:]

# Append the CLEAN_TITLE with its value to GITHUB_ENV
with open(github_env, "a") as f:
f.write(f"CLEAN_TITLE={clean_title}")
if use_labels == "True":
clean_pr_title(pr_title, use_labels)
else:
clean_pr_title(pr_title, use_labels)

- name: "Create and commit towncrier fragment"
env:
Expand All @@ -191,7 +222,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
118 changes: 118 additions & 0 deletions doc-changelog/parse_pr_title.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import os


def save_env_variable(env_var_name, env_var_value):
# Get the GITHUB_ENV variable
github_env = os.getenv("GITHUB_ENV")

# Save environment variable with its value
with open(github_env, "a") as f:
f.write(f"{env_var_name}={env_var_value}")


def get_first_letter_case(pr_title):
pr_title = f"""{pr_title}"""
index = 0
first_letter = pr_title[index]

print(pr_title)

while first_letter == " ":
index += 1
try:
first_letter = pr_title[index]
except IndexError:
print("Pull request title is blank")
exit(1)

if first_letter.islower():
save_env_variable("FIRST_LETTER", "lowercase")
else:
save_env_variable("FIRST_LETTER", "uppercase")


def get_conventional_commit_type(pr_title):
pr_title = f"""{pr_title}"""
colon_index = pr_title.index(":")
cc_type = '"' + pr_title[:colon_index] + '"'
save_env_variable("CC_TYPE", cc_type)


def changelog_category_cc(cc_type):
# Get conventional commit type from env variable
cc_type = cc_type.lower()

print(cc_type)

cc_type_changelog_dict = {
"feat": "added",
"fix": "fixed",
"docs": "documentation",
"build": "dependencies",
"revert": "miscellaneous",
"style": "miscellaneous",
"refactor": "miscellaneous",
"perf": "miscellaneous",
"test": "test",
"chore": "maintenance",
"ci": "maintenance",
}

changelog_section = cc_type_changelog_dict[cc_type]

save_env_variable("CHANGELOG_SECTION", changelog_section)


def changelog_cateogry_labels(labels):
# Create a list of labels found in the pull request
# For example, "enhancement maintenance".split() -> ["enhancement", "maintenance"]
existing_labels = 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",
"documentation": "documentation",
"testing": "test",
"dependencies": "dependencies",
"CI/CD": "maintenance",
"maintenance": "maintenance",
}

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

save_env_variable(
"CHANGELOG_SECTION", get_changelog_section(pr_labels, existing_labels)
)


def clean_pr_title(pr_title, use_labels):
# Retrieve title
clean_title = pr_title

# If not using label, remove conventional commit type from title
if use_labels == "False":
colon_index = clean_title.index(":")
clean_title = clean_title[colon_index + 1 :]

print(clean_title)

# Remove extra whitespace
clean_title = clean_title.strip()

# Add backslash in front of backtick and double quote
clean_title = clean_title.replace("`", "\\`").replace('"', '\\"')

save_env_variable("CLEAN_TITLE", clean_title)
1 change: 1 addition & 0 deletions doc/source/doc-actions/examples/doc-changelog-basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ changelog-fragment:
- uses: ansys/actions/doc-changelog@{{ version }}
with:
token: ${{ '{{ secrets.PYANSYS_CI_BOT_TOKEN }}' }}
# use-labels: true # uncomment this line to use labels instead of conventional commits
klmcadams marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 14 additions & 4 deletions doc/source/migrations/docs-changelog-setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,13 @@ Also, replace ``ansys.<product>.<library>`` with the name under ``tool.flit.modu
showcontent = true

[[tool.towncrier.type]]
directory = "changed"
name = "Changed"
directory = "dependencies"
name = "Dependencies"
showcontent = true

[[tool.towncrier.type]]
directory = "documentation"
name = "Documentation"
showcontent = true

[[tool.towncrier.type]]
Expand All @@ -257,15 +262,20 @@ Also, replace ``ansys.<product>.<library>`` with the name under ``tool.flit.modu
showcontent = true

[[tool.towncrier.type]]
directory = "dependencies"
name = "Dependencies"
directory = "maintenance"
name = "Maintenance"
showcontent = true

[[tool.towncrier.type]]
directory = "miscellaneous"
name = "Miscellaneous"
showcontent = true

[[tool.towncrier.type]]
directory = "test"
name = "Test"
showcontent = true

klmcadams marked this conversation as resolved.
Show resolved Hide resolved
A reference pull request for these changes can be found in the `PyAnsys Geometry #1023 <https://github.com/ansys/pyansys-geometry/pull/1023/files>`_ pull request.


Expand Down
Loading