Skip to content

Commit

Permalink
Add PR labeling
Browse files Browse the repository at this point in the history
  • Loading branch information
jpwilliams committed Nov 17, 2023
1 parent 61c99b1 commit 04c4323
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/label-prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Label PR based on changed package

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
label:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- uses: ./.github/actions/setup-and-build
with:
install-dependencies: false
build: false

- run: pnpm install

- name: Label PR
run: node scripts/labelPrs.js
env:
GITHUB_TOKEN: ${{ secrets.CHANGESET_GITHUB_TOKEN }} # inngest-release-bot
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"devDependencies": {
"@actions/core": "^1.10.0",
"@actions/exec": "^1.1.1",
"@actions/github": "^6.0.0",
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.2",
"cross-env": "^7.0.3"
Expand Down
122 changes: 122 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions scripts/labelPrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const github = require("@actions/github");
const fs = require("fs").promises;
const path = require("path");

const token = process.env.GITHUB_TOKEN;
const octokit = github.getOctokit(token);

const rootDir = path.resolve(__dirname, "..");

async function getChangedPackages() {
const prNumber = github.context.payload.pull_request.number;
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;

const { data: files } = await octokit.rest.pulls.listFiles({
owner,
repo,
pull_number: prNumber,
});

const changedPackages = new Set();

for (const file of files) {
if (file.filename.startsWith("packages/")) {
const packageName = file.filename.split("/")[1];
changedPackages.add(packageName);
}
}

return changedPackages;
}

async function getLabelsForPackages(packageNames) {
const labels = [];

for (const packageName of packageNames) {
const packageJsonPath = path.resolve(
rootDir,
"packages",
packageName,
"package.json"
);
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, "utf8"));
const label = `📦 ${packageJson.name}`;
labels.push(label);
}

return labels;
}

async function labelPR(labels) {
if (labels.length === 0) return;

const prNumber = github.context.payload.pull_request.number;
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;

await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: prNumber,
labels: labels,
});
}

async function run() {
const changedPackages = await getChangedPackages();
if (changedPackages.size > 0) {
const labels = await getLabelsForPackages(changedPackages);
await labelPR(labels);
}
}

run().catch((error) => {
console.error(error);
process.exit(1);
});

0 comments on commit 04c4323

Please sign in to comment.