-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
meta: request reviews from github actions #53149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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: Request reviews for PRs | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened] | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| issues: write | ||
|
|
||
| jobs: | ||
| main: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 | ||
| name: Checkout default branch | ||
| - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
| name: Make initial comment | ||
| with: | ||
| script: | | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const initialCommentBuilder = require('${{ github.workspace }}/tools/actions/pr-open.js'); | ||
| await initialCommentBuilder(github, context); | ||
This file contains hidden or 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,86 @@ | ||
| 'use strict'; | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| module.exports = async (client, context) => { | ||
| function globToRegex(glob) { | ||
| const patterns = { | ||
| '*': '([^\\/]+)', | ||
| '**': '(.+\\/)?([^\\/]+)', | ||
| '**/': '(.+\\/)', | ||
| }; | ||
|
|
||
| const escapedGlob = glob.replace(/\./g, '\\.').replace(/\*\*$/g, '(.+)'); | ||
| const regexString = '^(' + escapedGlob.replace(/\*\*\/|\*\*|\*/g, (match) => patterns[match] || '') + ')$'; | ||
| return new RegExp(regexString); | ||
| } | ||
|
|
||
| class CodeOwners { | ||
| constructor(fileContent) { | ||
| this.owners = fileContent.split('\n').map((line) => line.trim()) | ||
| .filter((line) => line && !line.startsWith('#')) | ||
| .map((line) => { | ||
| const [path, ...owners] = line.split(/\s+/); | ||
| return { path: globToRegex(path), owners }; | ||
| }); | ||
| } | ||
|
|
||
| getOwners(filePath) { | ||
| const normalizedPath = filePath.startsWith('/') ? filePath : `/${filePath}`; | ||
| return this.owners | ||
| .filter(({ path }) => path.test(normalizedPath)) | ||
| .flatMap(({ owners }) => owners); | ||
| } | ||
| } | ||
|
|
||
| async function getChangedFiles(base, head) { | ||
| const { data } = await client.rest.repos.compareCommits({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| base, | ||
| head, | ||
| }); | ||
| return data.files.map((file) => file.filename); | ||
| } | ||
|
|
||
| async function getCodeOwners() { | ||
| const { data } = await client.rest.repos.getContent({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| path: '.github/CODEOWNERS', | ||
| }); | ||
|
|
||
| const content = Buffer.from(data.content, 'base64').toString('utf8'); | ||
| return new CodeOwners(content); | ||
| } | ||
|
|
||
| async function makeInitialComment(changedFiles) { | ||
| const codeOwners = await getCodeOwners(); | ||
|
|
||
| const owners = new Set(); | ||
| changedFiles.forEach((file) => { | ||
| codeOwners.getOwners(file).forEach((owner) => owners.add(owner)); | ||
| }); | ||
|
|
||
| const ownersList = Array.from(owners).map((owner) => `- ${owner}`).join('\n'); | ||
|
|
||
| const body = | ||
| 'The following teams have been pinged to review your changes:\n\n' + | ||
| ownersList; | ||
|
|
||
| return client.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.payload.pull_request.number, | ||
| body: body, | ||
| }); | ||
| } | ||
|
|
||
| const base = context.payload.pull_request?.base?.sha; | ||
| const head = context.payload.pull_request?.head?.sha; | ||
|
|
||
| if (!base || !head) { | ||
| throw new Error('Cannot get base or head commit'); | ||
| } | ||
|
|
||
| const changedFiles = await getChangedFiles(base, head); | ||
| await makeInitialComment(changedFiles); | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.