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: changelog action & example #408

Merged
merged 30 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
147360e
add changelog action & example
klmcadams Feb 22, 2024
4abbef2
add more comments
klmcadams Feb 22, 2024
310a350
Merge branch 'main' into feat/changelog-action
RobPasMue Feb 23, 2024
c963e03
Merge branch 'main' into feat/changelog-action
klmcadams Feb 26, 2024
3a9d1ef
add permissions & comments
klmcadams Feb 26, 2024
0f80490
change contents to write
klmcadams Feb 26, 2024
82e1ec7
change bot commit message
klmcadams Feb 26, 2024
078154a
update migration steps doc
klmcadams Feb 26, 2024
d1614ca
update migration guide and add ci bot creds
klmcadams Feb 27, 2024
f64d453
change yml to yaml
klmcadams Feb 27, 2024
bd0820c
remove ci bot from action
klmcadams Feb 27, 2024
8a73116
add token to checkout step & update docs
klmcadams Feb 27, 2024
ae6a088
change username to pyansys-ci-bot
klmcadams Feb 27, 2024
101b7c9
Merge branch 'main' into feat/changelog-action
RobPasMue Feb 28, 2024
4ce4f73
Merge branch 'main' into feat/changelog-action
klmcadams Feb 28, 2024
26751e7
created separate doc for changelog setup instructions linked in migra…
klmcadams Feb 28, 2024
bdf9d61
Merge branch 'feat/changelog-action' of https://github.com/ansys/acti…
klmcadams Feb 28, 2024
6365f8e
fix vale issues
klmcadams Feb 28, 2024
7293c59
add license headers :p
klmcadams Feb 28, 2024
0ad3ac5
fix token & indents in doc changelog setup
klmcadams Feb 28, 2024
58b6cea
use pyansys-geometry as example & change branch ref to main
klmcadams Feb 28, 2024
911e151
fix wording
klmcadams Feb 28, 2024
6eb7561
fix url in towncrier line for changelog
klmcadams Feb 28, 2024
390dcca
remove emphasis from towncrier
klmcadams Feb 28, 2024
1f30568
docs: layout
RobPasMue Feb 29, 2024
158c7d8
apply Kathy's changes
klmcadams Mar 12, 2024
a3831ce
remove capital towncrier
klmcadams Mar 12, 2024
a66c1b4
Merge branch 'main' into feat/changelog-action
klmcadams Mar 12, 2024
6bacbd4
fix indent warning
klmcadams Mar 12, 2024
e9a569b
Merge branch 'feat/changelog-action' of https://github.com/ansys/acti…
klmcadams Mar 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
187 changes: 187 additions & 0 deletions doc-changelog/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# Copyright (C) 2022 - 2024 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

name: >
Documentation Changelog

description: |
Adds a newsfragment to the target repository using ``towncrier``
containing the title and number of the pull request.

This action will only work on Linux/macOS runners.

inputs:
# Required inputs

token:
description: >
Use the PYANSYS_CI_BOT_TOKEN to do a git commit & push.
The "contents: write" and "pull-requests: write" permissions
are required for this action.
required: true
type: string

# Optional inputs

python-version:
description: >
Python version used for setting up Python.
default: '3.10'
required: false
type: string

use-python-cache:
description: >
Whether to use the Python cache for installing previously downloaded
libraries. If ``true``, previously downloaded libraries are installed from the
Python cache. If ``false``, libraries are downloaded from the PyPI index.
required: false
default: true
type: boolean

runs:
using: "composite"
steps:
- name: "Install Git and clone project"
env:
PR_BRANCH: ${{ github.event.pull_request.head.ref }}
uses: actions/checkout@v4
with:
# Required to checkout branch from origin rather than remote
ref: ${{ env.PR_BRANCH }}
RobPasMue marked this conversation as resolved.
Show resolved Hide resolved
token: ${{ inputs.token }}

