Skip to content

Commit

Permalink
fix config fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfoyle committed Apr 3, 2024
1 parent e89824c commit 08fda1a
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions scripts/components/git_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,24 +206,36 @@ class GitClient {
if (this.isConfigured) {
return;
}
const { stdout: originalEmail } = await $`git config user.email`;
const { stdout: originalName } = await $`git config user.name`;
const { stdout: originalAutoSetupRemote } =
await $`git config push.autoSetupRemote`;

const userEmailKey = 'user.email';
const userNameKey = 'user.name';
const autoSetupRemoteKey = 'push.autoSetupRemote';

const originalEmail = await this.getConfigSafe(userEmailKey);
const originalName = await this.getConfigSafe(userNameKey);
const originalAutoSetupRemote = await this.getConfigSafe(
autoSetupRemoteKey
);

// eslint-disable-next-line spellcheck/spell-checker
await $`git config user.email "github-actions[bot]@users.noreply.github.com"`;
await $`git config user.name "github-actions[bot]"`;
await $`git config ${userEmailKey} "github-actions[bot]@users.noreply.github.com"`;
await $`git config ${userNameKey} "github-actions[bot]"`;

await $`git config --unset-all push.autoSetupRemote`;
await $`git config push.autoSetupRemote true`;
await $`git config --unset-all ${autoSetupRemoteKey}`;
await $`git config ${autoSetupRemoteKey} true`;

this.registerCleanup(async () => {
// reset config on exit
await $`git config user.email ${originalEmail}`;
await $`git config user.name ${originalName}`;
await $`git config --unset-all push.autoSetupRemote`;
await $`git config push.autoSetupRemote ${originalAutoSetupRemote}`;
if (originalEmail) {
await $`git config user.email ${originalEmail}`;
}
if (originalName) {
await $`git config ${userNameKey} ${originalName}`;
}
if (originalAutoSetupRemote) {
await $`git config --unset-all ${autoSetupRemoteKey}`;
await $`git config ${autoSetupRemoteKey} ${originalAutoSetupRemote}`;
}
});
this.isConfigured = true;
};
Expand All @@ -238,6 +250,15 @@ class GitClient {
process.once('uncaughtException', cleanupCallbackTypeAssertion);
process.once('unhandledRejection', cleanupCallbackTypeAssertion);
};

private getConfigSafe = async (configKey: string) => {
try {
const { stdout } = await $`git config ${configKey}`;
return stdout;
} catch {
return undefined;
}
};
}

/**
Expand Down

0 comments on commit 08fda1a

Please sign in to comment.