fix: use timestamp throttle for GitHub fetch instead of block height … #104
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Version Check Bot | ||
| on: | ||
| issues: | ||
| types: [opened, edited] | ||
| permissions: | ||
| issues: write | ||
| jobs: | ||
| check-version: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Check version in issue title | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const issue = context.payload.issue; | ||
| const title = issue.title; | ||
| // Regex to detect version pattern: optional v followed by numbers and dots | ||
| // Examples: v0.1.0, 0.0.4-alpha, v1.2.3-beta, [v0.1.0], [0.0.4-alpha], etc. | ||
| const versionRegex = /\[?v?\d+\.\d+(\.\d+)?(-[\w.]+)?\]?/i; | ||
| const hasVersion = versionRegex.test(title); | ||
| // Extract detected version for logging | ||
| const versionMatch = title.match(versionRegex); | ||
| const detectedVersion = versionMatch ? versionMatch[0] : null; | ||
| if (hasVersion) { | ||
| console.log(`Version detected in title: ${detectedVersion}`); | ||
| // Add a label to indicate version was detected | ||
| try { | ||
| await github.rest.issues.addLabels({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: issue.number, | ||
| labels: ['version-specified'] | ||
| }); | ||
| } catch (e) { | ||
| console.log('Could not add label (may not exist): ' + e.message); | ||
| } | ||
| // Remove warning label if it exists | ||
| try { | ||
| await github.rest.issues.removeLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: issue.number, | ||
| name: 'missing-version' | ||
| }); | ||
| } catch (e) { | ||
| // Label may not exist, that's fine | ||
| } | ||
| return; | ||
| } | ||
| // No version detected - warn the user | ||
| console.log('No version detected in title'); | ||
| const warningMessage = ` | ||
| ## ⚠️ Version Required | ||
| Hello @${issue.user.login}, | ||
| Thank you for your report! However, **your issue title is missing a version number**. | ||
| Please update your issue title to include the affected/target version in the format: | ||
| - \`[BUG] [v0.1.5] Your issue description\` | ||
| - \`[FEATURE] [v0.2.0] Your feature request\` | ||
| - \`[PERF] [v0.1.5] Your performance issue\` | ||
| **Examples of valid titles:** | ||
| - \`[BUG] [v0.1.5] CLI crashes on Windows\` | ||
| - \`[FEATURE] [v0.2.0] Add export functionality\` | ||
| Please edit your issue title to add the version. **Issues without a version will be automatically closed in 7 days.** | ||
| --- | ||
| *This is an automated message. You can find your version by running \`[app cli --version]\`.* | ||
| `; | ||
| // Post warning comment | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: issue.number, | ||
| body: warningMessage | ||
| }); | ||
| // Add warning label | ||
| try { | ||
| await github.rest.issues.addLabels({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: issue.number, | ||
| labels: ['missing-version'] | ||
| }); | ||
| } catch (e) { | ||
| console.log('Could not add label: ' + e.message); | ||
| } | ||
| close-stale-issues: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Close issues without version after grace period | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const sevenDaysAgo = new Date(); | ||
| sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7); | ||
| // Find issues with missing-version label | ||
| const { data: issues } = await github.rest.issues.listForRepo({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| labels: 'missing-version', | ||
| state: 'open' | ||
| }); | ||
| const versionRegex = /\[?v?\d+\.\d+(\.\d+)?(-[\w.]+)?\]?/i; | ||
| for (const issue of issues) { | ||
| const createdAt = new Date(issue.created_at); | ||
| // Skip if issue was created less than 7 days ago | ||
| if (createdAt > sevenDaysAgo) { | ||
| console.log(`Issue #${issue.number} is too recent, skipping`); | ||
| continue; | ||
| } | ||
| // Double-check title still doesn't have version | ||
| if (versionRegex.test(issue.title)) { | ||
| console.log(`Issue #${issue.number} now has version, removing label`); | ||
| try { | ||
| await github.rest.issues.removeLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: issue.number, | ||
| name: 'missing-version' | ||
| }); | ||
| } catch (e) {} | ||
| continue; | ||
| } | ||
| // Close the issue | ||
| console.log(`Closing issue #${issue.number} - no version after 7 days`); | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: issue.number, | ||
| body: `## Issue Closed | ||
| This issue has been automatically closed because no version was specified in the title after 7 days. | ||
| If you still want to report this issue, please create a new issue with the version included in the title (e.g., \`[BUG] [v0.1.5] Your description\`). | ||
| Thank you for your understanding.` | ||
| }); | ||
| await github.rest.issues.update({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: issue.number, | ||
| state: 'closed', | ||
| state_reason: 'not_planned' | ||
| }); | ||
| } | ||