name: Close Milestone Issues on: workflow_call: inputs: repo: required: true type: string milestone: required: true type: string jobs: close_issues: runs-on: ubuntu-latest steps: - name: List and close issues run: | GITEA_API_URL="https://gitea.greyhound-software.com/api/v1" OWNER="greyhound" API_TOKEN=${{ vars.PAT_TOKEN }} NEW_LABEL_ID="32" FILTER_LABEL_ID="37" # Fetch milestones from the repository milestones=$(curl -s -H "Authorization: token $API_TOKEN" \ "$GITEA_API_URL/repos/$OWNER/${{ inputs.repo }}/milestones") # Extract the ID of the ${{ inputs.milestone }} milestone update_milestone_id=$(echo "$milestones" | jq '.[] | select(.title == "${{ inputs.milestone }}") | .id') if [ -z "$update_milestone_id" ]; then echo "No milestone named '${{ inputs.milestone }}' found. Exiting." exit 1 fi # Reading the Issues with the given Milestone echo "Milestone Id '$update_milestone_id'" echo "Reading issues with Url '$GITEA_API_URL/repos/$OWNER/${{ inputs.repo }}/issues?state=open&milestone=$update_milestone_id'" issues=$(curl -s -H "Authorization: token $API_TOKEN" \ "$GITEA_API_URL/repos/$OWNER/${{ inputs.repo }}/issues?state=open&milestones=$update_milestone_id") for issue_number in $(echo "$issues" | jq -r '.[].number'); do echo "Processing issue #$issue_number" # Debug: Output the full response for the current issue issue_details=$(curl -s -H "Authorization: token $API_TOKEN" \ "$GITEA_API_URL/repos/$OWNER/${{ inputs.repo }}/issues/$issue_number") echo "Issue details: $issue_details" # Get the current labels for the issue (now correctly parsing the JSON structure) current_labels=$(echo "$issue_details" | jq '[.labels[].id]') # Check if the issue has the specific label you're filtering by has_filter_label=$(echo "$current_labels" | jq --argjson filter_label $FILTER_LABEL_ID 'index($filter_label) != null') if [ "$has_filter_label" = "true" ]; then echo "Issue #$issue_number has the label $FILTER_LABEL_ID and milestone ${{ inputs.milestone }}" # Debug: Output current labels echo "Current labels for issue #$issue_number: $current_labels" # Update the issue updateResponse=$(curl -s -X PATCH -H "Authorization: token $API_TOKEN" \ -H "Content-Type: application/json" \ -d "{\"state\": \"closed\"}" \ "$GITEA_API_URL/repos/$OWNER/${{ inputs.repo }}/issues/$issue_number") echo "ISSUE update response: $updateResponse" # Update the issue labels labelResponse=$(curl -s -X POST -H "Authorization: token $API_TOKEN" \ -H "Content-Type: application/json" \ -d "{\"labels\": [$NEW_LABEL_ID]}" \ "$GITEA_API_URL/repos/$OWNER/${{ inputs.repo }}/issues/$issue_number/labels") echo "ISSUE Label update response code: $labelResponse" else echo "Issue #$issue_number does not have the label $FILTER_LABEL_ID, skipping..." fi done