Skip to content

PR Label Automation #251

PR Label Automation

PR Label Automation #251

Workflow file for this run

name: PR Label Automation
on:
schedule:
- cron: '0 10 * * *'
jobs:
update-labels:
runs-on: ubuntu-latest
steps:
- name: Check and Update PR Labels
uses: actions/github-script@v5
with:
script: |
const repo = context.repo;
// Fetch all open PRs
const prs = await github.rest.pulls.list({
owner: repo.owner,
repo: repo.repo,
state: 'open',
});
// Define the Discord webhook URL
const webhookUrl = 'https://discord.com/api/webhooks/1198592354127458326/mighz1RApMReApkYxMy0mJHI97du_nWnBRymuRLldMqgvRDNCme2RIW0fDtS3nEZAKip';
for (const pr of prs.data) {
const prNumber = pr.number;
let labels = pr.labels.map(label => label.name);
// Function to update label
async function updateLabel(oldLabel, newLabel) {
if (oldLabel) {
await github.rest.issues.removeLabel({
owner: repo.owner,
repo: repo.repo,
issue_number: prNumber,
name: oldLabel,
});
}
await github.rest.issues.addLabels({
owner: repo.owner,
repo: repo.repo,
issue_number: prNumber,
labels: [newLabel],
});
}
// Check and update 'D-x' labels
let dLabel = labels.find(label => label.startsWith("D-"));
if (dLabel) {
let day = parseInt(dLabel.split("-")[1]);
if (day > 0) {
const newDayLabel = `D-${day - 1}`;
await updateLabel(dLabel, newDayLabel);
console.log(`Updated label from ${dLabel} to ${newDayLabel} on PR #${prNumber}`);
await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: `Attention! PR #${prNumber} has ${day - 1} day(s) left before the deadline.`,
}),
});
} else if (day === 0) {
await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: `Urgent! PR #${prNumber} is due today!`,
}),
});
}
} else {
await updateLabel(null, 'D-5');
}
}