-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GitHub workflow to block PR if new fragments are created (#5882)
* testing workflow * gh workflow to block new fragments * remove test page * adding test page and test fragment * comment update * comment update * removing tests * revised comment --------- Co-authored-by: katiegoines <[email protected]>
- Loading branch information
1 parent
e1ee943
commit e294c61
Showing
2 changed files
with
77 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,35 @@ | ||
name: Block PRs with New Fragments | ||
on: | ||
pull_request: | ||
branches: [main] | ||
jobs: | ||
checkForNewFragments: | ||
name: Check for new fragments | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 https://github.com/actions/checkout/commit/f43a0e5ff2bd294095638e18286ca9a3d1956744 | ||
- name: Check if there are new fragments | ||
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 https://github.com/actions/github-script/commit/d7906e4ad0b1822421a7e6a35d5ca353c962f410 | ||
id: new-fragments-boolean | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
result-encoding: string | ||
script: | | ||
const { getAddedFragments } = require('./.github/workflows/scripts/check_for_new_fragments.js'); | ||
return getAddedFragments({github, context, core}); | ||
- name: Add Comment to PR if new fragments have been added | ||
if: ${{ steps.new-fragments-boolean.outputs.result > 0 }} | ||
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 https://github.com/actions/github-script/commit/d7906e4ad0b1822421a7e6a35d5ca353c962f410 | ||
env: | ||
CURRENT_BRANCH: ${{ github.head_ref }} | ||
CURRENT_REPO: ${{ github.repository }} | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const { addComment } = require('./.github/workflows/scripts/check_for_new_fragments.js'); | ||
addComment({github, context}); | ||
- name: Fail if new fragments have been added | ||
if: ${{ steps.new-fragments-boolean.outputs.result > 0 }} | ||
run: exit 1 | ||
|
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,42 @@ | ||
module.exports = { | ||
getAddedFragments: ({ 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) => { | ||
const newFragments = []; | ||
response.data.forEach((file) => { | ||
if ( | ||
file.status === 'added' && | ||
file.filename.startsWith('src/fragments') | ||
) { | ||
newFragments.push(file); | ||
} | ||
}); | ||
return newFragments.length; | ||
} | ||
); | ||
}, | ||
|
||
addComment: async ({ github, context }) => { | ||
const { | ||
issue: { number: issue_number }, | ||
repo: { owner, repo } | ||
} = context; | ||
|
||
const useInlineFiltersComment = | ||
'Amplify Docs is moving away from the use of Fragments. Please instead use InlineFilter. See our [README](https://github.com/aws-amplify/docs/blob/main/Readme.md#inline-filters) for more information.'; | ||
github.rest.issues.createComment({ | ||
owner, | ||
repo, | ||
issue_number, | ||
body: useInlineFiltersComment | ||
}); | ||
} | ||
}; |