From b663696bd4913b209339a4800b12cf4fd2cdc471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nuno=20G=C3=B3is?= Date: Tue, 22 Jul 2025 16:21:56 +0100 Subject: [PATCH] chore: AI flag cleanup reports errors back to issue (#10381) https://linear.app/unleash/issue/2-3706/ai-flag-cleanup-reports-errors-back-to-issue Reports AI flag cleanup errors back to the issue. --- .github/workflows/ai-flag-cleanup-pr.yml | 58 ++++++++++++++++++++---- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ai-flag-cleanup-pr.yml b/.github/workflows/ai-flag-cleanup-pr.yml index 1b43ce1150..e3e87647af 100644 --- a/.github/workflows/ai-flag-cleanup-pr.yml +++ b/.github/workflows/ai-flag-cleanup-pr.yml @@ -39,6 +39,10 @@ permissions: contents: write issues: write +concurrency: + group: AI-flag-cleanup-${{ inputs.issue-number }} + cancel-in-progress: true + jobs: create-pull-request: runs-on: ubuntu-latest @@ -88,7 +92,9 @@ jobs: if [[ "$TITLE" =~ Flag[[:space:]]([a-zA-Z0-9_-]+)[[:space:]]marked ]]; then echo "flag-name=${BASH_REMATCH[1]}" >> $GITHUB_OUTPUT else - echo "❌ Could not extract flag name from title: $TITLE" >&2 + MSG="Could not extract flag name from title: $TITLE" + echo "ERROR_MESSAGE=$MSG" >> $GITHUB_ENV + echo "❌ $MSG" >&2 exit 1 fi @@ -157,7 +163,9 @@ jobs: FLAG="${{ steps.extract_flag.outputs.flag-name }}" mapfile -d '' FILES < <(rg -0 -l "$FLAG" .) if [[ -z "$FILES" ]]; then - echo "❌ No files found for flag '$FLAG'" + MSG="No files found for flag '$FLAG'" + echo "ERROR_MESSAGE=$MSG" >> $GITHUB_ENV + echo "❌ $MSG" >&2 exit 1 fi printf '%s\0' "${FILES[@]}" > file_list.bin @@ -208,13 +216,13 @@ jobs: with: python-version: '3.12' - - name: Install build tools & Aider + - name: Install tools run: | python -m pip install --upgrade pip pip install aider-chat - - name: Run Aider CLI and capture summary - id: run_aider + - name: Clean up flag and capture summary + id: flag_cleanup timeout-minutes: ${{ inputs.chat-timeout }} env: GIT_AUTHOR_NAME: 'unleash-bot' @@ -223,6 +231,8 @@ jobs: GIT_COMMITTER_EMAIL: '194219037+unleash-bot[bot]@users.noreply.github.com' ${{ inputs.api_key_env_name }}: ${{ secrets.api_key_env_value }} run: | + set -euo pipefail + mapfile -d '' FILES < <(cat "${{ steps.find_files.outputs.file_list }}") aider --model "${{ inputs.model }}" \ --yes \ @@ -231,13 +241,18 @@ jobs: --no-attribute-committer \ --no-attribute-co-authored-by \ "${FILES[@]}" \ - | tee aider_output.txt + | tee flag_cleanup.txt || { + MSG="Flag cleanup failed" + echo "ERROR_MESSAGE=$MSG" >> $GITHUB_ENV + echo "❌ $MSG" >&2 + exit 1 + } SUMMARY=$(sed -n '/=== AI Flag Cleanup Summary Start ===/,/=== AI Flag Cleanup Summary End ===/{ /=== AI Flag Cleanup Summary Start ===/d /=== AI Flag Cleanup Summary End ===/d p - }' aider_output.txt) + }' flag_cleanup.txt) echo "summary<> $GITHUB_OUTPUT echo "$SUMMARY" >> $GITHUB_OUTPUT @@ -255,7 +270,7 @@ jobs: const { owner, repo } = context.repo; const branch = '${{ steps.create_branch.outputs.result }}'; const flag = '${{ steps.extract_flag.outputs.flag-name }}'; - const summary = ${{ toJson(steps.run_aider.outputs.summary) }}; + const summary = ${{ toJson(steps.flag_cleanup.outputs.summary) }}; const body = [ `This PR cleans up the ${flag} flag. These changes were automatically generated by AI and should be reviewed carefully.`, @@ -297,3 +312,30 @@ jobs: console.log(`Created PR #${pr.number}: ${pr.html_url}`); return pr; + + - name: Report failure to Issue + if: failure() + uses: actions/github-script@v7 + with: + github-token: ${{ steps.app_token.outputs.token }} + script: | + const runId = process.env.GITHUB_RUN_ID; + const serverUrl = process.env.GITHUB_SERVER_URL || 'https://github.com'; + const runUrl = `${serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}`; + + const errorMsg = process.env.ERROR_MESSAGE || "An unknown error occurred."; + + const body = [ + `⚠️ **AI Flag Cleanup** workflow [run #${runId}](${runUrl}) failed:`, + '', + `> ${errorMsg}`, + '', + 'Please check the logs for more details.' + ].join('\n'); + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: ${{ inputs.issue-number }}, + body + });