Skip to content

Commit

Permalink
Merge pull request #1632 from intuit/verify-timeout
Browse files Browse the repository at this point in the history
add timeout when verifying auth to remote
  • Loading branch information
hipstersmoothie authored Nov 4, 2020
2 parents 1b66860 + a962ba2 commit a4197d3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@
"html",
"lcov",
"text"
]
],
"globals": {
"ts-jest": {
"isolatedModules": true
}
}
},
"ignite": {
"repo": "intuit/auto",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { runCli, execute } from "../src/run";
process.env.GH_TOKEN = "XXXX";

jest.mock("@octokit/rest");
jest.mock("../../core/src/utils/verify-auth");

test("throws error for unknown args", async () => {
process.exit = jest.fn() as any;
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/utils/__tests__/verify-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe("verify-auth", () => {
test("should handle error", async () => {
spawn.mockImplementationOnce(() => ({
stderr: { on: () => {} },
kill: () => {},
on: () => {
throw new Error();
},
Expand All @@ -18,6 +19,7 @@ describe("verify-auth", () => {
test("should verify auth when we can push to remote", async () => {
spawn.mockImplementationOnce(() => ({
stderr: { on: () => {} },
kill: () => {},
on: (_: string, cb: () => void) => cb(),
}));
expect(await verifyAuth("origin", "master")).toBe(true);
Expand All @@ -29,6 +31,7 @@ describe("verify-auth", () => {
on: (_: string, cb: (data: string) => void) =>
cb("fatal: could not read Username"),
},
kill: () => {},
on: (_: string, cb: () => void) => cb(),
}));
expect(await verifyAuth("bad", "master")).toBe(false);
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/utils/verify-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { spawn } from "child_process";
*/
export default function verifyAuth(remote: string, branch: string) {
return new Promise<boolean>((resolve) => {
let timeout: NodeJS.Timeout | null = null;

/** Clear the timeout timeout */
const clear = () => {
if (timeout) {
clearTimeout(timeout);
}
};

try {
const child = spawn(
`git push --dry-run --no-verify ${remote} HEAD:${branch} -q`,
Expand All @@ -16,20 +25,27 @@ export default function verifyAuth(remote: string, branch: string) {
}
);

timeout = setTimeout(() => {
child.kill(0);
resolve(false);
}, 5 * 1000);

let err = "";

child.stderr.on("data", (data) => {
err += data.toString();
});

child.on("exit", () => {
clear();
resolve(
!err.startsWith("fatal: could not read Username") &&
!err.startsWith("ssh_askpass") &&
!err.includes("fatal")
);
});
} catch (error) {
clear();
resolve(false);
}
});
Expand Down

0 comments on commit a4197d3

Please sign in to comment.