Update short-titles.yml #20
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Enforce Short Title | |
on: | |
issues: | |
types: [opened] | |
pull_request: | |
types: [opened] | |
jobs: | |
enforce_short_title: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
issues: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
with: | |
fetch-depth: 0 | |
ref: ${{ github.event.pull_request.head.ref }} | |
- name: Check Title Length | |
id: title_check | |
continue-on-error: true | |
run: | | |
if [ "$GITHUB_EVENT_NAME" == "issues" ]; then | |
title=$(jq -r '.issue.title' "$GITHUB_EVENT_PATH") | |
title_length=${#title} | |
if [ "$title_length" -ge 40 ]; then | |
echo "::error::Issue title length should be less than 40 characters." | |
exit 1 | |
fi | |
elif [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then | |
title=$(jq -r '.pull_request.title' "$GITHUB_EVENT_PATH") | |
title_length=${#title} | |
if [ "$title_length" -ge 40 ]; then | |
echo "::error::Pull request title length should be less than 40 characters." | |
exit 1 | |
fi | |
fi | |
- name: Truncate Title | |
if: ${{ steps.title_check.outcome == 'failure' }} | |
id: truncate_title | |
run: | | |
if [ "$GITHUB_EVENT_NAME" == "issues" ]; then | |
title=$(jq -r '.issue.title' "$GITHUB_EVENT_PATH") | |
elif [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then | |
title=$(jq -r '.pull_request.title' "$GITHUB_EVENT_PATH") | |
fi | |
# Check if the title exceeds 40 characters | |
title_length=${#title} | |
if [ "$title_length" -ge 40 ]; then | |
# Truncate the title at the last space within the first 40 characters | |
new_title=$(echo "$title" | awk '{ print substr($0, 1, 40) }') | |
new_title=$(echo "$new_title" | awk '{ sub(/\s+[^ ]*$/, ""); print }') | |
else | |
# Title doesn't need truncation | |
new_title="$title" | |
fi | |
echo "New title: $new_title" | |
echo "NEW_TITLE=$new_title" >> $GITHUB_ENV | |
- name: Update Issue/Pull Request Title | |
if: ${{ steps.title_check.outcome == 'failure' }} | |
run: | | |
new_title="${{ env.NEW_TITLE }}" | |
if [ "$GITHUB_EVENT_NAME" == "issues" ]; then | |
gh issue edit "$NUMBER" --title "$new_title" | |
elif [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then | |
gh pr edit "$NUMBER" --title "$new_title" | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
NUMBER: ${{ github.event.issue.number }} # For issues |