|  | 
|  | 1 | +name: Auto Close Issues | 
|  | 2 | + | 
|  | 3 | +on: | 
|  | 4 | +  schedule: | 
|  | 5 | +    - cron: '0 14 * * 1-5'  # 9 AM EST (2 PM UTC) Monday through Friday | 
|  | 6 | +  workflow_dispatch: | 
|  | 7 | +    inputs: | 
|  | 8 | +      dry_run: | 
|  | 9 | +        description: 'Run in dry-run mode (no actions taken, only logging)' | 
|  | 10 | +        required: false | 
|  | 11 | +        default: 'false' | 
|  | 12 | +        type: boolean | 
|  | 13 | + | 
|  | 14 | +permissions: | 
|  | 15 | +  contents: read | 
|  | 16 | +  issues: write | 
|  | 17 | +  pull-requests: write | 
|  | 18 | + | 
|  | 19 | +jobs: | 
|  | 20 | +  auto-close: | 
|  | 21 | +    runs-on: ubuntu-latest | 
|  | 22 | +    strategy: | 
|  | 23 | +      matrix: | 
|  | 24 | +        include: | 
|  | 25 | +          - label: 'autoclose in 3 days' | 
|  | 26 | +            days: 3 | 
|  | 27 | +            issue_types: 'issues' #issues/pulls/both | 
|  | 28 | +            replacement_label: '' | 
|  | 29 | +            closure_message: 'This issue has been automatically closed as it was marked for auto-closure by the team and no additional responses was received within 3 days.' | 
|  | 30 | +            dry_run: 'false' | 
|  | 31 | +          - label: 'autoclose in 7 days' | 
|  | 32 | +            days: 7 | 
|  | 33 | +            issue_types: 'issues'  # issues/pulls/both | 
|  | 34 | +            replacement_label: '' | 
|  | 35 | +            closure_message: 'This issue has been automatically closed as it was marked for auto-closure by the team and no additional responses was received within 7 days.' | 
|  | 36 | +            dry_run: 'false' | 
|  | 37 | +    steps: | 
|  | 38 | +      - name: Validate and process ${{ matrix.label }} | 
|  | 39 | +        uses: actions/github-script@v8 | 
|  | 40 | +        env: | 
|  | 41 | +          LABEL_NAME: ${{ matrix.label }} | 
|  | 42 | +          DAYS_TO_WAIT: ${{ matrix.days }} | 
|  | 43 | +          AUTHORIZED_USERS: '' | 
|  | 44 | +          AUTH_MODE: 'write-access' | 
|  | 45 | +          ISSUE_TYPES: ${{ matrix.issue_types }} | 
|  | 46 | +          DRY_RUN: ${{ matrix.dry_run }} | 
|  | 47 | +          REPLACEMENT_LABEL: ${{ matrix.replacement_label }} | 
|  | 48 | +          CLOSE_MESSAGE: ${{matrix.closure_message}} | 
|  | 49 | +        with: | 
|  | 50 | +          script: | | 
|  | 51 | +            const REQUIRED_PERMISSIONS = ['write', 'admin']; | 
|  | 52 | +            const CLOSE_MESSAGE = process.env.CLOSE_MESSAGE; | 
|  | 53 | +            const isDryRun = '${{ inputs.dry_run }}' === 'true' || process.env.DRY_RUN === 'true'; | 
|  | 54 | +             | 
|  | 55 | +            const config = { | 
|  | 56 | +              labelName: process.env.LABEL_NAME, | 
|  | 57 | +              daysToWait: parseInt(process.env.DAYS_TO_WAIT), | 
|  | 58 | +              authMode: process.env.AUTH_MODE, | 
|  | 59 | +              authorizedUsers: process.env.AUTHORIZED_USERS?.split(',').map(u => u.trim()).filter(u => u) || [], | 
|  | 60 | +              issueTypes: process.env.ISSUE_TYPES, | 
|  | 61 | +              replacementLabel: process.env.REPLACEMENT_LABEL?.trim() || null | 
|  | 62 | +            }; | 
|  | 63 | +             | 
|  | 64 | +            console.log(`🏷️ Processing label: "${config.labelName}" (${config.daysToWait} days)`); | 
|  | 65 | +            if (isDryRun) console.log('🧪 DRY-RUN MODE: No actions will be taken'); | 
|  | 66 | +             | 
|  | 67 | +            const cutoffDate = new Date(); | 
|  | 68 | +            cutoffDate.setDate(cutoffDate.getDate() - config.daysToWait); | 
|  | 69 | +             | 
|  | 70 | +            async function isAuthorizedUser(username) { | 
|  | 71 | +              try { | 
|  | 72 | +                if (config.authMode === 'users') { | 
|  | 73 | +                  return config.authorizedUsers.includes(username); | 
|  | 74 | +                } else if (config.authMode === 'write-access') { | 
|  | 75 | +                  const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ | 
|  | 76 | +                    owner: context.repo.owner, | 
|  | 77 | +                    repo: context.repo.repo, | 
|  | 78 | +                    username: username | 
|  | 79 | +                  }); | 
|  | 80 | +                  return REQUIRED_PERMISSIONS.includes(data.permission); | 
|  | 81 | +                } | 
|  | 82 | +              } catch (error) { | 
|  | 83 | +                console.log(`⚠️ Failed to check authorization for ${username}: ${error.message}`); | 
|  | 84 | +                return false; | 
|  | 85 | +              } | 
|  | 86 | +              return false; | 
|  | 87 | +            } | 
|  | 88 | +             | 
|  | 89 | +            let allIssues = []; | 
|  | 90 | +            let page = 1; | 
|  | 91 | +             | 
|  | 92 | +            while (true) { | 
|  | 93 | +              const { data: issues } = await github.rest.issues.listForRepo({ | 
|  | 94 | +                owner: context.repo.owner, | 
|  | 95 | +                repo: context.repo.repo, | 
|  | 96 | +                state: 'open', | 
|  | 97 | +                labels: config.labelName, | 
|  | 98 | +                sort: 'updated', | 
|  | 99 | +                direction: 'desc', | 
|  | 100 | +                per_page: 100, | 
|  | 101 | +                page: page | 
|  | 102 | +              }); | 
|  | 103 | +               | 
|  | 104 | +              if (issues.length === 0) break; | 
|  | 105 | +              allIssues = allIssues.concat(issues); | 
|  | 106 | +              if (issues.length < 100) break; | 
|  | 107 | +              page++; | 
|  | 108 | +            } | 
|  | 109 | +             | 
|  | 110 | +            const targetIssues = allIssues.filter(issue => { | 
|  | 111 | +              if (config.issueTypes === 'issues' && issue.pull_request) return false; | 
|  | 112 | +              if (config.issueTypes === 'pulls' && !issue.pull_request) return false; | 
|  | 113 | +              return true; | 
|  | 114 | +            }); | 
|  | 115 | +             | 
|  | 116 | +            console.log(`🔍 Found ${targetIssues.length} items with label "${config.labelName}"`); | 
|  | 117 | +             | 
|  | 118 | +            if (targetIssues.length === 0) { | 
|  | 119 | +              console.log('✅ No items to process'); | 
|  | 120 | +              return; | 
|  | 121 | +            } | 
|  | 122 | +             | 
|  | 123 | +            let closedCount = 0; | 
|  | 124 | +            let labelRemovedCount = 0; | 
|  | 125 | +            let skippedCount = 0; | 
|  | 126 | +             | 
|  | 127 | +            for (const issue of targetIssues) { | 
|  | 128 | +              console.log(`\n📋 Processing #${issue.number}: ${issue.title}`); | 
|  | 129 | +               | 
|  | 130 | +              try { | 
|  | 131 | +                const { data: events } = await github.rest.issues.listEvents({ | 
|  | 132 | +                  owner: context.repo.owner, | 
|  | 133 | +                  repo: context.repo.repo, | 
|  | 134 | +                  issue_number: issue.number | 
|  | 135 | +                }); | 
|  | 136 | +                 | 
|  | 137 | +                const labelEvents = events | 
|  | 138 | +                  .filter(e => e.event === 'labeled' && e.label?.name === config.labelName) | 
|  | 139 | +                  .sort((a, b) => new Date(b.created_at) - new Date(a.created_at)); | 
|  | 140 | +                 | 
|  | 141 | +                if (labelEvents.length === 0) { | 
|  | 142 | +                  console.log(`⚠️ No label events found for #${issue.number}`); | 
|  | 143 | +                  skippedCount++; | 
|  | 144 | +                  continue; | 
|  | 145 | +                } | 
|  | 146 | +                 | 
|  | 147 | +                const lastLabelAdded = new Date(labelEvents[0].created_at); | 
|  | 148 | +                const labelAdder = labelEvents[0].actor.login; | 
|  | 149 | +                 | 
|  | 150 | +                const { data: comments } = await github.rest.issues.listComments({ | 
|  | 151 | +                  owner: context.repo.owner, | 
|  | 152 | +                  repo: context.repo.repo, | 
|  | 153 | +                  issue_number: issue.number, | 
|  | 154 | +                  since: lastLabelAdded.toISOString() | 
|  | 155 | +                }); | 
|  | 156 | +                 | 
|  | 157 | +                let hasUnauthorizedComment = false; | 
|  | 158 | +                 | 
|  | 159 | +                for (const comment of comments) { | 
|  | 160 | +                  if (comment.user.login === labelAdder) continue; | 
|  | 161 | +                   | 
|  | 162 | +                  const isAuthorized = await isAuthorizedUser(comment.user.login); | 
|  | 163 | +                  if (!isAuthorized) { | 
|  | 164 | +                    console.log(`❌ New comment from ${comment.user.login}`); | 
|  | 165 | +                    hasUnauthorizedComment = true; | 
|  | 166 | +                    break; | 
|  | 167 | +                  } | 
|  | 168 | +                } | 
|  | 169 | +                 | 
|  | 170 | +                if (hasUnauthorizedComment) { | 
|  | 171 | +                  if (isDryRun) { | 
|  | 172 | +                    console.log(`🧪 DRY-RUN: Would remove ${config.labelName} label from #${issue.number}`); | 
|  | 173 | +                    if (config.replacementLabel) { | 
|  | 174 | +                      console.log(`🧪 DRY-RUN: Would add ${config.replacementLabel} label to #${issue.number}`); | 
|  | 175 | +                    } | 
|  | 176 | +                  } else { | 
|  | 177 | +                    await github.rest.issues.removeLabel({ | 
|  | 178 | +                      owner: context.repo.owner, | 
|  | 179 | +                      repo: context.repo.repo, | 
|  | 180 | +                      issue_number: issue.number, | 
|  | 181 | +                      name: config.labelName | 
|  | 182 | +                    }); | 
|  | 183 | +                    console.log(`🏷️ Removed ${config.labelName} label from #${issue.number}`); | 
|  | 184 | +                     | 
|  | 185 | +                    if (config.replacementLabel) { | 
|  | 186 | +                      await github.rest.issues.addLabels({ | 
|  | 187 | +                        owner: context.repo.owner, | 
|  | 188 | +                        repo: context.repo.repo, | 
|  | 189 | +                        issue_number: issue.number, | 
|  | 190 | +                        labels: [config.replacementLabel] | 
|  | 191 | +                      }); | 
|  | 192 | +                      console.log(`🏷️ Added ${config.replacementLabel} label to #${issue.number}`); | 
|  | 193 | +                    } | 
|  | 194 | +                  } | 
|  | 195 | +                  labelRemovedCount++; | 
|  | 196 | +                  continue; | 
|  | 197 | +                } | 
|  | 198 | +                 | 
|  | 199 | +                if (lastLabelAdded > cutoffDate) { | 
|  | 200 | +                  const daysRemaining = Math.ceil((lastLabelAdded - cutoffDate) / (1000 * 60 * 60 * 24)); | 
|  | 201 | +                  console.log(`⏳ Label added too recently (${daysRemaining} days remaining)`); | 
|  | 202 | +                  skippedCount++; | 
|  | 203 | +                  continue; | 
|  | 204 | +                } | 
|  | 205 | +                 | 
|  | 206 | +                if (isDryRun) { | 
|  | 207 | +                  console.log(`🧪 DRY-RUN: Would close #${issue.number} with comment`); | 
|  | 208 | +                } else { | 
|  | 209 | +                  await github.rest.issues.createComment({ | 
|  | 210 | +                    owner: context.repo.owner, | 
|  | 211 | +                    repo: context.repo.repo, | 
|  | 212 | +                    issue_number: issue.number, | 
|  | 213 | +                    body: CLOSE_MESSAGE | 
|  | 214 | +                  }); | 
|  | 215 | +                   | 
|  | 216 | +                  await github.rest.issues.update({ | 
|  | 217 | +                    owner: context.repo.owner, | 
|  | 218 | +                    repo: context.repo.repo, | 
|  | 219 | +                    issue_number: issue.number, | 
|  | 220 | +                    state: 'closed' | 
|  | 221 | +                  }); | 
|  | 222 | +                   | 
|  | 223 | +                  console.log(`🔒 Closed #${issue.number}`); | 
|  | 224 | +                } | 
|  | 225 | +                closedCount++; | 
|  | 226 | +              } catch (error) { | 
|  | 227 | +                console.log(`❌ Error processing #${issue.number}: ${error.message}`); | 
|  | 228 | +                skippedCount++; | 
|  | 229 | +              } | 
|  | 230 | +            } | 
|  | 231 | +             | 
|  | 232 | +            console.log(`\n📊 Summary for "${config.labelName}":`); | 
|  | 233 | +            if (isDryRun) { | 
|  | 234 | +              console.log(`   🧪 DRY-RUN MODE - No actual changes made:`); | 
|  | 235 | +              console.log(`   • Issues that would be closed: ${closedCount}`); | 
|  | 236 | +              console.log(`   • Labels that would be removed: ${labelRemovedCount}`); | 
|  | 237 | +            } else { | 
|  | 238 | +              console.log(`   • Issues closed: ${closedCount}`); | 
|  | 239 | +              console.log(`   • Labels removed: ${labelRemovedCount}`); | 
|  | 240 | +            } | 
|  | 241 | +            console.log(`   • Issues skipped: ${skippedCount}`); | 
|  | 242 | +            console.log(`   • Total processed: ${targetIssues.length}`); | 
0 commit comments