-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add workflow to check for new files so CODEOWNERS can be updated #5110
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b661a58
Add workflow to check for new files
timngyn f3671a0
update codeowners link
timngyn e1165d7
Add newlines
timngyn 2b1b385
Move var into env var
timngyn ccc9eff
Merge remote-tracking branch 'origin/main' into check-for-new-files-w…
timngyn d947944
Update workflow to pull codeowners from current branch
timngyn 539a4f2
Only run check if there are new files
timngyn 97ec9de
Split out inline github scripts into its own JS file
timngyn 8c25fd3
Fix typos
timngyn 38c6ebb
Don't filter out documentation-team owners
timngyn 9f8890a
Console log new files found before adding a comment to the PR
timngyn 5bb489f
Don't hardcode fetch url
timngyn e13dd81
Specify version for ignore package
timngyn f91fe80
Update workflow based on feedback
timngyn ba154d7
Return the entire github.paginate call in order to return the file count
timngyn 024c87b
Merge branch 'main' into check-for-new-files-workflow
timngyn 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,37 @@ | ||
name: Validate CODEOWNERS | ||
on: | ||
pull_request: | ||
branches: [main] | ||
jobs: | ||
checkIfCodeownersUpdateNeeded: | ||
permissions: | ||
pull-requests: write # Used to add comment to PR | ||
name: Check if CODEOWNERS needs to be updated | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
- name: Get count of added files | ||
uses: actions/[email protected] | ||
id: set-added-files-count | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
result-encoding: string | ||
script: | | ||
const { getAddedFiles } = require('./.github/workflows/scripts/check_for_new_files.js'); | ||
return getAddedFiles({github, context, core}); | ||
- name: Install npm package ignore | ||
if: ${{ steps.set-added-files-count.outputs.result > 0 }} | ||
run: yarn add [email protected] -W # help verify CODEOWNERS | ||
- name: Check file against CODEOWNERS | ||
if: ${{ steps.set-added-files-count.outputs.result > 0 }} | ||
uses: actions/[email protected] | ||
env: | ||
CURRENT_BRANCH: ${{ github.head_ref }} | ||
CURRENT_REPO: ${{ github.repository }} | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const ignore = require('ignore'); | ||
const { validateCodeowners } = require('./.github/workflows/scripts/check_for_new_files.js'); | ||
validateCodeowners({github, context, fetch, ignore}); |
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,82 @@ | ||
module.exports = { | ||
getAddedFiles: ({ github, context, core }) => { | ||
const { | ||
issue: { number: issue_number }, | ||
repo: { owner, repo } | ||
} = context; | ||
|
||
// Use the Github API to query for the list of files from the PR | ||
return github | ||
.paginate( | ||
'GET /repos/{owner}/{repo}/pulls/{pull_number}/files', | ||
{ owner, repo, pull_number: issue_number }, | ||
(response) => response.data.filter((file) => file.status === 'added') | ||
) | ||
.then((files) => { | ||
// Save these values to the Github env | ||
core.exportVariable('NEW_FILES', files); | ||
|
||
console.log('New files: ', files); | ||
|
||
// Return the new files count to be used in the github workflow | ||
return files.length; | ||
}); | ||
}, | ||
validateCodeowners: async ({ github, context, fetch, ignore }) => { | ||
const { NEW_FILES, CURRENT_BRANCH, CURRENT_REPO } = process.env; | ||
|
||
const codeownersFile = `https://github.com/raw/${CURRENT_REPO}/${CURRENT_BRANCH}/.github/CODEOWNERS`; | ||
|
||
console.log('Fetching CODEOWNERS from: ', codeownersFile); | ||
|
||
const response = await fetch(codeownersFile); | ||
const body = await response.text(); | ||
|
||
// Filter out comments from CODEOWNERS file | ||
const codeownersFilePatterns = body | ||
.split('\n') | ||
.filter((e) => !e.startsWith('#')) | ||
.filter((e) => e.length > 1) | ||
.map((e) => e.split(/\s+/)[0]); | ||
|
||
console.log( | ||
'CODEOWNERS patterns to match new files against: ', | ||
codeownersFilePatterns | ||
); | ||
|
||
// Add the patterns to the ignore package | ||
const ig = ignore().add(codeownersFilePatterns); | ||
|
||
const filesNotInCodeowners = []; | ||
|
||
JSON.parse(NEW_FILES).forEach((newFile) => { | ||
// Check if the file isn't covered by our list of patterns | ||
if (!ig.ignores(newFile.filename)) { | ||
console.log(`${newFile.filename} is not covered by CODEOWNERS`); | ||
filesNotInCodeowners.push(newFile.filename); | ||
} | ||
}); | ||
|
||
const { | ||
issue: { number: issue_number }, | ||
repo: { owner, repo } | ||
} = context; | ||
|
||
console.log('New files: ', filesNotInCodeowners); | ||
|
||
// If we found files not covered by CODEOWNERS, then add a comment to the PR | ||
if (filesNotInCodeowners.length > 0) { | ||
const files = filesNotInCodeowners.map((e) => `- ${e}\n`).join(''); | ||
|
||
const needCodeownersUpdateComment = `CODEOWNERS need to be updated because these new files are not covered:\n ${files}`; | ||
github.rest.issues.createComment({ | ||
owner, | ||
repo, | ||
issue_number, | ||
body: needCodeownersUpdateComment | ||
}); | ||
const labels = ['update-codeowners']; | ||
github.rest.issues.addLabels({ owner, repo, issue_number, labels }); | ||
} | ||
} | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
await
required ifresponse
is already awaited?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea,
response.text()
is technically aPromise
still so I need to await it to get the actual string: https://developer.mozilla.org/en-US/docs/Web/API/Response/text