diff --git a/.github/scripts/main.js b/.github/scripts/main.js new file mode 100644 index 0000000..ebbd55d --- /dev/null +++ b/.github/scripts/main.js @@ -0,0 +1,24 @@ +const fs = require('fs'); +const { execSync } = require('child_process') + +const tsGraph = require('@andriyorehov/ts-graph'); + + +module.exports = async ({ github, context }) => { + const { commits } = context.payload.pull_request + + const rawFiles = execSync(`git diff --name-only HEAD HEAD~${commits}`).toString() + const changedFiles = rawFiles.split('\n').filter(Boolean) + + const owners = fs.readFileSync('owners.json', 'utf8'); + const ownersObj = JSON.parse(owners); + + for (const owner of ownersObj) { + for (const entries of owner.owns) { + const paths = Object.keys(tsGraph.getTreeByFile(entries).flatTree); + if (changedFiles.some(file => paths.includes(file))) { + execSync(`gh pr edit ${process.env.PR_NUMBER} --add-reviewer ${owner.team}`) + } + } + } +}; diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cbf0cdd..1b45200 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,11 +6,15 @@ on: branches: - main + jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 - uses: pnpm/action-setup@v2 with: version: 8 @@ -20,14 +24,15 @@ jobs: cache: 'pnpm' - name: Install modules run: pnpm install + - name: Check for automated commit + run: echo $(git diff-tree --no-commit-id --name-only -r HEAD) - name: Check changed files uses: actions/github-script@v6 + env: + GH_TOKEN: ${{ secrets.GH_TOKEN }} + PR_NUMBER: ${{ github.event.number }} with: script: | - const { execSync } = require('child_process') - const { commits } = context.payload.pull_request - console.log(commits) - const rawFiles = execSync(`git diff --name-only HEAD HEAD~${commits}`).toString() - const files = rawFiles.split('\n').filter(Boolean) + const script = require('.github/scripts/main.js') + script({github, context}) - console.log(files) diff --git a/main.ts b/main.js similarity index 57% rename from main.ts rename to main.js index 9ef07ab..7494c8f 100644 --- a/main.ts +++ b/main.js @@ -1,6 +1,6 @@ -import fs from 'fs'; +const fs = require('fs'); -import { getTreeByFile } from '@andriyorehov/ts-graph'; +const tsGraph = require('@andriyorehov/ts-graph'); const owners = fs.readFileSync('owners.json', 'utf8'); const ownersObj = JSON.parse(owners); @@ -8,6 +8,6 @@ const ownersObj = JSON.parse(owners); for (const owner of ownersObj) { for (const entries of owner.owns) { console.log(entries); - console.log(Object.keys(getTreeByFile(entries).flatTree)); + console.log(Object.keys(tsGraph.getTreeByFile(entries).flatTree)); } } diff --git a/main/entry1/nested.ts b/main/entry1/nested.ts index 258a787..a0f3e14 100644 --- a/main/entry1/nested.ts +++ b/main/entry1/nested.ts @@ -1 +1 @@ -export const nested = 'nested'; +export const nested = 'nested-new-'; diff --git a/owners.json b/owners.json index 7bdaa51..6125c68 100644 --- a/owners.json +++ b/owners.json @@ -1,10 +1,10 @@ [ { - "team": "entry1", + "team": "github-tooling/entry1", "owns": ["main/entry1/main.ts"] }, { - "team": "entry2", + "team": "github-tooling/entry2", "owns": ["main/entry2/main.ts"] } ]