Skip to content

Commit

Permalink
Merge pull request #45 from octoposprime/fix/sdd/20/short-title-workflow
Browse files Browse the repository at this point in the history
Update short-titles.yml
  • Loading branch information
Feyzanrs authored May 6, 2024
2 parents 7c7cedb + 64506cb commit e2fcc13
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions .github/workflows/short-titles.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
name: Enforce Short Title

on:
issues:
types:
- opened
types: [opened]
pull_request:
types:
- opened
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")
Expand All @@ -31,22 +39,38 @@ jobs:
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: failure()
if: ${{ steps.title_check.outcome == 'failure' }}
run: |
new_title="${{ env.NEW_TITLE }}"
if [ "$GITHUB_EVENT_NAME" == "issues" ]; then
new_title=$(echo "$title" | cut -c -39)
echo "::set-output name=new_title::$new_title"
gh issue edit "$NUMBER" --title "$new_title"
elif [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
new_title=$(echo "$title" | cut -c -39)
echo "::set-output name=new_title::$new_title"
gh pr edit "$NUMBER" --title "$new_title"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUMBER: ${{ github.event.issue.number }} # For issues
LABELS: ${{ secrets.REPO_TEAM }}
GH_REPO: ${{ github.repository }}
PROJECT: ${{ secrets.ASSIGN_PROJECT }}
# NUMBER: ${{ github.event.pull_request.number }} # For pull requests

0 comments on commit e2fcc13

Please sign in to comment.