add PR requirements warning and enforcement workflow and remove the workflow dispatch trigger

This commit is contained in:
Akshaj Tiwari
2026-03-07 00:39:35 +05:30
parent df63c3e781
commit 37651e534f
2 changed files with 68 additions and 31 deletions
+13 -12
View File
@@ -5,7 +5,6 @@ on:
schedule:
- cron: "0 0 * * *" # runs every day once at midnight
workflow_dispatch:
jobs:
enforce:
name: Close PRs still failing contribution requirements
@@ -13,42 +12,44 @@ jobs:
permissions:
pull-requests: write
issues: write
steps:
- name: Close PRs still failing requirements
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const { data: prs } = await github.rest.pulls.list({
const prs = await github.paginate(github.rest.pulls.list, {
owner,
repo,
state: "open",
per_page: 100
});
for (const pr of prs) {
// Skip draft PRs — author may still be actively working toward compliance
if (pr.draft) continue;
const labels = pr.labels.map(l => l.name);
if (!labels.includes("pr-requirements-warning")) continue;
const gracePeriod = 24 * 60 * 60 * 1000;
const lastUpdated = new Date(pr.updated_at);
const now = new Date();
if (now - lastUpdated < gracePeriod) {
console.log(`Skipping PR #${pr.number} — still within grace period`);
continue;
}
const prNumber = pr.number;
const prAuthor = pr.user.login;
await github.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: `Closing PR because the contribution requirements were not resolved within the allowed time window.`
body: `Closing PR because the contribution requirements were not resolved within the 24-hour grace period.
If this was closed in error, feel free to reopen the PR after fixing the requirements.`
});
await github.rest.pulls.update({
owner,
repo,
pull_number: prNumber,
state: "closed"
});
console.log(`Closed PR #${prNumber} by ${prAuthor}`);
console.log(`Closed PR #${prNumber} by ${prAuthor} (PR requirements were not met)`);
}
+55 -19
View File
@@ -47,7 +47,7 @@ jobs:
const message = `## PR Requirements Warning
This PR does not meet the contribution requirements.
If the issue is not fixed within ~2 hours, it may be automatically closed.
If the issue is not fixed within ~24 hours, it may be automatically closed.
**Missing:** No linked issue found.
@@ -69,13 +69,14 @@ jobs:
**Why is this required?** See #472 for details.`;
const comments = await github.rest.issues.listComments({
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100,
});
const botComment = comments.data.find(
const botComment = comments.find(
(c) => c.user.type === 'Bot' && c.body.includes('PR Requirements Warning')
);
@@ -88,12 +89,29 @@ jobs:
});
}
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: ['pr-requirements-warning'],
});
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: ['pr-requirements-warning'],
});
} catch (error) {
if (error.status === 422) {
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: 'pr-requirements-warning',
color: 'e11d48',
});
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: ['pr-requirements-warning'],
});
}
}
core.setFailed('PR must reference an issue');
return;
@@ -137,7 +155,7 @@ jobs:
const message = `## PR Requirements Warning
This PR does not meet the contribution requirements.
If the issue is not fixed within ~2 hours, it may be automatically closed.
If the issue is not fixed within ~24 hours, it may be automatically closed.
**PR Author:** @${prAuthor}
**Found issues:** ${issueList}
@@ -160,13 +178,14 @@ jobs:
**Why is this required?** See #472 for details.`;
const comments = await github.rest.issues.listComments({
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100,
});
const botComment = comments.data.find(
const botComment = comments.find(
(c) => c.user.type === 'Bot' && c.body.includes('PR Requirements Warning')
);
@@ -179,12 +198,29 @@ jobs:
});
}
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: ['pr-requirements-warning'],
});
try {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: ['pr-requirements-warning'],
});
} catch (error) {
if (error.status === 422) {
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: 'pr-requirements-warning',
color: 'e11d48',
});
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
labels: ['pr-requirements-warning'],
});
}
}
core.setFailed('PR author must be assigned to the linked issue');
} else {
@@ -199,4 +235,4 @@ jobs:
}catch (error){
//ignore if label doesn't exist
}
}
}