Dead Domains Check #14
This file contains 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: Dead Domains Check | |
env: | |
NODE_VERSION: 20 | |
on: | |
schedule: | |
# Run the job on every Monday at 8:00 AM UTC | |
- cron: '0 8 * * 1' | |
push: | |
branches: | |
- master | |
paths: | |
- '.github/workflows/dead-domains-check.yml' | |
workflow_dispatch: | |
jobs: | |
dead-domains-check: | |
name: Run Dead Domains | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check Out Repository | |
uses: actions/checkout@v4 | |
- name: Install pnpm | |
uses: pnpm/action-setup@v4 | |
with: | |
version: 9 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: ${{ env.NODE_VERSION }} | |
cache: pnpm | |
- name: Setup Perl and Tiny | |
uses: shogo82148/actions-setup-perl@v1 | |
with: | |
perl-version: '5.38' | |
install-modules: | | |
Path::Tiny | |
- name: Install Dependencies | |
run: pnpm install | |
- name: Export Dead Domains | |
run: pnpm dead-domains-linter --export dead-domains.txt | |
- name: Read Dead Domains | |
id: read-dead-domains | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const { readFile } = require('fs').promises; | |
const deadDomains = (await readFile('dead-domains.txt', 'utf8')) | |
.split(/\r?\n/) | |
.filter(Boolean); | |
// Add warning to the console | |
for (const domain of deadDomains) { | |
core.warning(`Possible Dead Domain: ${domain}`); | |
} | |
core.setOutput('has_dead_domains', deadDomains.length > 0 ? 'true' : 'false'); | |
core.setOutput('dead_domains', deadDomains); | |
- name: Close Pull Request(s) | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const { data: pullRequests } = await github.rest.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: 'open', | |
}); | |
const previousPullRequests = pullRequests.filter(pullRequest => { | |
const branchName = pullRequest.head.ref; | |
const isAutomated = branchName.startsWith('feature/dead-domains'); | |
const isBot = pullRequest.user.login === 'github-actions[bot]'; | |
return isAutomated && isBot; | |
}); | |
for (const previousPullRequest of previousPullRequests) { | |
await github.rest.pulls.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: previousPullRequest.number, | |
state: 'closed', | |
}); | |
core.info(`Closed Pull Request #${previousPullRequest.number}`); | |
} | |
for (const previousPullRequest of previousPullRequests) { | |
await github.rest.git.deleteRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: `heads/${previousPullRequest.head.ref}`, | |
}); | |
core.info(`Deleted Branch ${previousPullRequest.head.ref}`); | |
} | |
- name: Create New Branch | |
if: steps.read-dead-domains.outputs.has_dead_domains == 'true' | |
id: create-branch | |
run: | | |
branch_name="feature/dead-domains" | |
git checkout -b $branch_name | |
echo "branch_name=$branch_name" >> $GITHUB_OUTPUT | |
- name: Perform Changes | |
if: steps.read-dead-domains.outputs.has_dead_domains == 'true' | |
run: | | |
pnpm dead-domains-linter --auto --import dead-domains.txt | |
- name: Update Checksum and Date | |
if: steps.read-dead-domains.outputs.has_dead_domains == 'true' | |
run: | | |
for i in $(git status |grep modified |awk -F: '{print $NF}' |xargs); do | |
scripts/checksum-sort.sh $i | |
done | |
- name: Stage Changes | |
if: steps.read-dead-domains.outputs.has_dead_domains == 'true' | |
run: | | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
# Add "dead-domains.txt" to the .gitignore if it's not there | |
if ! grep -q "dead-domains.txt" .gitignore; then | |
echo "dead-domains.txt" >> .gitignore | |
fi | |
git add -A | |
- name: Commit and Push | |
if: steps.read-dead-domains.outputs.has_dead_domains == 'true' | |
run: | | |
git commit -m "Automated Dead Domains Check" | |
git push --set-upstream origin ${{ steps.create-branch.outputs.branch_name }} | |
- name: Create Pull Request | |
if: steps.read-dead-domains.outputs.has_dead_domains == 'true' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const { data: pullRequest } = await github.rest.pulls.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
title: 'Automated Dead Domains Check', | |
head: '${{ steps.create-branch.outputs.branch_name }}', | |
base: context.ref.replace('refs/heads/', ''), | |
body: [ | |
'This is an automated pull request.', | |
'', | |
'Please note that this is an automated check and some low-traffic websites may be incorrectly marked as dead.', | |
'', | |
'For more information, see https://github.com/AdguardTeam/DeadDomainsLinter/blob/master/README.md', | |
].join('\n'), | |
}); | |
core.info(`Pull Request #${pullRequest.number} Opened`); | |
// Add labels to the pull request | |
await github.rest.issues.addLabels({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: pullRequest.number, | |
labels: ['dead website'], | |
}); |