Skip to content

PR Issue Labeler

PR Issue Labeler #19

Workflow file for this run

name: PR Label Automation
on:
pull_request:
types: [opened, reopened, edited]
schedule:
- cron: '*/5 * * * *' # Run every 5 minutes
jobs:
pr-label-automation:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install @octokit/rest
- name: Run PR label automation script
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
const { Octokit } = require("@octokit/rest");
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
async function processPR(owner, repo, pull_number) {
const { data: pr } = await octokit.pulls.get({
owner,
repo,
pull_number,
});
const issueRegex = /#(\d+)/;
const match = pr.body.match(issueRegex);
if (match) {
const issueNumber = parseInt(match[1]);
const { data: issue } = await octokit.issues.get({
owner,
repo,
issue_number: issueNumber,
});
if (issue.labels.length > 0) {
await octokit.issues.addLabels({
owner,
repo,
issue_number: pull_number,
labels: issue.labels.map(label => label.name),
});
console.log(`Added labels to PR #${pull_number}`);
} else {
console.log(`Issue #${issueNumber} has no labels`);
}
} else {
await octokit.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: "Please include the related issue number in the PR description.",
});
console.log(`Commented on PR #${pull_number} to request issue number`);
}
}
async function run() {
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
if (process.env.GITHUB_EVENT_NAME === "pull_request") {
const pull_number = process.env.GITHUB_EVENT.pull_request.number;
await processPR(owner, repo, pull_number);
} else {
const { data: prs } = await octokit.pulls.list({
owner,
repo,
state: "open",
});
for (const pr of prs) {
await processPR(owner, repo, pr.number);
}
}
}
run().catch(console.error);