-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61c99b1
commit 8fcded3
Showing
4 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
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
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.GITHUB_TOKEN }} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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); | ||
}); |