Skip to content

Commit a2392e5

Browse files
committed
cleanup
1 parent 3a5a40c commit a2392e5

File tree

10 files changed

+369
-99
lines changed

10 files changed

+369
-99
lines changed

.github/workflows/report_test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ jobs:
9292
gh_pages: 'gh-pages-dir'
9393
report_dir: 'lighthouse-html'
9494
list_dirs: ${{ github.ref == 'refs/heads/main' }}
95+
branch_cleanup_enabled: ${{ github.ref == 'refs/heads/main' }}
96+
max_reports: 9
9597

9698
- name: Local Chart Report (single)
9799
if: ${{ always() && steps.allure.outputs.report_url }}
@@ -103,6 +105,7 @@ jobs:
103105
report_dir: 'test-results/Allure_Trend.csv'
104106
list_dirs: ${{ github.ref == 'refs/heads/main' }}
105107
report_type: csv
108+
max_reports: 9
106109

107110
- name: Local Chart Report (multiple)
108111
if: ${{ always() && steps.allure.outputs.report_url }}
@@ -114,6 +117,7 @@ jobs:
114117
report_dir: 'lighthouse-csv'
115118
list_dirs: ${{ github.ref == 'refs/heads/main' }}
116119
report_type: csv
120+
max_reports: 9
117121

118122
- name: Git Commit and Push Action
119123
uses: mgrybyk/git-commit-pull-push-action@v1

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ concurrency:
122122

