mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-27 23:13:43 +00:00
feat: 支持“仅生成不上传”模式,优化定时同步与上传流程
- 新增云配置同步策略:定时任务在缺少 GitHub Token 时会跳过需要上传的条目,但仍执行“仅本地生成”(upload=false)的条目 - 同步逻辑支持 `upload: false`:配置仍会生成和更新时间,但不上传到 Gist,并会清理旧的远端 URL - 上传批次改为仅处理可上传条目;当无可上传条目时直接提示并跳过上传 - 同步日志和通知区分“上传成功/仅生成未上传/因缺少 token 跳过”,统计更准确 - 删除配置的远端删除条件调整为更合理:没有远端 URL 且仅本地运行的条目不会再尝试远端删除 - 新增/扩展测试覆盖了: - cron 同步策略判断 - 批量上传时跳过 `upload: false` - 单条同步时仅更新运行时间且不上传 - 无远端 URL 时删除行为
This commit is contained in:
8 files changed
+519
-42
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.23.15",
|
||||
"version": "2.23.16",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"packageManager": "pnpm@11.0.9",
|
||||
@@ -60,4 +60,4 @@
|
||||
"peggy": "^2.0.1",
|
||||
"prettier": "2.6.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,17 @@ import {
|
||||
COLLECTIONS_KEY,
|
||||
} from '@/constants';
|
||||
import $ from '@/core/app';
|
||||
import { produceArtifact, uploadArtifactBatches } from '@/restful/sync';
|
||||
import {
|
||||
markArtifactProducedWithoutUpload,
|
||||
produceArtifact,
|
||||
shouldUploadArtifact,
|
||||
uploadArtifactBatches,
|
||||
} from '@/restful/sync';
|
||||
import { findByName } from '@/utils/database';
|
||||
import { hasGistSyncCredentials } from '@/utils/gist';
|
||||
import {
|
||||
resolveCronArtifactSyncPolicy,
|
||||
shouldSkipCronArtifactWithoutUploadCredentials,
|
||||
} from '@/utils/artifact-sync-policy';
|
||||
|
||||
!(async function () {
|
||||
let arg;
|
||||
@@ -35,14 +43,12 @@ import { hasGistSyncCredentials } from '@/utils/gist';
|
||||
await produceArtifacts(col_names, 'collection');
|
||||
} else {
|
||||
const settings = $.read(SETTINGS_KEY);
|
||||
// if GitHub token is not configured
|
||||
if (!hasGistSyncCredentials(settings)) return;
|
||||
|
||||
const artifacts = $.read(ARTIFACTS_KEY);
|
||||
if (!artifacts || artifacts.length === 0) return;
|
||||
|
||||
const shouldSync = artifacts.some((artifact) => artifact.sync);
|
||||
if (shouldSync) await doSync(arg);
|
||||
const policy = resolveCronArtifactSyncPolicy({ artifacts, settings });
|
||||
|
||||
if (policy.shouldRun) await doSync(arg, policy);
|
||||
}
|
||||
})().finally(() => $.done());
|
||||
|
||||
@@ -91,7 +97,7 @@ async function produceArtifacts(names, type) {
|
||||
$.error(`produceArtifacts error: ${e.message ?? e}`);
|
||||
}
|
||||
}
|
||||
async function doSync(arg = {}) {
|
||||
async function doSync(arg = {}, { canUpload = true } = {}) {
|
||||
const syncSuccessNotify = isTruthyArgument(arg.sync_success_notify);
|
||||
console.log(
|
||||
`
|
||||
@@ -108,6 +114,8 @@ async function doSync(arg = {}) {
|
||||
try {
|
||||
const valid = [];
|
||||
const invalid = [];
|
||||
const producedWithoutUpload = [];
|
||||
const skippedWithoutUploadCredentials = [];
|
||||
const allSubs = $.read(SUBS_KEY);
|
||||
const allCols = $.read(COLLECTIONS_KEY);
|
||||
const subNames = [];
|
||||
@@ -115,6 +123,13 @@ async function doSync(arg = {}) {
|
||||
allArtifacts.map((artifact) => {
|
||||
if (artifact.sync && artifact.source) {
|
||||
enabledCount++;
|
||||
if (
|
||||
shouldSkipCronArtifactWithoutUploadCredentials(artifact, {
|
||||
canUpload,
|
||||
})
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if (artifact.type === 'subscription') {
|
||||
const subName = artifact.source;
|
||||
const sub = findByName(allSubs, subName);
|
||||
@@ -161,6 +176,16 @@ async function doSync(arg = {}) {
|
||||
allArtifacts.map(async (artifact) => {
|
||||
try {
|
||||
if (artifact.sync && artifact.source) {
|
||||
if (
|
||||
shouldSkipCronArtifactWithoutUploadCredentials(
|
||||
artifact,
|
||||
{ canUpload },
|
||||
)
|
||||
) {
|
||||
skippedWithoutUploadCredentials.push(artifact.name);
|
||||
return;
|
||||
}
|
||||
|
||||
$.info(`正在同步云配置:${artifact.name}...`);
|
||||
|
||||
const useMihomoExternal =
|
||||
@@ -186,11 +211,15 @@ async function doSync(arg = {}) {
|
||||
// if (!output || output.length === 0)
|
||||
// throw new Error('该配置的结果为空 不进行上传');
|
||||
|
||||
files[encodeURIComponent(artifact.name)] = {
|
||||
content: output,
|
||||
};
|
||||
|
||||
valid.push(artifact.name);
|
||||
if (shouldUploadArtifact(artifact)) {
|
||||
files[encodeURIComponent(artifact.name)] = {
|
||||
content: output,
|
||||
};
|
||||
valid.push(artifact.name);
|
||||
} else {
|
||||
markArtifactProducedWithoutUpload(artifact);
|
||||
producedWithoutUpload.push(artifact.name);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
$.error(
|
||||
@@ -203,10 +232,31 @@ async function doSync(arg = {}) {
|
||||
}),
|
||||
);
|
||||
|
||||
$.info(`${valid.length} 个同步配置生成成功: ${valid.join(', ')}`);
|
||||
const producedCount = valid.length + producedWithoutUpload.length;
|
||||
$.info(
|
||||
`${producedCount} 个同步配置生成成功: ${valid
|
||||
.concat(producedWithoutUpload)
|
||||
.join(', ')}`,
|
||||
);
|
||||
$.info(`${invalid.length} 个同步配置生成失败: ${invalid.join(', ')}`);
|
||||
if (producedWithoutUpload.length > 0) {
|
||||
$.info(
|
||||
`${
|
||||
producedWithoutUpload.length
|
||||
} 个同步配置仅生成未上传: ${producedWithoutUpload.join(', ')}`,
|
||||
);
|
||||
}
|
||||
if (skippedWithoutUploadCredentials.length > 0) {
|
||||
$.info(
|
||||
`${
|
||||
skippedWithoutUploadCredentials.length
|
||||
} 个同步配置因未设置 GitHub Token 已跳过上传: ${skippedWithoutUploadCredentials.join(
|
||||
', ',
|
||||
)}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (valid.length === 0) {
|
||||
if (producedCount === 0) {
|
||||
throw new Error(
|
||||
`同步配置 ${invalid.join(', ')} 生成失败 详情请查看日志`,
|
||||
);
|
||||
@@ -220,15 +270,27 @@ async function doSync(arg = {}) {
|
||||
});
|
||||
|
||||
$.write(allArtifacts, ARTIFACTS_KEY);
|
||||
$.info('上传配置成功');
|
||||
$.info('同步配置执行完成');
|
||||
|
||||
if (invalid.length > 0) {
|
||||
$.notify(
|
||||
'🌍 Sub-Store',
|
||||
`同步配置成功 ${uploaded.length} 个, 失败 ${invalid.length} 个, 详情请查看日志`,
|
||||
`同步配置成功 ${
|
||||
uploaded.length + producedWithoutUpload.length
|
||||
} 个, 失败 ${invalid.length} 个${
|
||||
skippedWithoutUploadCredentials.length
|
||||
? `, 跳过 ${skippedWithoutUploadCredentials.length} 个需上传配置`
|
||||
: ''
|
||||
}, 详情请查看日志`,
|
||||
);
|
||||
} else if (syncSuccessNotify) {
|
||||
$.notify('🌍 Sub-Store', '同步配置完成');
|
||||
$.notify(
|
||||
'🌍 Sub-Store',
|
||||
'同步配置完成',
|
||||
skippedWithoutUploadCredentials.length
|
||||
? `已跳过 ${skippedWithoutUploadCredentials.length} 个需上传配置(未设置 GitHub Token)`
|
||||
: undefined,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
$.notify('🌍 Sub-Store', '同步配置失败', `原因:${e.message ?? e}`);
|
||||
|
||||
@@ -269,7 +269,7 @@ async function deleteArtifactItem(name) {
|
||||
attempted: false,
|
||||
status: 'not_attempted',
|
||||
};
|
||||
if (artifact.updated) {
|
||||
if (artifact.url || (artifact.updated && artifact.upload !== false)) {
|
||||
const files = {};
|
||||
files[encodeURIComponent(artifact.name)] = {
|
||||
content: '',
|
||||
|
||||
+78
-21
@@ -755,16 +755,34 @@ function resolveArtifactUploadUrl(body, artifactName) {
|
||||
return new_url;
|
||||
}
|
||||
|
||||
function shouldUploadArtifact(artifact) {
|
||||
return artifact?.upload !== false;
|
||||
}
|
||||
|
||||
function markArtifactProducedWithoutUpload(artifact) {
|
||||
artifact.updated = new Date().getTime();
|
||||
delete artifact.url;
|
||||
}
|
||||
|
||||
async function uploadArtifactBatches({ allArtifacts, files, valid, invalid }) {
|
||||
const settings = $.read(SETTINGS_KEY) || {};
|
||||
const batchSize = normalizeArtifactSyncBatchSize(
|
||||
settings.artifactSyncBatchSize,
|
||||
);
|
||||
const batches = createArtifactUploadBatches(valid, batchSize);
|
||||
const uploadNames = valid.filter((name) => {
|
||||
const artifact = findByName(allArtifacts, name);
|
||||
return artifact && shouldUploadArtifact(artifact);
|
||||
});
|
||||
const batches = createArtifactUploadBatches(uploadNames, batchSize);
|
||||
const uploaded = [];
|
||||
|
||||
if (uploadNames.length === 0) {
|
||||
$.info('没有需要上传的同步配置');
|
||||
return uploaded;
|
||||
}
|
||||
|
||||
$.info(
|
||||
`准备分批上传同步配置: 共 ${valid.length} 个, 每批 ${batchSize} 个, 批次数 ${batches.length}`,
|
||||
`准备分批上传同步配置: 共 ${uploadNames.length} 个, 每批 ${batchSize} 个, 批次数 ${batches.length}`,
|
||||
);
|
||||
|
||||
for (let index = 0; index < batches.length; index++) {
|
||||
@@ -778,9 +796,9 @@ async function uploadArtifactBatches({ allArtifacts, files, valid, invalid }) {
|
||||
|
||||
try {
|
||||
$.info(
|
||||
`正在上传第 ${index + 1}/${batches.length} 批同步配置: ${batchNames.join(
|
||||
', ',
|
||||
)}`,
|
||||
`正在上传第 ${index + 1}/${
|
||||
batches.length
|
||||
} 批同步配置: ${batchNames.join(', ')}`,
|
||||
);
|
||||
const resp = await syncToGist(batchFiles);
|
||||
const body = JSON.parse(resp.body);
|
||||
@@ -810,9 +828,11 @@ async function uploadArtifactBatches({ allArtifacts, files, valid, invalid }) {
|
||||
}
|
||||
} catch (e) {
|
||||
$.error(
|
||||
`第 ${index + 1}/${batches.length} 批同步配置上传失败: ${batchNames.join(
|
||||
', ',
|
||||
)}, 原因: ${e.message ?? e}`,
|
||||
`第 ${index + 1}/${
|
||||
batches.length
|
||||
} 批同步配置上传失败: ${batchNames.join(', ')}, 原因: ${
|
||||
e.message ?? e
|
||||
}`,
|
||||
);
|
||||
invalid.push(...batchNames);
|
||||
}
|
||||
@@ -829,6 +849,7 @@ async function syncArtifacts() {
|
||||
try {
|
||||
const valid = [];
|
||||
const invalid = [];
|
||||
const producedWithoutUpload = [];
|
||||
const allSubs = $.read(SUBS_KEY);
|
||||
const allCols = $.read(COLLECTIONS_KEY);
|
||||
const subNames = [];
|
||||
@@ -909,11 +930,15 @@ async function syncArtifacts() {
|
||||
// if (!output || output.length === 0)
|
||||
// throw new Error('该配置的结果为空 不进行上传');
|
||||
|
||||
files[encodeURIComponent(artifact.name)] = {
|
||||
content: output,
|
||||
};
|
||||
|
||||
valid.push(artifact.name);
|
||||
if (shouldUploadArtifact(artifact)) {
|
||||
files[encodeURIComponent(artifact.name)] = {
|
||||
content: output,
|
||||
};
|
||||
valid.push(artifact.name);
|
||||
} else {
|
||||
markArtifactProducedWithoutUpload(artifact);
|
||||
producedWithoutUpload.push(artifact.name);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
$.error(
|
||||
@@ -926,10 +951,22 @@ async function syncArtifacts() {
|
||||
}),
|
||||
);
|
||||
|
||||
$.info(`${valid.length} 个同步配置生成成功: ${valid.join(', ')}`);
|
||||
const producedCount = valid.length + producedWithoutUpload.length;
|
||||
$.info(
|
||||
`${producedCount} 个同步配置生成成功: ${valid
|
||||
.concat(producedWithoutUpload)
|
||||
.join(', ')}`,
|
||||
);
|
||||
$.info(`${invalid.length} 个同步配置生成失败: ${invalid.join(', ')}`);
|
||||
if (producedWithoutUpload.length > 0) {
|
||||
$.info(
|
||||
`${
|
||||
producedWithoutUpload.length
|
||||
} 个同步配置仅生成未上传: ${producedWithoutUpload.join(', ')}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (valid.length === 0) {
|
||||
if (producedCount === 0) {
|
||||
throw new Error(
|
||||
`同步配置 ${invalid.join(', ')} 生成失败 详情请查看日志`,
|
||||
);
|
||||
@@ -943,14 +980,20 @@ async function syncArtifacts() {
|
||||
});
|
||||
|
||||
$.write(allArtifacts, ARTIFACTS_KEY);
|
||||
$.info('上传配置成功');
|
||||
$.info('同步配置执行完成');
|
||||
|
||||
if (invalid.length > 0) {
|
||||
throw new Error(
|
||||
`同步配置成功 ${uploaded.length} 个, 失败 ${invalid.length} 个, 详情请查看日志`,
|
||||
`同步配置成功 ${
|
||||
uploaded.length + producedWithoutUpload.length
|
||||
} 个, 失败 ${invalid.length} 个, 详情请查看日志`,
|
||||
);
|
||||
} else {
|
||||
$.info(`同步配置成功 ${uploaded.length} 个`);
|
||||
$.info(
|
||||
`同步配置成功 ${
|
||||
uploaded.length + producedWithoutUpload.length
|
||||
} 个`,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
$.error(`同步配置失败,原因:${e.message ?? e}`);
|
||||
@@ -1024,6 +1067,16 @@ async function syncArtifact(req, res) {
|
||||
},
|
||||
});
|
||||
|
||||
// if (!output || output.length === 0)
|
||||
// throw new Error('该配置的结果为空 不进行上传');
|
||||
if (!shouldUploadArtifact(artifact)) {
|
||||
$.info(`配置 ${artifact.name} 已关闭上传, 仅更新执行时间`);
|
||||
markArtifactProducedWithoutUpload(artifact);
|
||||
$.write(allArtifacts, ARTIFACTS_KEY);
|
||||
success(res, artifact);
|
||||
return;
|
||||
}
|
||||
|
||||
$.info(
|
||||
`正在上传配置:${artifact.name}\n>>>${JSON.stringify(
|
||||
artifact,
|
||||
@@ -1031,8 +1084,6 @@ async function syncArtifact(req, res) {
|
||||
2,
|
||||
)}`,
|
||||
);
|
||||
// if (!output || output.length === 0)
|
||||
// throw new Error('该配置的结果为空 不进行上传');
|
||||
const resp = await syncToGist({
|
||||
[encodeURIComponent(artifact.name)]: {
|
||||
content: output,
|
||||
@@ -1059,4 +1110,10 @@ async function syncArtifact(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
export { produceArtifact, syncArtifacts, uploadArtifactBatches };
|
||||
export {
|
||||
markArtifactProducedWithoutUpload,
|
||||
produceArtifact,
|
||||
shouldUploadArtifact,
|
||||
syncArtifacts,
|
||||
uploadArtifactBatches,
|
||||
};
|
||||
@@ -0,0 +1,180 @@
|
||||
import { expect } from 'chai';
|
||||
import { after, before, beforeEach, describe, it } from 'mocha';
|
||||
|
||||
import { ARTIFACTS_KEY, FILES_KEY, SETTINGS_KEY } from '@/constants';
|
||||
|
||||
let $;
|
||||
let registerArtifactRoutes;
|
||||
let registerSyncRoutes;
|
||||
let originalError;
|
||||
let originalInfo;
|
||||
let originalRead;
|
||||
let originalWrite;
|
||||
let state;
|
||||
|
||||
function createRouteApp() {
|
||||
const handlers = new Map();
|
||||
const methods = ['get', 'post', 'put', 'patch', 'delete'];
|
||||
const app = {
|
||||
handlers,
|
||||
route(pattern) {
|
||||
const chain = {};
|
||||
methods.forEach((method) => {
|
||||
chain[method] = (handler) => {
|
||||
handlers.set(`${method.toUpperCase()} ${pattern}`, handler);
|
||||
return chain;
|
||||
};
|
||||
});
|
||||
return chain;
|
||||
},
|
||||
};
|
||||
|
||||
methods.forEach((method) => {
|
||||
app[method] = (pattern, handler) => {
|
||||
handlers.set(`${method.toUpperCase()} ${pattern}`, handler);
|
||||
return app;
|
||||
};
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
function getHandler(pattern) {
|
||||
const app = createRouteApp();
|
||||
registerSyncRoutes(app);
|
||||
return app.handlers.get(`GET ${pattern}`);
|
||||
}
|
||||
|
||||
function createResponse(routePath) {
|
||||
return {
|
||||
req: {
|
||||
route: {
|
||||
path: routePath,
|
||||
},
|
||||
},
|
||||
body: null,
|
||||
statusCode: 200,
|
||||
json(payload) {
|
||||
this.body = payload;
|
||||
return this;
|
||||
},
|
||||
status(code) {
|
||||
this.statusCode = code;
|
||||
return this;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function requestSyncArtifact(name) {
|
||||
const handler = getHandler('/api/sync/artifact/:name');
|
||||
const res = createResponse('/api/sync/artifact/:name');
|
||||
|
||||
await handler(
|
||||
{
|
||||
body: {},
|
||||
params: { name },
|
||||
query: {},
|
||||
},
|
||||
res,
|
||||
);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
async function requestDeleteArtifact(name) {
|
||||
const app = createRouteApp();
|
||||
registerArtifactRoutes(app);
|
||||
const handler = app.handlers.get('DELETE /api/artifact/:name');
|
||||
const res = createResponse('/api/artifact/:name');
|
||||
|
||||
await handler(
|
||||
{
|
||||
params: { name },
|
||||
query: {},
|
||||
},
|
||||
res,
|
||||
);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
describe('sync routes', function () {
|
||||
before(async function () {
|
||||
({ default: $ } = require('@/core/app'));
|
||||
({ default: registerArtifactRoutes } = require('@/restful/artifacts'));
|
||||
({ default: registerSyncRoutes } = require('@/restful/sync'));
|
||||
|
||||
originalRead = $.read.bind($);
|
||||
originalWrite = $.write.bind($);
|
||||
originalInfo = $.info.bind($);
|
||||
originalError = $.error.bind($);
|
||||
});
|
||||
|
||||
after(function () {
|
||||
if ($) {
|
||||
$.read = originalRead;
|
||||
$.write = originalWrite;
|
||||
$.info = originalInfo;
|
||||
$.error = originalError;
|
||||
}
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
state = {
|
||||
[SETTINGS_KEY]: {},
|
||||
[ARTIFACTS_KEY]: [
|
||||
{
|
||||
name: 'local-artifact',
|
||||
type: 'file',
|
||||
source: 'local-file',
|
||||
sync: true,
|
||||
upload: false,
|
||||
updated: 1711111111111,
|
||||
url: 'https://gist.example.com/old',
|
||||
},
|
||||
],
|
||||
[FILES_KEY]: [
|
||||
{
|
||||
name: 'local-file',
|
||||
source: 'local',
|
||||
content: 'local content',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
$.read = (key) => state[key] || [];
|
||||
$.write = (data, key) => {
|
||||
state[key] = data;
|
||||
return true;
|
||||
};
|
||||
$.info = () => {};
|
||||
$.error = () => {};
|
||||
});
|
||||
|
||||
it('updates run time without requiring a Gist URL when upload is disabled', async function () {
|
||||
const startedAt = new Date().getTime();
|
||||
|
||||
const res = await requestSyncArtifact('local-artifact');
|
||||
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.body.status).to.equal('success');
|
||||
expect(res.body.data.updated).to.be.at.least(startedAt);
|
||||
expect(res.body.data).to.not.have.property('url');
|
||||
expect(state[ARTIFACTS_KEY][0].updated).to.be.at.least(startedAt);
|
||||
expect(state[ARTIFACTS_KEY][0]).to.not.have.property('url');
|
||||
});
|
||||
|
||||
it('does not try to delete a remote file when only run time exists', async function () {
|
||||
delete state[ARTIFACTS_KEY][0].url;
|
||||
|
||||
const res = await requestDeleteArtifact('local-artifact');
|
||||
|
||||
expect(res.statusCode).to.equal(200);
|
||||
expect(res.body.status).to.equal('success');
|
||||
expect(res.body.data.remote).to.deep.equal({
|
||||
attempted: false,
|
||||
status: 'not_attempted',
|
||||
});
|
||||
expect(state[ARTIFACTS_KEY]).to.deep.equal([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { expect } from 'chai';
|
||||
import { describe, it } from 'mocha';
|
||||
|
||||
import {
|
||||
resolveCronArtifactSyncPolicy,
|
||||
shouldSkipCronArtifactWithoutUploadCredentials,
|
||||
} from '@/utils/artifact-sync-policy';
|
||||
|
||||
describe('cron sync artifact policy', function () {
|
||||
it('keeps upload-only schedules quiet without Gist credentials', function () {
|
||||
const policy = resolveCronArtifactSyncPolicy({
|
||||
settings: {},
|
||||
artifacts: [{ name: 'upload-me', sync: true }],
|
||||
});
|
||||
|
||||
expect(policy).to.deep.equal({
|
||||
canUpload: false,
|
||||
shouldRun: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('runs only no-upload artifacts when mixed schedules have no Gist credentials', function () {
|
||||
const policy = resolveCronArtifactSyncPolicy({
|
||||
settings: {},
|
||||
artifacts: [
|
||||
{ name: 'cache-refresh', sync: true, upload: false },
|
||||
{ name: 'gist-upload', sync: true },
|
||||
],
|
||||
});
|
||||
|
||||
expect(policy).to.deep.equal({
|
||||
canUpload: false,
|
||||
shouldRun: true,
|
||||
});
|
||||
expect(
|
||||
shouldSkipCronArtifactWithoutUploadCredentials(
|
||||
{ name: 'cache-refresh', upload: false },
|
||||
policy,
|
||||
),
|
||||
).to.equal(false);
|
||||
expect(
|
||||
shouldSkipCronArtifactWithoutUploadCredentials(
|
||||
{ name: 'gist-upload' },
|
||||
policy,
|
||||
),
|
||||
).to.equal(true);
|
||||
});
|
||||
|
||||
it('allows every synced artifact when Gist credentials exist', function () {
|
||||
const policy = resolveCronArtifactSyncPolicy({
|
||||
settings: { gistToken: 'token' },
|
||||
artifacts: [
|
||||
{ name: 'cache-refresh', sync: true, upload: false },
|
||||
{ name: 'gist-upload', sync: true },
|
||||
],
|
||||
});
|
||||
|
||||
expect(policy).to.deep.equal({
|
||||
canUpload: true,
|
||||
shouldRun: true,
|
||||
});
|
||||
expect(
|
||||
shouldSkipCronArtifactWithoutUploadCredentials(
|
||||
{ name: 'gist-upload' },
|
||||
policy,
|
||||
),
|
||||
).to.equal(false);
|
||||
});
|
||||
});
|
||||
@@ -333,4 +333,79 @@ describe('Gist GitHub API URL', function () {
|
||||
Gist.prototype.upload = originalUpload;
|
||||
}
|
||||
});
|
||||
|
||||
it('skips artifact batches for items that disabled upload', async function () {
|
||||
const originalRead = $.read.bind($);
|
||||
const originalWrite = $.write.bind($);
|
||||
const originalInfo = $.info.bind($);
|
||||
const originalLog = $.log.bind($);
|
||||
const originalError = $.error.bind($);
|
||||
const originalUpload = Gist.prototype.upload;
|
||||
let capturedFiles;
|
||||
|
||||
$.read = (key) => {
|
||||
if (key === SETTINGS_KEY) {
|
||||
return {
|
||||
gistToken: 'token',
|
||||
artifactSyncBatchSize: 10,
|
||||
};
|
||||
}
|
||||
return originalRead(key);
|
||||
};
|
||||
$.write = (data, key) => {
|
||||
if (key === SETTINGS_KEY) {
|
||||
return true;
|
||||
}
|
||||
return originalWrite(data, key);
|
||||
};
|
||||
$.info = () => {};
|
||||
$.log = () => {};
|
||||
$.error = () => {};
|
||||
Gist.prototype.upload = async function (files) {
|
||||
capturedFiles = files;
|
||||
return {
|
||||
body: JSON.stringify({
|
||||
html_url: 'https://gist.example.com/sub-store',
|
||||
files: {
|
||||
b: {
|
||||
raw_url: 'https://gist.example.com/raw/hash/b',
|
||||
},
|
||||
},
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
const allArtifacts = [
|
||||
{ name: 'a', sync: true, source: 'sub-a', upload: false },
|
||||
{ name: 'b', sync: true, source: 'sub-b' },
|
||||
];
|
||||
const invalid = [];
|
||||
|
||||
try {
|
||||
const uploaded = await uploadArtifactBatches({
|
||||
allArtifacts,
|
||||
files: {
|
||||
a: { content: 'A' },
|
||||
b: { content: 'B' },
|
||||
},
|
||||
valid: ['a', 'b'],
|
||||
invalid,
|
||||
});
|
||||
|
||||
expect(Object.keys(capturedFiles)).to.deep.equal(['b']);
|
||||
expect(uploaded).to.deep.equal(['b']);
|
||||
expect(invalid).to.deep.equal([]);
|
||||
expect(allArtifacts[0].url).to.equal(undefined);
|
||||
expect(allArtifacts[1].url).to.equal(
|
||||
'https://gist.example.com/raw/b',
|
||||
);
|
||||
} finally {
|
||||
$.read = originalRead;
|
||||
$.write = originalWrite;
|
||||
$.info = originalInfo;
|
||||
$.log = originalLog;
|
||||
$.error = originalError;
|
||||
Gist.prototype.upload = originalUpload;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
import { hasGistSyncCredentials } from '@/utils/gist';
|
||||
|
||||
function shouldUploadArtifactForCron(artifact) {
|
||||
return artifact?.upload !== false;
|
||||
}
|
||||
|
||||
function resolveCronArtifactSyncPolicy({ artifacts = [], settings = {} } = {}) {
|
||||
const hasUploadCredentials = hasGistSyncCredentials(settings);
|
||||
const shouldSync = artifacts.some((artifact) => artifact.sync);
|
||||
const shouldProduceWithUpload = artifacts.some(
|
||||
(artifact) => artifact.sync && shouldUploadArtifactForCron(artifact),
|
||||
);
|
||||
const shouldProduceWithoutUpload = artifacts.some(
|
||||
(artifact) => artifact.sync && !shouldUploadArtifactForCron(artifact),
|
||||
);
|
||||
|
||||
return {
|
||||
canUpload: hasUploadCredentials || !shouldProduceWithUpload,
|
||||
shouldRun:
|
||||
shouldSync && (hasUploadCredentials || shouldProduceWithoutUpload),
|
||||
};
|
||||
}
|
||||
|
||||
function shouldSkipCronArtifactWithoutUploadCredentials(
|
||||
artifact,
|
||||
{ canUpload },
|
||||
) {
|
||||
return !canUpload && shouldUploadArtifactForCron(artifact);
|
||||
}
|
||||
|
||||
export {
|
||||
resolveCronArtifactSyncPolicy,
|
||||
shouldSkipCronArtifactWithoutUploadCredentials,
|
||||
};
|
||||
Reference in new issue
Block a user