pr-age #46
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: pr-age | |
on: | |
schedule: | |
- cron: '30 0,3,9,15 * * *' | |
pull_request: | |
types: [opened, synchronize, reopened] | |
# pull_request_review: | |
# types: [submitted] | |
workflow_dispatch: | |
permissions: | |
pull-requests: write | |
checks: write | |
contents: read | |
jobs: | |
check-pr-age: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check if 24 hours have passed since PR creation | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
// check 24-hours | |
async function checkPrAge(pr) { | |
const createdAt = new Date(pr.created_at); | |
const now = new Date(); | |
const diffHours = (now - createdAt) / (1000 * 60 * 60); | |
if (diffHours >= 24) { | |
// success | |
console.log(`PR #${pr.number} is older than 24 hours. Merge allowed.`); | |
await github.rest.checks.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
name: 'check-pr-age', | |
head_sha: pr.head.sha, | |
status: 'completed', | |
conclusion: 'success', | |
output: { | |
title: 'check-pr-age', | |
summary: `PR #${pr.number} is older than 24 hours.`, | |
}, | |
}); | |
} else { | |
// fail | |
const hoursLeft = (24 - diffHours).toFixed(2); | |
console.log(`PR #${pr.number} is not yet older than 24 hours. Left ${hoursLeft} hours.`); | |
await github.rest.checks.create({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
name: 'check-pr-age', | |
head_sha: pr.head.sha, | |
status: 'completed', | |
conclusion: 'failure', | |
output: { | |
title: 'check-pr-age', | |
summary: `PR #${pr.number} is not yet older than 24 hours. Please wait another ${hoursLeft} hours.`, | |
}, | |
}); | |
} | |
} | |
// know whether sceduled event | |
const isScheduled = !context.payload.pull_request; | |
if (isScheduled) { | |
const { data: pullRequests } = await github.rest.pulls.list({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
state: 'open', | |
per_page: 100, | |
}); | |
for (const pr of pullRequests) { | |
await checkPrAge(pr); | |
} | |
} else { | |
const pr = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: context.payload.pull_request.number, | |
}); | |
await checkPrAge(pr.data); | |
} |