cleanup-release 動作重整與安全強化 #2
+110
-31
@@ -191,14 +191,52 @@ function requireValue(name, value, displayValue = value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 驗證字串是否為非負整數。
|
* 驗證字串是否為正整數,且落在安全整數範圍內;下限 1 可避免把保留數設成 0 而清空所有 release。
|
||||||
*
|
*
|
||||||
* @param {string} name 參數名稱。
|
* @param {string} name 參數名稱。
|
||||||
* @param {string} value 參數值。
|
* @param {string} value 參數值。
|
||||||
*/
|
*/
|
||||||
function requireInteger(name, value) {
|
function requirePositiveInteger(name, value) {
|
||||||
if (!/^[0-9]+$/.test(value)) {
|
if (!/^[0-9]+$/.test(value) || !Number.isSafeInteger(Number(value)) || Number(value) < 1) {
|
||||||
fail(`${name} must be a non-negative integer`);
|
fail(`${name} must be a positive integer within the safe integer range`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 驗證值是否為合法的 HTTPS 絕對 URL,避免 URL 解析失敗留到主流程中途才拋出。
|
||||||
|
*
|
||||||
|
* @param {string} name 參數名稱。
|
||||||
|
* @param {*} value 參數值。
|
||||||
|
*/
|
||||||
|
function requireHttpsUrl(name, value) {
|
||||||
|
let parsed;
|
||||||
|
try {
|
||||||
|
parsed = new URL(String(value));
|
||||||
|
} catch (error) {
|
||||||
|
fail(`${name} must be a valid absolute URL`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsed.protocol !== 'https:') {
|
||||||
|
fail(`${name} must use HTTPS`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 驗證 repository 是否為 `owner/repo` 格式且僅含安全字元,拒絕 `.`、`..` 等路徑片段。
|
||||||
|
*
|
||||||
|
* @param {string} name 參數名稱。
|
||||||
|
* @param {*} value 參數值。
|
||||||
|
*/
|
||||||
|
function requireRepository(name, value) {
|
||||||
|
const segments = String(value).split('/');
|
||||||
|
const isValidSegment = (segment) =>
|
||||||
|
/^[A-Za-z0-9_.-]+$/.test(segment) && segment !== '.' && segment !== '..';
|
||||||
|
|
||||||
|
if (segments.length !== 2 || !segments.every(isValidSegment)) {
|
||||||
|
fail(`${name} must be in owner/repo format`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -207,10 +245,10 @@ function requireInteger(name, value) {
|
|||||||
* 對指定 URL 發送 request,回傳狀態碼與 body。
|
* 對指定 URL 發送 request,回傳狀態碼與 body。
|
||||||
*
|
*
|
||||||
* @param {string} url 完整目標網址。
|
* @param {string} url 完整目標網址。
|
||||||
* @param {{ method?: string, headers?: Record<string, string> }} [options] request 設定。
|
* @param {{ method?: string, headers?: Record<string, string>, collectBody?: boolean }} [options] request 設定;`collectBody` 為 `false` 時丟棄回應內容、只保留狀態碼。
|
||||||
* @returns {Promise<{ statusCode: number, body: string }>} 回應狀態碼與內容。
|
* @returns {Promise<{ statusCode: number, body: string }>} 回應狀態碼與內容。
|
||||||
*/
|
*/
|
||||||
function request(url, { method = 'GET', headers = {} } = {}) {
|
function request(url, { method = 'GET', headers = {}, collectBody = true } = {}) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const target = new URL(url);
|
const target = new URL(url);
|
||||||
if (target.protocol !== 'https:') {
|
if (target.protocol !== 'https:') {
|
||||||
@@ -226,6 +264,16 @@ function request(url, { method = 'GET', headers = {} } = {}) {
|
|||||||
agent: keepAliveAgent,
|
agent: keepAliveAgent,
|
||||||
},
|
},
|
||||||
(res) => {
|
(res) => {
|
||||||
|
const statusCode = res.statusCode || 0;
|
||||||
|
|
||||||
|
if (!collectBody) {
|
||||||
|
res.resume();
|
||||||
|
res.on('end', () => {
|
||||||
|
resolve({ statusCode, body: '' });
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
|
|
||||||
res.setEncoding('utf8');
|
res.setEncoding('utf8');
|
||||||
@@ -234,7 +282,7 @@ function request(url, { method = 'GET', headers = {} } = {}) {
|
|||||||
});
|
});
|
||||||
res.on('end', () => {
|
res.on('end', () => {
|
||||||
resolve({
|
resolve({
|
||||||
statusCode: res.statusCode || 0,
|
statusCode,
|
||||||
body: chunks.join(''),
|
body: chunks.join(''),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -300,21 +348,49 @@ async function deleteResource(url, headers) {
|
|||||||
return request(url, {
|
return request(url, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers,
|
headers,
|
||||||
|
collectBody: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 以固定批次大小處理項目,降低逐筆等待造成的延遲。
|
* 以固定批次大小處理項目,降低逐筆等待造成的延遲;單筆例外不會中止同批其他項目。
|
||||||
*
|
*
|
||||||
* @param {any[]} items 要處理的項目。
|
* @param {any[]} items 要處理的項目。
|
||||||
* @param {number} batchSize 每批同時處理的數量。
|
* @param {number} batchSize 每批同時處理的數量。
|
||||||
* @param {(item: any) => Promise<void>} handler 單筆處理函式。
|
* @param {(item: any) => Promise<boolean>} handler 單筆處理函式,回傳該筆是否成功。
|
||||||
|
* @returns {Promise<PromiseSettledResult<boolean>[]>} 依原始順序排列的處理結果。
|
||||||
*/
|
*/
|
||||||
async function processInBatches(items, batchSize, handler) {
|
async function processInBatches(items, batchSize, handler) {
|
||||||
|
const results = [];
|
||||||
|
|
||||||
for (let index = 0; index < items.length; index += batchSize) {
|
for (let index = 0; index < items.length; index += batchSize) {
|
||||||
const batch = items.slice(index, index + batchSize);
|
const batch = items.slice(index, index + batchSize);
|
||||||
await Promise.all(batch.map((item) => handler(item)));
|
results.push(...(await Promise.allSettled(batch.map((item) => handler(item)))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 彙總批次結果;記錄被 reject 的例外,並回傳是否有任何一筆失敗。
|
||||||
|
*
|
||||||
|
* @param {PromiseSettledResult<boolean>[]} results 批次處理結果。
|
||||||
|
* @returns {boolean} 只要有任一筆失敗即回傳 `true`。
|
||||||
|
*/
|
||||||
|
function hasBatchFailure(results) {
|
||||||
|
let failed = false;
|
||||||
|
|
||||||
|
for (const result of results) {
|
||||||
|
if (result.status === 'rejected') {
|
||||||
|
failed = true;
|
||||||
|
const reason = result.reason;
|
||||||
|
fail(`批次處理發生例外: ${reason instanceof Error ? reason.message : String(reason)}`);
|
||||||
|
} else if (result.value === false) {
|
||||||
|
failed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -328,9 +404,11 @@ async function main() {
|
|||||||
|
|
||||||
section('參數檢查');
|
section('參數檢查');
|
||||||
requireValue('GITEA_SERVER_URL', GITEA_SERVER_URL, maskUrlForLog(GITEA_SERVER_URL));
|
requireValue('GITEA_SERVER_URL', GITEA_SERVER_URL, maskUrlForLog(GITEA_SERVER_URL));
|
||||||
|
requireHttpsUrl('GITEA_SERVER_URL', GITEA_SERVER_URL);
|
||||||
requireValue('GITEA_REPOSITORY', GITEA_REPOSITORY);
|
requireValue('GITEA_REPOSITORY', GITEA_REPOSITORY);
|
||||||
|
requireRepository('GITEA_REPOSITORY', GITEA_REPOSITORY);
|
||||||
requireValue('KEEP_COUNT', KEEP_COUNT);
|
requireValue('KEEP_COUNT', KEEP_COUNT);
|
||||||
requireInteger('KEEP_COUNT', KEEP_COUNT);
|
requirePositiveInteger('KEEP_COUNT', KEEP_COUNT);
|
||||||
|
|
||||||
const keepCount = Number(KEEP_COUNT);
|
const keepCount = Number(KEEP_COUNT);
|
||||||
const authHeaders = {};
|
const authHeaders = {};
|
||||||
@@ -348,7 +426,8 @@ async function main() {
|
|||||||
serverBase.hash = '';
|
serverBase.hash = '';
|
||||||
const serverBaseUrl = serverBase.toString().replace(/\/+$/, '');
|
const serverBaseUrl = serverBase.toString().replace(/\/+$/, '');
|
||||||
|
|
||||||
const releaseApiUrl = `${serverBaseUrl}/api/v1/repos/${GITEA_REPOSITORY}/releases`;
|
const repositoryPath = GITEA_REPOSITORY.split('/').map(encodeURIComponent).join('/');
|
||||||
|
const releaseApiUrl = `${serverBaseUrl}/api/v1/repos/${repositoryPath}/releases`;
|
||||||
|
|
||||||
section('取得成品資訊');
|
section('取得成品資訊');
|
||||||
info(`GET ${releaseApiUrl}`);
|
info(`GET ${releaseApiUrl}`);
|
||||||
@@ -370,21 +449,19 @@ async function main() {
|
|||||||
info(`RELEASE_COUNT=${releaseCount}`);
|
info(`RELEASE_COUNT=${releaseCount}`);
|
||||||
info(`KEEP_COUNT=${KEEP_COUNT}`);
|
info(`KEEP_COUNT=${KEEP_COUNT}`);
|
||||||
|
|
||||||
let hadFailure = false;
|
|
||||||
|
|
||||||
if (releaseCount <= keepCount) {
|
if (releaseCount <= keepCount) {
|
||||||
info('沒有需要清理的舊版本成品');
|
info('沒有需要清理的舊版本成品');
|
||||||
} else {
|
} else {
|
||||||
section('刪除舊版本成品');
|
section('刪除舊版本成品');
|
||||||
|
|
||||||
const releaseToDelete = releaseJson.slice(keepCount);
|
const releaseToDelete = releaseJson.slice(keepCount);
|
||||||
await processInBatches(releaseToDelete, DELETE_CONCURRENCY, async (releaseItem) => {
|
const releaseResults = await processInBatches(releaseToDelete, DELETE_CONCURRENCY, async (releaseItem) => {
|
||||||
const releaseId = releaseItem?.id;
|
const releaseId = releaseItem?.id;
|
||||||
if (!Number.isSafeInteger(releaseId) || releaseId <= 0) {
|
if (!Number.isSafeInteger(releaseId) || releaseId <= 0) {
|
||||||
warn(
|
warn(
|
||||||
`略過 id 不是正整數的成品: ${sanitizeLogText(releaseItem?.tag_name || '')} (${sanitizeLogText(releaseItem?.name || '')})`,
|
`略過 id 不是正整數的成品: ${sanitizeLogText(releaseItem?.tag_name || '')} (${sanitizeLogText(releaseItem?.name || '')})`,
|
||||||
);
|
);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const releaseTag = sanitizeLogText(releaseItem.tag_name || '');
|
const releaseTag = sanitizeLogText(releaseItem.tag_name || '');
|
||||||
@@ -395,15 +472,16 @@ async function main() {
|
|||||||
const { statusCode } = await deleteResource(deleteUrl, authHeaders);
|
const { statusCode } = await deleteResource(deleteUrl, authHeaders);
|
||||||
if (statusCode === 204) {
|
if (statusCode === 204) {
|
||||||
info(`成功刪除: ${releaseTag} (${releaseName})`);
|
info(`成功刪除: ${releaseTag} (${releaseName})`);
|
||||||
} else {
|
return true;
|
||||||
hadFailure = true;
|
|
||||||
fail(`刪除失敗: ${releaseTag} (${releaseName}), HTTP ${statusCode}`);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hadFailure) {
|
fail(`刪除失敗: ${releaseTag} (${releaseName}), HTTP ${statusCode}`);
|
||||||
throw new Error('至少有一筆 release 刪除失敗');
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (hasBatchFailure(releaseResults)) {
|
||||||
|
throw new Error('至少有一筆 release 刪除失敗');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
section('刪除未指定 release 的 tag');
|
section('刪除未指定 release 的 tag');
|
||||||
@@ -415,23 +493,23 @@ async function main() {
|
|||||||
.filter((tag) => !isEmptyOrNull(tag)),
|
.filter((tag) => !isEmptyOrNull(tag)),
|
||||||
);
|
);
|
||||||
|
|
||||||
const tagApiUrl = `${serverBaseUrl}/api/v1/repos/${GITEA_REPOSITORY}/tags`;
|
const tagApiUrl = `${serverBaseUrl}/api/v1/repos/${repositoryPath}/tags`;
|
||||||
info(`GET ${tagApiUrl}`);
|
info(`GET ${tagApiUrl}`);
|
||||||
|
|
||||||
const tagJson = await fetchAllPages(tagApiUrl, authHeaders);
|
const tagJson = await fetchAllPages(tagApiUrl, authHeaders);
|
||||||
info(`TAG_COUNT=${tagJson.length}`);
|
info(`TAG_COUNT=${tagJson.length}`);
|
||||||
|
|
||||||
await processInBatches(tagJson, DELETE_CONCURRENCY, async (tagItem) => {
|
const tagResults = await processInBatches(tagJson, DELETE_CONCURRENCY, async (tagItem) => {
|
||||||
const tagName = tagItem?.name;
|
const tagName = tagItem?.name;
|
||||||
if (isEmptyOrNull(tagName)) {
|
if (isEmptyOrNull(tagName)) {
|
||||||
warn('略過沒有名稱的 tag');
|
warn('略過沒有名稱的 tag');
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const safeTagName = sanitizeLogText(tagName);
|
const safeTagName = sanitizeLogText(tagName);
|
||||||
if (releaseTags.has(tagName)) {
|
if (releaseTags.has(tagName)) {
|
||||||
info(`保留指定 release 的 tag: ${safeTagName}`);
|
info(`保留指定 release 的 tag: ${safeTagName}`);
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteUrl = `${tagApiUrl}/${encodeURIComponent(tagName)}`;
|
const deleteUrl = `${tagApiUrl}/${encodeURIComponent(tagName)}`;
|
||||||
@@ -440,13 +518,14 @@ async function main() {
|
|||||||
const { statusCode } = await deleteResource(deleteUrl, authHeaders);
|
const { statusCode } = await deleteResource(deleteUrl, authHeaders);
|
||||||
if (statusCode === 204) {
|
if (statusCode === 204) {
|
||||||
info(`成功刪除未指定 release 的 tag: ${safeTagName}`);
|
info(`成功刪除未指定 release 的 tag: ${safeTagName}`);
|
||||||
} else {
|
return true;
|
||||||
hadFailure = true;
|
|
||||||
fail(`刪除 tag 失敗: ${safeTagName}, HTTP ${statusCode}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fail(`刪除 tag 失敗: ${safeTagName}, HTTP ${statusCode}`);
|
||||||
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (hadFailure) {
|
if (hasBatchFailure(tagResults)) {
|
||||||
throw new Error('至少有一筆 tag 刪除失敗');
|
throw new Error('至少有一筆 tag 刪除失敗');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user