|
| 1 | +name: Cron – Check Broken Markdown Links |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 0 1 * *' |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + dry_run: |
| 9 | + description: 'Run without creating issues? (true/false)' |
| 10 | + required: true |
| 11 | + default: true |
| 12 | + type: boolean |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + issues: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + cron-check-broken-links: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Harden runner (audit outbound calls) |
| 23 | + uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0 |
| 24 | + with: |
| 25 | + egress-policy: audit |
| 26 | + |
| 27 | + - name: Checkout repository |
| 28 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 29 | + |
| 30 | + - name: Check Markdown links (Lychee) |
| 31 | + id: lychee |
| 32 | + uses: lycheeverse/lychee-action@a8c4c7cb88f0c7386610c35eb25108e448569cb0 # v2.7.0 |
| 33 | + continue-on-error: true |
| 34 | + with: |
| 35 | + args: --verbose --no-progress './**/*.md' |
| 36 | + fail: true |
| 37 | + env: |
| 38 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 39 | + |
| 40 | + - name: Report Broken Links (Idempotent Issue Management) |
| 41 | + if: steps.lychee.outcome == 'failure' |
| 42 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 43 | + with: |
| 44 | + script: | |
| 45 | + // Determine if this is a dry run |
| 46 | + const isManual = context.eventName === 'workflow_dispatch'; |
| 47 | + const dryRun = isManual ? context.payload.inputs.dry_run === true : false; |
| 48 | +
|
| 49 | + // Labels configuration |
| 50 | + const targetLabels = ['broken-markdown-links', 'automated']; |
| 51 | + const issueTitle = "Scheduled Markdown Link Check Found Broken Links"; |
| 52 | + const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 53 | +
|
| 54 | + console.log(`Event: ${context.eventName}, Dry Run: ${dryRun}`); |
| 55 | +
|
| 56 | + // Define Issue Body |
| 57 | + const body = `### 🔗 Broken Links Detected\n\n` + |
| 58 | + `The scheduled markdown link check workflow has detected broken links.\n\n` + |
| 59 | + `**Run Details:**\n` + |
| 60 | + `- **Timestamp:** ${new Date().toISOString()}\n` + |
| 61 | + `- **Workflow Run:** [View Logs](${runUrl})\n\n` + |
| 62 | + `> **Note:** We use [Lychee](https://github.com/lycheeverse/lychee) for link checking. ` + |
| 63 | + `Please check the "Check Markdown links" step in the logs to see the specific URLs that failed.`; |
| 64 | +
|
| 65 | + if (dryRun) { |
| 66 | + console.log("DRY RUN: Would have created or updated an issue."); |
| 67 | + return; |
| 68 | + } |
| 69 | +
|
| 70 | + // Search for existing issue and Update/Create |
| 71 | + try { |
| 72 | + const { data: issues } = await github.rest.issues.listForRepo({ |
| 73 | + owner: context.repo.owner, |
| 74 | + repo: context.repo.repo, |
| 75 | + state: 'open', |
| 76 | + labels: targetLabels.join(','), |
| 77 | + per_page: 100 |
| 78 | + }); |
| 79 | +
|
| 80 | + const existingIssue = issues.find(issue => issue.title === issueTitle); |
| 81 | +
|
| 82 | + if (existingIssue) { |
| 83 | + console.log(`Updating existing issue #${existingIssue.number}`); |
| 84 | + await github.rest.issues.createComment({ |
| 85 | + owner: context.repo.owner, |
| 86 | + repo: context.repo.repo, |
| 87 | + issue_number: existingIssue.number, |
| 88 | + body: `**Update ${new Date().toISOString()}:** Still finding broken links.\nCheck new run logs: ${runUrl}` |
| 89 | + }); |
| 90 | + } else { |
| 91 | + console.log("Creating a new issue..."); |
| 92 | + await github.rest.issues.create({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + title: issueTitle, |
| 96 | + body: body, |
| 97 | + labels: targetLabels |
| 98 | + }); |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + console.error('Failed to manage broken link issue:', error); |
| 102 | + core.setFailed(`Failed to create or update issue: ${error.message}`); |
| 103 | + } |
0 commit comments