Skip to content

Commit 5d7973b

Browse files
authored
make PR about deletable orphan assets (github#26484)
* warn on orphan images * oops * temporarily adding orphan image * fix the bug * permission * better comment * try this * try this * debugging * debugging * using fs * cjs * remove experiment image * debugging * debugging * try that * try that * git config * cleaning * token * body * label * try that * try that * try that * not upstream * try that * token twice * try again * cleaning up * fix unit test * one more exception * feedbacked
1 parent d191408 commit 5d7973b

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

.github/workflows/orphaned-assets-check.yml

+35-5
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ name: 'Orphaned assets check'
55
# **Who does it impact**: Docs content.
66

77
on:
8-
pull_request:
9-
push:
10-
branches:
11-
- gh-readonly-queue/main/**
8+
workflow_dispatch:
9+
schedule:
10+
- cron: '13 10 * * *' # Once a day at 10:13 UTC
1211

1312
permissions:
1413
contents: read
1514

1615
jobs:
1716
orphaned-assets-check:
17+
if: ${{ github.repository == 'github/docs-internal' }}
1818
runs-on: ubuntu-latest
1919
steps:
2020
- name: Checkout
2121
uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579
22+
with:
23+
# Using a PAT is necessary so that the new commit will trigger the
24+
# CI in the PR. (Events from GITHUB_TOKEN don't trigger new workflows.)
25+
token: ${{ secrets.DOCUBOT_REPO_PAT }}
2226

2327
- name: Setup node
2428
uses: actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561
@@ -30,4 +34,30 @@ jobs:
3034
run: npm ci
3135

3236
- name: Check for orphaned assets
33-
run: ./script/find-orphaned-assets.mjs --verbose --exit
37+
env:
38+
# Needed for gh
39+
GITHUB_TOKEN: ${{ secrets.DOCUBOT_REPO_PAT }}
40+
run: |
41+
set -e
42+
43+
./script/find-orphaned-assets.mjs | xargs git rm
44+
45+
# If nothing to commit, exit now. It's fine. No orphans.
46+
git status | grep 'nothing to commit' && exit 0
47+
48+
# Replicated from the translation pipeline PR-maker Action
49+
git config --global user.name "docubot"
50+
git config --global user.email "[email protected]"
51+
52+
date=$(date '+%Y-%m-%d-%H-%M')
53+
branchname=orphaned-assets-$date-$GITHUB_RUN_ID
54+
55+
git checkout -b $branchname
56+
git commit -m "Delete orphaned assets $date"
57+
git push origin $branchname
58+
59+
gh pr create \
60+
--title "Delete orphaned assets ($date)" \
61+
--body "Found with the find-orphaned-assets.mjs script" \
62+
--repo github/docs-internal \
63+
--label docs-content-fr

script/find-orphaned-assets.mjs

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ import path from 'path'
1212
import program from 'commander'
1313
import walk from 'walk-sync'
1414

15-
const EXCEPTIONS = new Set(['assets/images/site/favicon.ico'])
15+
const EXCEPTIONS = new Set([
16+
'assets/images/site/favicon.ico',
17+
'assets/images/site/apple-touch-icon.png',
18+
])
1619

1720
function isExceptionPath(imagePath) {
1821
// We also check for .DS_Store because any macOS user that has opened
1922
// a folder with images will have this on disk. It won't get added
2023
// to git anyway thanks to our .DS_Store.
2124
// But if we don't make it a valid exception, it can become inconvenient
2225
// to run this script locally.
23-
return EXCEPTIONS.has(imagePath) || path.basename('.DS_Store')
26+
return (
27+
EXCEPTIONS.has(imagePath) ||
28+
path.basename(imagePath) === '.DS_Store' ||
29+
imagePath.split(path.sep).includes('early-access')
30+
)
2431
}
2532

2633
program
@@ -61,7 +68,7 @@ async function main(opts) {
6168
root,
6269
Object.assign(
6370
{
64-
globs: ['!**/*.+(png|csv|graphql|json|svg)'],
71+
globs: ['!**/*.+(png|jpe?g|csv|graphql|json|svg)'],
6572
},
6673
walkOptions
6774
)

0 commit comments

Comments
 (0)