feat: 前置驗證納入 git push 認證檢查 #11

Merged
jiantw83 merged 12 commits from develop into master 2026-06-16 06:23:52 +00:00
2 changed files with 26 additions and 3 deletions
Showing only changes of commit 7339145641 - Show all commits
+14 -3
View File
@@ -62,11 +62,22 @@ function withAskpass(workspace, fn) {
const askpassScript = path.join(workspace, '.git-askpass.sh'); const askpassScript = path.join(workspace, '.git-askpass.sh');
fs.writeFileSync(askpassScript, '#!/bin/sh\necho "$GIT_TOKEN"\n', { mode: 0o700 }); fs.writeFileSync(askpassScript, '#!/bin/sh\necho "$GIT_TOKEN"\n', { mode: 0o700 });
const credEnv = { ...process.env, GIT_ASKPASS: askpassScript, GIT_USERNAME: 'x-token', GIT_TOKEN: GITEA_TOKEN }; const credEnv = { ...process.env, GIT_ASKPASS: askpassScript, GIT_USERNAME: 'x-token', GIT_TOKEN: GITEA_TOKEN };
const cleanup = () => { try { fs.unlinkSync(askpassScript); } catch {} };
let result;
try { try {
return fn(credEnv); result = fn(credEnv);
} finally { } catch (e) {
try { fs.unlinkSync(askpassScript); } catch {} cleanup();
throw e;
} }
// Defer cleanup until an async callback settles, otherwise the askpass script
// is deleted at the first `await` and later network ops (e.g. git push) fail
// with "cannot exec .git-askpass.sh". Sync callbacks clean up immediately.
if (result && typeof result.then === 'function') {
return result.finally(cleanup);
}
cleanup();
return result;
} }
function readGitOutput(run, args, cwd, env) { function readGitOutput(run, args, cwd, env) {
+12
View File
@@ -93,6 +93,18 @@ describe('commitAndPush', () => {
} }
}); });
it('keeps the askpass script present while the network push runs', async () => {
let askpassExistsAtPush = null;
const spawn = makeSpawn({
push: (_args, opts) => {
askpassExistsAtPush = !!(opts?.env?.GIT_ASKPASS && fs.existsSync(opts.env.GIT_ASKPASS));
return { status: 0, stdout: '', stderr: '', error: null };
},
});
await commitAndPush(workspace, path.join(workspace, 'repo'), spawn, sourceRoot);
assert.equal(askpassExistsAtPush, true, 'askpass script must still exist when git push runs');
});
it('cleans up askpass script after successful run', async () => { it('cleans up askpass script after successful run', async () => {
await commitAndPush(workspace, path.join(workspace, 'repo'), makeSpawn(), sourceRoot); await commitAndPush(workspace, path.join(workspace, 'repo'), makeSpawn(), sourceRoot);
const leftover = fs.readdirSync(workspace).filter(f => f.endsWith('.git-askpass.sh')); const leftover = fs.readdirSync(workspace).filter(f => f.endsWith('.git-askpass.sh'));