Skip to content

Commit

Permalink
fix: catch remote connection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta committed Dec 7, 2022
1 parent e7d003b commit 56f7db1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions __tests__/helpers/get-git-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default function getGitRemoteMock(
}
// second call (used to grab default branch)
if (arr.length === 2 && arr[0] === 'show' && arr[1] === remote) {
if (remote === 'bad-remote') {
return Promise.reject(
new Error(`fatal: unable to access '${remoteUrl}': Could not resolve host: ${remoteUrl}`)
) as unknown as Response<string>;
}
if (!defaultBranch) return Promise.reject(new Error('Bad mock uh oh')) as unknown as Response<string>;
return Promise.resolve(`* remote origin
Fetch URL: ${remoteUrl}
Expand Down
6 changes: 6 additions & 0 deletions __tests__/lib/createGHA.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ describe('#createGHA', () => {
return expect(createGHA('success!', cmd, command.args, opts)).resolves.toBe('success!');
});

it('should not run if unable to connect to remote', () => {
git.remote = getGitRemoteMock('bad-remote', 'http://somebadurl.git');

return expect(createGHA('success!', cmd, command.args, opts)).resolves.toBe('success!');
});

it('should not run if user previously declined to set up GHA for current directory + pkg version', async () => {
const repoRoot = process.cwd();

Expand Down
6 changes: 5 additions & 1 deletion src/lib/createGHA/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ export async function getGitData() {
if (rawRemotes) {
const remote = rawRemotes.split('\n')[0];
debug(`[getGitData] remote result: ${remote}`);
const rawRemote = await git.remote(['show', remote]);
const rawRemote = await git.remote(['show', remote]).catch(e => {
debug(`[getGitData] error accessing remote: ${e.message}`);
return '';
});

debug(`[getGitData] rawRemote result: ${rawRemote}`);
// Extract head branch from git output
const rawHead = headLineRegEx.exec(rawRemote as string)?.[0];
Expand Down

0 comments on commit 56f7db1

Please sign in to comment.