- name: "Set up Python ${{ inputs.python-version }}"
env:
PYTHON_VERSION: ${{ inputs.python-version }}
PYTHON_CACHE: ${{ inputs.use-python-cache }}
uses: ansys/actions/_setup-python@main
with:
python-version: ${{ env.PYTHON_VERSION }}
use-cache: ${{ env.PYTHON_CACHE }}

- name: "Install towncrier"
shell: bash
run: |
python -m pip install --upgrade pip towncrier

- name: "Get labels in the pull request"
env:
OWNER: ${{ github.repository_owner }}
REPO_NAME: ${{ github.event.repository.name }}
PR_NUMBER: ${{ github.event.number }}
GH_TOKEN: ${{ inputs.token }}
shell: bash
run: |
# Get the labels in the pull request
pr_labels=$(gh api repos/${{ env.OWNER }}/${{ env.REPO_NAME }}/pulls/${{ env.PR_NUMBER }} --jq '.labels.[].name')

# Save the labels to an environment variable
# For example, LABELS="enhancement maintenance"
echo LABELS='"'$pr_labels'"' >> $GITHUB_ENV

- name: "Set PR label environment variable"
shell: python
klmcadams marked this conversation as resolved.
Show resolved Hide resolved
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)}")

- name: "Remove PR fragment file if it already exists"
env:
PR_NUMBER: ${{ github.event.number }}
shell: bash
run: |
# Find files containing the PR number
# For example, 20.*.md
file=`find . -type f -name "${{ env.PR_NUMBER }}.*.md"`
klmcadams marked this conversation as resolved.
Show resolved Hide resolved

# If the fragment file exists, then delete the file
if [ ! -z "$file" ]; then
echo "Removing $file"
rm $file
fi

- name: "Create and commit towncrier fragment"
env:
PR_BRANCH: ${{ github.event.pull_request.head.ref }}
PR_TITLE: "${{ github.event.pull_request.title }}"
PR_NUMBER: ${{ github.event.number }}
shell: bash
run: |
# Changelog fragment file in the following format
# For example, 20.added.md
fragment="${{ env.PR_NUMBER }}.${{ env.PR_LABEL }}.md"

# Create changelog fragment with towncrier
# Fragment file contains the title of the PR
towncrier create -c "${{ env.PR_TITLE }}" $fragment

# Configure git username & email
git config user.name 'pyansys-ci-bot'
git config user.email 'pyansys.github.bot@ansys.com'

# Add towncrier fragment
git add .

# Check if file was modified
modified=`git diff HEAD --name-only`

# If the file was modified, commit & push it to the branch
if [ ! -z "$modified" ]; then
echo "modified: $modified"
# Commit and push fragment
git commit -m "Adding changelog entry: $fragment"
git push
fi
22 changes: 22 additions & 0 deletions doc/source/doc-actions/examples/doc-changelog-basic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Add these lines to .github/workflows/label.yml so the changelog fragment
# is updated when the PR is labeled & the title changes:
# on:
# pull_request:
# # opened, reopened, and synchronize are default for pull_request
# # edited - when PR title or body is changed
# # labeled - when labels are added to PR
# types: [opened, reopened, synchronize, edited, labeled]

# Put changelog-fragment action in .github/workflows/label.yml
# Don't forget to add pyansys-ci-bot as a member of your repository
changelog-fragment:
name: "Create changelog fragment"
needs: [labeler]
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: ansys/actions/doc-changelog@{{ version }}
with:
token: ${{ '{{ secrets.PYANSYS_CI_BOT_TOKEN }}' }}
8 changes: 4 additions & 4 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

.. note:: Adapting to a new version?

If you are adapting to a new version of the actions, please refer to the
:ref:`migration guide <migration_guide>`.
If you are adapting to a new version of the actions, see the
:ref:`migration_guide`.

For more information on available actions and how to use them,
see each card below:
see each of these cards:

.. grid:: 1 1 2 2
:gutter: 2
Expand Down Expand Up @@ -74,4 +74,4 @@ see each card below:
release-actions/index
housekeeping-actions/index
vulnerability-actions/index
migration_guide
migrations/index