From 64506cb046826b327d9c6106f8656c7f4f7fab77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCmeyye=20Dilara=20DO=C4=9EAN?= <122971619+Sddilora@users.noreply.github.com> Date: Tue, 30 Apr 2024 17:50:39 +0300 Subject: [PATCH] Update short-titles.yml Logic fixed. Title char fixed < 40 char without trimming last word --- .github/workflows/short-titles.yml | 50 ++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/.github/workflows/short-titles.yml b/.github/workflows/short-titles.yml index f3796f7..70f85be 100644 --- a/.github/workflows/short-titles.yml +++ b/.github/workflows/short-titles.yml @@ -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") @@ -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