Repo sync #824
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
| name: 'Orphaned files check' | |
| # This workflow opens deletion PRs for unused assets, reusables, and tables | |
| # so orphaned files leave the repo. | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: '20 16 * * 1' | |
| pull_request: | |
| paths: | |
| - .github/workflows/orphaned-assets-check.yml | |
| - .github/workflows/orphaned-files-check.yml | |
| # Dependency changes can break the orphaned-files scripts. | |
| - 'package*.json' | |
| - src/assets/scripts/find-orphaned-assets.ts | |
| - src/content-render/scripts/reusables-cli/find/unused.ts | |
| - src/data-directory/scripts/find-orphaned-tables.ts | |
| - src/workflows/walk-files.ts | |
| - src/languages/lib/languages.ts | |
| - .github/actions/clone-translations/action.yml | |
| - .github/actions/node-npm-setup/action.yml | |
| permissions: | |
| contents: read | |
| jobs: | |
| orphaned-files-check: | |
| if: ${{ github.repository == 'github/docs-internal' }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout English repo | |
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | |
| with: | |
| # A PAT triggers CI on the PR; GITHUB_TOKEN events do not start workflows. | |
| token: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| # Translations can lag English, so clone them before deciding that a file is unused. | |
| - name: Clone all translations | |
| uses: ./.github/actions/clone-translations | |
| with: | |
| token: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| - uses: ./.github/actions/node-npm-setup | |
| - name: Check for orphaned assets and reusables | |
| env: | |
| # The gh CLI reads GITHUB_TOKEN. | |
| GITHUB_TOKEN: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| DRY_RUN: ${{ github.event_name == 'pull_request'}} | |
| run: | | |
| set -e | |
| # The -s flag keeps npm script-alias noise out of the file lists. | |
| assetFilesToRemove=$(npm run -s find-orphaned-assets) | |
| reusableFilesToRemove=$(npm run -s reusables -- find unused | grep '^data/reusables' || true) | |
| tableFilesToRemove=$(npm run -s find-orphaned-tables) | |
| [ -z "$assetFilesToRemove" ] && [ -z "$reusableFilesToRemove" ] && [ -z "$tableFilesToRemove" ] && exit 0 | |
| if [ -n "$assetFilesToRemove" ]; then | |
| echo $assetFilesToRemove | xargs git rm | |
| fi | |
| if [ -n "$reusableFilesToRemove" ]; then | |
| echo $reusableFilesToRemove | xargs git rm | |
| fi | |
| if [ -n "$tableFilesToRemove" ]; then | |
| echo $tableFilesToRemove | xargs git rm | |
| fi | |
| git status | |
| git status -- ':!translations*' | grep 'nothing to commit' && exit 0 | |
| # Pull request runs exercise the tooling without pushing deletion PRs. | |
| if [ "$DRY_RUN" = "true" ]; then | |
| echo "Dry-run mode when run in a pull request" | |
| exit 0 | |
| fi | |
| git config --global user.name "docs-bot" | |
| git config --global user.email "77750099+docs-bot@users.noreply.github.com" | |
| date=$(date '+%Y-%m-%d-%H-%M') | |
| branchname=orphaned-files-$date-$GITHUB_RUN_ID | |
| git checkout -b $branchname | |
| git commit -m "Delete orphaned files $date" | |
| git push origin $branchname | |
| body=$(cat <<-EOM | |
| Found with the `npm run find-orphaned-assets`, `npm run -s reusables -- find unused`, and `npm run find-orphaned-tables` scripts. | |
| The orphaned files workflow file .github/workflows/orphaned-files-check.yml runs every Monday at 16:20 UTC / 8:20 PST. | |
| If you are the first responder, please spot check some of the unused assets, reusables, and tables to make sure they aren't referenced anywhere. Then, approve and merge the pull request. | |
| For more information, see [Doc: Orphaned Assets](https://github.com/github/technical-content/blob/main/engineering/orphaned-assets.md) and [Doc: Reusables CLI](https://github.com/github/docs-internal/tree/main/src/content-render/scripts/reusables-cli). | |
| Generated by the [orphaned files workflow run]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID). | |
| EOM | |
| ) | |
| gh pr create \ | |
| --title "Delete orphaned files ($date)" \ | |
| --body "$body" \ | |
| --repo github/docs-internal \ | |
| --label docs-content-fr,workflow-generated | |
| - uses: ./.github/actions/create-workflow-failure-issue | |
| id: create-failure-issue | |
| if: ${{ failure() && github.event_name == 'schedule' }} | |
| with: | |
| token: ${{ secrets.DOCS_BOT_PAT_BASE }} | |
| - uses: ./.github/actions/slack-alert | |
| if: ${{ failure() && github.event_name == 'schedule' }} | |
| with: | |
| slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }} | |
| issue_url: ${{ steps.create-failure-issue.outputs.issue_url }} |