From 9d4a8e033a8d955713d1b0fd5342db4a202c234e Mon Sep 17 00:00:00 2001 From: Lakshmi Pavananjali <154777864+Anjaliavv51@users.noreply.github.com> Date: Fri, 20 Dec 2024 21:32:12 +0530 Subject: [PATCH] Update close-old-issue.yml --- .github/workflows/close-old-issue.yml | 86 +++++++++++++-------------- 1 file changed, 40 insertions(+), 46 deletions(-) diff --git a/.github/workflows/close-old-issue.yml b/.github/workflows/close-old-issue.yml index d3e0e348..9cfb6ef9 100644 --- a/.github/workflows/close-old-issue.yml +++ b/.github/workflows/close-old-issue.yml @@ -1,55 +1,49 @@ -name: Close Old Issues +name: Close All Issues + on: - schedule: - - cron: "0 0 * * *" # Runs daily at midnight + workflow_dispatch: # Allows the workflow to be triggered manually + +permissions: + issues: write # Explicitly grant write permission to issues jobs: - manage-issues: + close_issues: runs-on: ubuntu-latest steps: - - name: Checkout Repository - uses: actions/checkout@v4 + - name: Debug Context + uses: actions/github-script@v6 + with: + script: | + console.log(`Repository: ${context.repo.owner}/${context.repo.repo}`); + console.log(`Workflow triggered by: ${context.actor}`); - - name: Manage Issues - run: | - open_issues=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ - "https://api.github.com/repos/${{ github.repository }}/issues?state=open" \ - | jq -r '.[] | .number') - for issue in $open_issues; do - # Get issue details - issue_details=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ - "https://api.github.com/repos/${{ github.repository }}/issues/$issue") - last_updated=$(echo $issue_details | jq -r '.updated_at') - labels=$(echo $issue_details | jq -r '.labels[].name') + - name: Fetch and close all open issues + uses: actions/github-script@v6 + with: + script: | + const issues = await github.paginate(github.rest.issues.listForRepo, { + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open' // Fetch only open issues + }); - days_since_update=$(( ( $(date +%s) - $(date -d "$last_updated" +%s) ) / 86400 )) + if (issues.length === 0) { + console.log("No open issues to close."); + } else { + console.log(`Found ${issues.length} open issues.`); + } - if [ $days_since_update -gt 7 ]; then - if echo "$labels" | grep -q "stale"; then - # If stale for more than 10 days, close the issue - if [ $days_since_update -gt 17 ]; then - curl -s -X PATCH -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ - -H "Accept: application/vnd.github.v3+json" \ - -d '{"state":"closed"}' \ - "https://api.github.com/repos/${{ github.repository }}/issues/$issue" - - curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ - -H "Accept: application/vnd.github.v3+json" \ - -d '{"body":"This issue has been automatically closed because it has been inactive for more than 17 days (including 7 days marked as stale). If you believe this is still relevant, feel free to reopen it or create a new one. Thank you!"}' \ - "https://api.github.com/repos/${{ github.repository }}/issues/$issue/comments" - fi - else - # Mark as stale - curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ - -H "Accept: application/vnd.github.v3+json" \ - -d '{"labels":["stale"]}' \ - "https://api.github.com/repos/${{ github.repository }}/issues/$issue/labels" - - curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ - -H "Accept: application/vnd.github.v3+json" \ - -d '{"body":"This issue has been marked as stale because it has been inactive for more than 7 days. It will be closed if no further activity occurs in the next 10 days. Please update if you want to keep it open."}' \ - "https://api.github.com/repos/${{ github.repository }}/issues/$issue/comments" - fi - fi - done + for (const issue of issues) { + if (!issue.pull_request) { // Ensure it’s an issue, not a pull request + await github.rest.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue.number, + state: 'closed' + }); + console.log(`Closed issue #${issue.number}: ${issue.title}`); + } else { + console.log(`Skipped pull request #${issue.number}`); + } + }