From e6552378ccfe562f55ea49cd8b321e28179d789f Mon Sep 17 00:00:00 2001 From: WuYongJie <549727955@qq.com> Date: Thu, 3 Sep 2026 18:10:19 +0800 Subject: [PATCH] fix: sync exact progress commits safely --- scripts/kaifa-progress-sync.mjs | 46 ++++++++++++++++++++-- tests/scripts/kaifa-progress-sync.test.mjs | 3 ++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/scripts/kaifa-progress-sync.mjs b/scripts/kaifa-progress-sync.mjs index 124ef2c3..9612e150 100644 --- a/scripts/kaifa-progress-sync.mjs +++ b/scripts/kaifa-progress-sync.mjs @@ -74,6 +74,11 @@ export const FEATURE_RULES = [ subject: /快递|报价|签署|协议|E\s*签|esign|cooperation|courier|signing/i, paths: /(?:Courier|Esign|agreement-signing|quote-management)/i, }, + { + key: 'dingtalk-login', priority: 110, moduleName: '生产发布与团队协作', featureName: '钉钉账号登录与绑定', + subject: /(?:(?:DingTalk|钉钉).*(?:login|callback|登录|回调)|(?:login|callback|登录|回调).*(?:DingTalk|钉钉))/i, + paths: /(?:Auth\/DingTalkLogin|DingTalkIdentity|dingtalk.*callback|config\/dingtalk\.php)/i, + }, { key: 'ding-payables', priority: 100, moduleName: '快递与审批', featureName: '钉钉应付流程与状态同步', subject: /钉钉|应付.*流程|ding payable/i, @@ -195,6 +200,16 @@ function git(repoRoot, args) { return result.stdout.trim() } +function isAncestor(repoRoot, ancestor, descendant) { + const result = spawnSync('git', ['merge-base', '--is-ancestor', ancestor, descendant], { + cwd: repoRoot, + encoding: 'utf8', + }) + if (result.status === 0) return true + if (result.status === 1) return false + fail(`GIT_FAILED:merge-base:${result.stderr.trim()}`) +} + function getChangedFiles(repoRoot, sha) { const statuses = git(repoRoot, ['diff-tree', '--root', '--no-commit-id', '--name-status', '-r', '-M', sha]).split('\n').filter(Boolean) const nums = git(repoRoot, ['diff-tree', '--root', '--no-commit-id', '--numstat', '-r', '-M', sha]).split('\n').filter(Boolean) @@ -315,6 +330,13 @@ export async function main(argv = process.argv.slice(2)) { const dryRun = flags.has('dry-run') const ref = values.ref || 'origin/main' const since = values.since || DEFAULT_SINCE + const requestedSha = values.sha || null + const manualModule = values.module || null + const manualFeature = values.feature || null + if ((manualModule || manualFeature) && (!requestedSha || !manualModule || !manualFeature)) { + fail('MANUAL_FEATURE_REQUIRES_SHA_MODULE_AND_FEATURE') + } + if (requestedSha && !/^[0-9a-f]{40}$/i.test(requestedSha)) fail('INVALID_SHA') const limit = values.limit ? Number(values.limit) : Number.POSITIVE_INFINITY if (!(limit > 0)) fail('INVALID_LIMIT') const remoteUrl = git(repoRoot, ['config', '--get', 'remote.origin.url']) @@ -328,7 +350,10 @@ export async function main(argv = process.argv.slice(2)) { }) if (project.department !== env.DEPARTMENT) fail('PROJECT_DEPARTMENT_MISMATCH') const state = readState(statePath) - const shas = git(repoRoot, ['log', ref, `--since=${since}`, '--no-merges', '--reverse', '--format=%H']).split('\n').filter(Boolean) + const shas = requestedSha + ? [git(repoRoot, ['rev-parse', requestedSha])] + : git(repoRoot, ['log', ref, `--since=${since}`, '--no-merges', '--reverse', '--format=%H']).split('\n').filter(Boolean) + if (requestedSha && !isAncestor(repoRoot, shas[0], ref)) fail('SHA_NOT_IN_REF') const excluded = {} const excludedCommits = [] const excludedAuthors = {} @@ -346,7 +371,15 @@ export async function main(argv = process.argv.slice(2)) { excludedAuthors[author] = (excludedAuthors[author] || 0) + 1 continue } - const classification = classifyCommit(commit.message, commit.changed_files.map((file) => file.path)) + const classification = manualModule + ? { + rule: { + key: `manual:${manualModule}:${manualFeature}`, + moduleName: manualModule, + featureName: manualFeature, + }, + } + : classifyCommit(commit.message, commit.changed_files.map((file) => file.path)) if (!classification.rule) { excluded[classification.reason] = (excluded[classification.reason] || 0) + 1 excludedCommits.push({ sha: commit.sha, title: commit.message, reason: classification.reason }) @@ -368,7 +401,14 @@ export async function main(argv = process.argv.slice(2)) { return map }, {})) if (dryRun) { - console.log(JSON.stringify({ mode: 'dry-run', project: project.projectName, ref, since, status, candidates: candidates.length, excluded, excludedAuthors, excludedCommits, features: featureSummary, authors: authorSummary }, null, 2)) + const commits = candidates.map(({ commit, rule }) => ({ + sha: commit.sha, + title: commit.message, + author: `${commit.author_name} <${commit.author_email}>`, + module: rule.moduleName, + feature: rule.featureName, + })) + console.log(JSON.stringify({ mode: 'dry-run', project: project.projectName, ref, since, status, candidates: candidates.length, excluded, excludedAuthors, excludedCommits, features: featureSummary, authors: authorSummary, commits }, null, 2)) return } if (!candidates.length) { diff --git a/tests/scripts/kaifa-progress-sync.test.mjs b/tests/scripts/kaifa-progress-sync.test.mjs index 8a3a5dc0..676f8697 100644 --- a/tests/scripts/kaifa-progress-sync.test.mjs +++ b/tests/scripts/kaifa-progress-sync.test.mjs @@ -58,6 +58,9 @@ test('assigns one primary feature and stops on ambiguous feature matches', () => const esign = classifyCommit('feat: add callback-free eSign status polling', ['app/Console/Commands/PollEsignSignatures.php']).rule assert.equal(esign.key, 'courier-esign') assert.equal(esign.featureName, 'E 签宝无回调状态轮询与归档') + const dingtalkLogin = classifyCommit('feat: add DingTalk account login', ['app/Http/Controllers/Auth/DingTalkLogin.php']).rule + assert.equal(dingtalkLogin.key, 'dingtalk-login') + assert.equal(dingtalkLogin.featureName, '钉钉账号登录与绑定') assert.equal( classifyCommit('feat: align taobao reconciliation headers', ['resources/views/store_receivables/zhongcai_reconciliations/partials/embedded-table.blade.php']).rule.key, 'zhongcai-reconciliation',