123123
- [thollander/actions-comment-pull-request](https://github.com/thollander/actions-comment-pull-request) for building Github Action that comments the linked PRs
124124

125-
## Upcoming features
125+
## ## Planned features
126126

127-
- cleanup old reports
127+
- cleanup `data.json` file per report. Raise an issue if you're interested!

action.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,28 @@ inputs:
2424
description: 'Write index.html to the Github Action folders. Might cause concurrency issues!'
2525
required: false
2626
default: false
27+
list_dirs_branch:
28+
description: 'Write index.html to the report branch folder. Might cause concurrency issues!'
29+
required: false
30+
default: true
31+
branch_cleanup_enabled:
32+
description: 'Cleanup reports from deleted branches. Might cause concurrency issues!'
33+
required: false
34+
default: false
35+
max_reports:
36+
description: 'Max reports to keep per branch/report. Set to 0 to disable cleanup.'
37+
required: false
38+
default: 100
2739
outputs:
2840
# report_url and report_history_url have the same url for CSV reports
2941
report_url:
3042
description: 'Published Report url'
3143
report_history_url:
3244
description: 'Published History list url'
45+
run_unique_id:
46+
description: 'Name of the folder containing the generated Allure Report'
47+
report_path:
48+
description: 'Path to the folder containing the generated Allure Report relative to the repo root'
3349
runs:
3450
using: 'node20'
3551
main: 'dist/index.js'

dist/index.js

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27751,6 +27751,9 @@ function httpRedirectFetch (fetchParams, response) {
2775127751
// https://fetch.spec.whatwg.org/#cors-non-wildcard-request-header-name
2775227752
request.headersList.delete('authorization')
2775327753

27754+
// https://fetch.spec.whatwg.org/#authentication-entries
27755+
request.headersList.delete('proxy-authorization', true)
27756+
2775427757
// "Cookie" and "Host" are forbidden request-headers, which undici doesn't implement.
2775527758
request.headersList.delete('cookie')
2775627759
request.headersList.delete('host')
@@ -38586,7 +38589,9 @@ __nccwpck_require__.a(module, async (__webpack_handle_async_dependencies__, __we
3858638589
/* harmony import */ var _src_csvReport_js__WEBPACK_IMPORTED_MODULE_4__ = __nccwpck_require__(7925);
3858738590
/* harmony import */ var _src_isFileExists_js__WEBPACK_IMPORTED_MODULE_5__ = __nccwpck_require__(2139);
3858838591
/* harmony import */ var _src_writeFolderListing_js__WEBPACK_IMPORTED_MODULE_6__ = __nccwpck_require__(4362);
38589-
/* harmony import */ var _src_helpers_js__WEBPACK_IMPORTED_MODULE_7__ = __nccwpck_require__(3015);
38592+
/* harmony import */ var _src_helpers_js__WEBPACK_IMPORTED_MODULE_8__ = __nccwpck_require__(3015);
38593+
/* harmony import */ var _src_cleanup_js__WEBPACK_IMPORTED_MODULE_7__ = __nccwpck_require__(646);
38594+
3859038595

3859138596

3859238597

@@ -38604,7 +38609,10 @@ try {
3860438609
const reportId = _actions_core__WEBPACK_IMPORTED_MODULE_1__.getInput('report_id');
3860538610
const reportType = _actions_core__WEBPACK_IMPORTED_MODULE_1__.getInput('report_type');
3860638611
const listDirs = _actions_core__WEBPACK_IMPORTED_MODULE_1__.getInput('list_dirs') == 'true';
38607-
const branchName = (0,_src_helpers_js__WEBPACK_IMPORTED_MODULE_7__/* .getBranchName */ .L)(_actions_github__WEBPACK_IMPORTED_MODULE_2__.context.ref, _actions_github__WEBPACK_IMPORTED_MODULE_2__.context.payload.pull_request);
38612+
const listDirsBranch = _actions_core__WEBPACK_IMPORTED_MODULE_1__.getInput('list_dirs_branch') == 'true';
38613+
const branchCleanupEnabled = _actions_core__WEBPACK_IMPORTED_MODULE_1__.getInput('branch_cleanup_enabled') == 'true';
38614+
const maxReports = parseInt(_actions_core__WEBPACK_IMPORTED_MODULE_1__.getInput('max_reports'), 10);
38615+
const branchName = (0,_src_helpers_js__WEBPACK_IMPORTED_MODULE_8__/* .getBranchName */ .L)(_actions_github__WEBPACK_IMPORTED_MODULE_2__.context.ref, _actions_github__WEBPACK_IMPORTED_MODULE_2__.context.payload.pull_request);
3860838616
const reportBaseDir = path__WEBPACK_IMPORTED_MODULE_0__.join(ghPagesPath, baseDir, branchName, reportId);
3860938617
/**
3861038618
* `runId` is unique but won't change on job re-run
@@ -38631,6 +38639,9 @@ try {
3863138639
reportDir,
3863238640
report_url: reportUrl,
3863338641
listDirs,
38642+
listDirsBranch,
38643+
branchCleanupEnabled,
38644+
maxReports,
3863438645
});
3863538646
if (!(await (0,_src_isFileExists_js__WEBPACK_IMPORTED_MODULE_5__/* .isFileExist */ .e)(ghPagesPath))) {
3863638647
throw new Error("Folder with gh-pages branch doesn't exist: " + ghPagesPath);
@@ -38655,6 +38666,9 @@ try {
3865538666
await (0,_src_writeFolderListing_js__WEBPACK_IMPORTED_MODULE_6__/* .writeFolderListing */ .l)(ghPagesPath, '.');
3865638667
}
3865738668
await (0,_src_writeFolderListing_js__WEBPACK_IMPORTED_MODULE_6__/* .writeFolderListing */ .l)(ghPagesPath, baseDir);
38669+
}
38670+
if (listDirsBranch) {
38671+
await (0,_src_writeFolderListing_js__WEBPACK_IMPORTED_MODULE_6__/* .writeFolderListing */ .l)(ghPagesPath, path__WEBPACK_IMPORTED_MODULE_0__.join(baseDir, branchName));
3865838672
await (0,_src_writeFolderListing_js__WEBPACK_IMPORTED_MODULE_6__/* .writeFolderListing */ .l)(ghPagesPath, path__WEBPACK_IMPORTED_MODULE_0__.join(baseDir, branchName));
3865938673
if (reportType === 'html') {
3866038674
await (0,_src_writeFolderListing_js__WEBPACK_IMPORTED_MODULE_6__/* .writeFolderListing */ .l)(ghPagesPath, path__WEBPACK_IMPORTED_MODULE_0__.join(baseDir, branchName, reportId));
@@ -38663,6 +38677,14 @@ try {
3866338677
// outputs
3866438678
_actions_core__WEBPACK_IMPORTED_MODULE_1__.setOutput('report_url', reportUrl);
3866538679
_actions_core__WEBPACK_IMPORTED_MODULE_1__.setOutput('report_history_url', ghPagesBaseDir);
38680+
_actions_core__WEBPACK_IMPORTED_MODULE_1__.setOutput('run_unique_id', runUniqueId);
38681+
_actions_core__WEBPACK_IMPORTED_MODULE_1__.setOutput('report_path', reportDir);
38682+
if (branchCleanupEnabled) {
38683+
await (0,_src_cleanup_js__WEBPACK_IMPORTED_MODULE_7__/* .cleanupOutdatedBranches */ .B)(ghPagesBaseDir);
38684+
}
38685+
if (maxReports > 0) {
38686+
await (0,_src_cleanup_js__WEBPACK_IMPORTED_MODULE_7__/* .cleanupOutdatedReports */ .g)(ghPagesBaseDir, maxReports);
38687+
}
3866638688
}
3866738689
catch (error) {
3866838690
_actions_core__WEBPACK_IMPORTED_MODULE_1__.setFailed(error.message);
@@ -38671,6 +38693,107 @@ catch (error) {
3867138693
__webpack_async_result__();
3867238694
} catch(e) { __webpack_async_result__(e); } }, 1);
3867338695

38696+
/***/ }),
38697+
38698+
/***/ 646:
38699+
/***/ ((__unused_webpack_module, __webpack_exports__, __nccwpck_require__) => {
38700+
38701+
38702+
// EXPORTS
38703+
__nccwpck_require__.d(__webpack_exports__, {
38704+
"B": () => (/* binding */ cleanupOutdatedBranches),
38705+
"g": () => (/* binding */ cleanupOutdatedReports)
38706+
});
38707+
38708+
// EXTERNAL MODULE: external "path"
38709+
var external_path_ = __nccwpck_require__(1017);
38710+
// EXTERNAL MODULE: external "fs/promises"
38711+
var promises_ = __nccwpck_require__(3292);
38712+
;// CONCATENATED MODULE: external "child_process"
38713+
const external_child_process_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("child_process");
38714+
;// CONCATENATED MODULE: ./src/spawnProcess.ts
38715+
38716+
const logError = (err, output) => {
38717+
console.log(output.join(''));
38718+
return err;
38719+
};
38720+
const spawnProcess = async (command, args, cwd) => {
38721+
const childProcess = external_child_process_namespaceObject.spawn(command, args, { cwd });
38722+
return new Promise((resolve, reject) => {
38723+
const output = [];
38724+
const r1 = childProcess.stdout?.on('data', (d) => output.push(d.toString()));
38725+
const r2 = childProcess.stderr?.on('data', (d) => output.push(d.toString()));
38726+
const p1 = new Promise((resolve) => (r1 ? r1.once('close', resolve) : resolve()));
38727+
const p2 = new Promise((resolve) => (r2 ? r2.once('close', resolve) : resolve()));
38728+
childProcess.once('error', (err) => reject(logError(err, output)));
38729+
childProcess.once('exit', async (code) => {
38730+
r1?.removeAllListeners('data');
38731+
r2?.removeAllListeners('data');
38732+
await p1;
38733+
await p2;
38734+
return code === 0 ? resolve(output.join('')) : reject(logError(code, output));
38735+
});
38736+
});
38737+
};
38738+
38739+
// EXTERNAL MODULE: ./src/helpers.ts
38740+
var helpers = __nccwpck_require__(3015);
38741+
;// CONCATENATED MODULE: ./src/cleanup.ts
38742+
38743+
38744+
38745+
38746+
const cleanupOutdatedBranches = async (ghPagesBaseDir) => {
38747+
try {
38748+
const prefix = 'refs/heads/';
38749+
const lsRemote = await spawnProcess('git', ['ls-remote', '--heads']);
38750+
const remoteBranches = lsRemote
38751+
.split('\n')
38752+
.filter((l) => l.includes(prefix))
38753+
.map((l) => (0,helpers/* normalizeBranchName */.i)(l.split(prefix)[1]));
38754+
const localBranches = (await promises_.readdir(ghPagesBaseDir, { withFileTypes: true })).filter((d) => d.isDirectory()).map((d) => d.name);
38755+
for (const localBranch of localBranches) {
38756+
if (!remoteBranches.includes(localBranch)) {
38757+
await promises_.rm(external_path_.join(ghPagesBaseDir, localBranch), { recursive: true, force: true });
38758+
}
38759+
}
38760+
}
38761+
catch (err) {
38762+
console.error('cleanup outdated branches failed.', err);
38763+
}
38764+
};
38765+
const cleanupOutdatedReports = async (ghPagesBaseDir, maxReports) => {
38766+
try {
38767+
const localBranches = (await promises_.readdir(ghPagesBaseDir, { withFileTypes: true })).filter((d) => d.isDirectory()).map((d) => d.name);
38768+
// branches
38769+
for (const localBranch of localBranches) {
38770+
const reports = (await promises_.readdir(external_path_.join(ghPagesBaseDir, localBranch), { withFileTypes: true }))
38771+
.filter((d) => d.isDirectory())
38772+
.map((d) => d.name);
38773+
// report per branch
38774+
for (const reportName of reports) {
38775+
const runs = (await promises_.readdir(external_path_.join(ghPagesBaseDir, localBranch, reportName), { withFileTypes: true }))
38776+
.filter((d) => d.isDirectory())
38777+
.map((d) => d.name);
38778+
// run per report
38779+
if (runs.length > maxReports) {
38780+
runs.sort();
38781+
while (runs.length > maxReports) {
38782+
await promises_.rm(external_path_.join(ghPagesBaseDir, localBranch, reportName, runs.shift()), {
38783+
recursive: true,
38784+
force: true,
38785+
});
38786+
}
38787+
}
38788+
}
38789+
}
38790+
}
38791+
catch (err) {
38792+
console.error('cleanup outdated reports failed.', err);
38793+
}
38794+
};
38795+
38796+
3867438797
/***/ }),
3867538798

3867638799
/***/ 7925:
@@ -38788,11 +38911,13 @@ const csvReport = async (sourceReportDir, reportBaseDir, reportId, meta) => {
3878838911
/***/ ((__unused_webpack_module, __webpack_exports__, __nccwpck_require__) => {
3878938912

3879038913
/* harmony export */ __nccwpck_require__.d(__webpack_exports__, {
38791-
/* harmony export */ "L": () => (/* binding */ getBranchName)
38914+
/* harmony export */ "L": () => (/* binding */ getBranchName),
38915+
/* harmony export */ "i": () => (/* binding */ normalizeBranchName)
3879238916
/* harmony export */ });
38917+
const normalizeBranchName = (branchName) => branchName.replaceAll('/', '_').replaceAll('.', '_');
3879338918
const getBranchName = (gitRef, pull_request) => {
3879438919
const branchName = pull_request ? pull_request.head.ref : gitRef.replace('refs/heads/', '');
38795-
return branchName.replaceAll('/', '_').replaceAll('.', '_');
38920+
return normalizeBranchName(branchName);
3879638921
};
3879738922

3879838923

index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { csvReport } from './src/csvReport.js'
66
import { isFileExist } from './src/isFileExists.js'
77
import { shouldWriteRootHtml, writeFolderListing } from './src/writeFolderListing.js'
88
import { getBranchName } from './src/helpers.js'
9+
import { cleanupOutdatedBranches, cleanupOutdatedReports } from './src/cleanup.js'
910

1011
const baseDir = 'report-action'
1112

@@ -18,6 +19,9 @@ try {
1819
const reportId = core.getInput('report_id')
1920
const reportType = core.getInput('report_type')
2021
const listDirs = core.getInput('list_dirs') == 'true'
22+
const listDirsBranch = core.getInput('list_dirs_branch') == 'true'
23+
const branchCleanupEnabled = core.getInput('branch_cleanup_enabled') == 'true'
24+
const maxReports = parseInt(core.getInput('max_reports'), 10)
2125
const branchName = getBranchName(github.context.ref, github.context.payload.pull_request)
2226
const reportBaseDir = path.join(ghPagesPath, baseDir, branchName, reportId)
2327

@@ -49,6 +53,9 @@ try {
4953
reportDir,
5054
report_url: reportUrl,
5155
listDirs,
56+
listDirsBranch,
57+
branchCleanupEnabled,
58+
maxReports,
5259
})
5360

5461
if (!(await isFileExist(ghPagesPath))) {
@@ -77,6 +84,9 @@ try {
7784
await writeFolderListing(ghPagesPath, '.')
7885
}
7986
await writeFolderListing(ghPagesPath, baseDir)
87+
}
88+
if (listDirsBranch) {
89+
await writeFolderListing(ghPagesPath, path.join(baseDir, branchName))
8090
await writeFolderListing(ghPagesPath, path.join(baseDir, branchName))
8191
if (reportType === 'html') {
8292
await writeFolderListing(ghPagesPath, path.join(baseDir, branchName, reportId))
@@ -86,6 +96,15 @@ try {
8696
// outputs
8797
core.setOutput('report_url', reportUrl)
8898
core.setOutput('report_history_url', ghPagesBaseDir)
99+
core.setOutput('run_unique_id', runUniqueId)
100+
core.setOutput('report_path', reportDir)
101+
102+
if (branchCleanupEnabled) {
103+
await cleanupOutdatedBranches(ghPagesBaseDir)
104+
}
105+
if (maxReports > 0) {
106+
await cleanupOutdatedReports(ghPagesBaseDir, maxReports)
107+
}
89108
} catch (error) {
90109
core.setFailed(error.message)
91110
}

0 commit comments

Comments
 (0)