Skip to content

Commit bc14054

Browse files
authored
refactor: guardrails for branches (#3241)
1 parent 522523d commit bc14054

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

.github/scripts/cleanup-gh-pages.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ const FS = require('fs');
55

66
const TAG = 'cleanup-gh-pages:';
77

8+
// Directories that should never be removed by cleanup
9+
const PROTECTED_DIRS = ['main'];
10+
811
const removeOldFromPath = (isTag, data) => {
912
const path = `public/${isTag ? 'version' : 'review'}`;
1013
if (FS.existsSync(path) && data?.length > 0) {
14+
const dataNames = data.map((item) => item.name);
1115
const dirsToDelete = FS.readdirSync(path).filter(
12-
(file) => !data.find((branch) => branch.name === file)
16+
(file) => !PROTECTED_DIRS.includes(file) && !dataNames.includes(file)
1317
);
1418
if (dirsToDelete?.length > 0) {
1519
console.log(
@@ -35,18 +39,20 @@ const removeOldFromPath = (isTag, data) => {
3539

3640
module.exports = async ({ github, context }) => {
3741
const { repo, owner } = context.repo;
38-
const branches = await github.rest.repos.listBranches({
42+
43+
// Use pagination to fetch all branches and tags
44+
const branches = await github.paginate(github.rest.repos.listBranches, {
3945
owner,
40-
repo
46+
repo,
47+
per_page: 100
4148
});
42-
const tags = await github.rest.repos.listTags({
49+
const tags = await github.paginate(github.rest.repos.listTags, {
4350
owner,
44-
repo
51+
repo,
52+
per_page: 100
4553
});
4654

4755
return {
48-
deploy:
49-
removeOldFromPath(false, branches.data) ||
50-
removeOldFromPath(true, tags.data)
56+
deploy: removeOldFromPath(false, branches) || removeOldFromPath(true, tags)
5157
};
5258
};

.github/workflows/cleanup.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
return await script({github, context})
3333
3434
- name: 🥅 Deploy to GH-Pages
35+
if: fromJSON(steps.cleanup.outputs.result).deploy
3536
uses: peaceiris/actions-gh-pages@v4
3637
with:
3738
github_token: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)