|
| 1 | +name: Update Broken Links |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [closed] |
| 6 | + |
| 7 | +jobs: |
| 8 | + update-broken-links: |
| 9 | + # فقط وقتی issue با label broken-link بسته میشه و با "won't fix" یا "fixed" نیست |
| 10 | + if: contains(github.event.issue.labels.*.name, 'broken-link') && github.event.issue.state_reason != 'not_planned' |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout |
| 17 | + uses: actions/checkout@v4 |
| 18 | + |
| 19 | + - name: Parse issue and update broken-links.json |
| 20 | + uses: actions/github-script@v7 |
| 21 | + with: |
| 22 | + script: | |
| 23 | + const fs = require('fs'); |
| 24 | + const path = 'data/broken-links.json'; |
| 25 | + const issue = context.payload.issue; |
| 26 | + const body = issue.body || ''; |
| 27 | +
|
| 28 | + // پارس بدنه issue |
| 29 | + function parseField(label) { |
| 30 | + const regex = new RegExp(`\\*\\*${label}:\\*\\*\\s*(.+)`); |
| 31 | + const match = body.match(regex); |
| 32 | + return match ? match[1].trim() : ''; |
| 33 | + } |
| 34 | +
|
| 35 | + const brokenUrl = parseField('لینک خراب'); |
| 36 | + const imdbId = parseField('IMDb'); |
| 37 | + const tmdbId = parseField('TMDb'); |
| 38 | + const server = parseField('سرور'); |
| 39 | +
|
| 40 | + if (!brokenUrl) { |
| 41 | + console.log('لینک خراب پیدا نشد، رد میشه'); |
| 42 | + return; |
| 43 | + } |
| 44 | +
|
| 45 | + // خواندن یا ساخت فایل |
| 46 | + let data = { updated_at: '', broken: [] }; |
| 47 | + if (fs.existsSync(path)) { |
| 48 | + data = JSON.parse(fs.readFileSync(path, 'utf8')); |
| 49 | + } |
| 50 | + if (!Array.isArray(data.broken)) data.broken = []; |
| 51 | +
|
| 52 | + // جلوگیری از تکرار |
| 53 | + const alreadyExists = data.broken.some(e => e.url === brokenUrl); |
| 54 | + if (!alreadyExists) { |
| 55 | + data.broken.push({ |
| 56 | + url: brokenUrl, |
| 57 | + imdbId, |
| 58 | + tmdbId, |
| 59 | + server, |
| 60 | + reportedAt: new Date().toISOString(), |
| 61 | + issueNumber: issue.number |
| 62 | + }); |
| 63 | + data.updated_at = new Date().toISOString(); |
| 64 | +
|
| 65 | + fs.mkdirSync('data', { recursive: true }); |
| 66 | + fs.writeFileSync(path, JSON.stringify(data, null, 2)); |
| 67 | + console.log(`لینک اضافه شد: ${brokenUrl}`); |
| 68 | + } else { |
| 69 | + console.log(`لینک قبلاً وجود داشت: ${brokenUrl}`); |
| 70 | + } |
| 71 | +
|
| 72 | + - name: Commit and push |
| 73 | + run: | |
| 74 | + git config user.name "github-actions[bot]" |
| 75 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 76 | + git add data/broken-links.json |
| 77 | + git diff --staged --quiet || git commit -m "chore: mark broken link from issue #${{ github.event.issue.number }}" |
| 78 | + git push |
0 commit comments