Skip to content

Commit c0b70cd

Browse files
committed
gh auth status: parse logged in variants
1 parent 9786793 commit c0b70cd

4 files changed

Lines changed: 65 additions & 18 deletions

File tree

src/app/DependencyCheck.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import { Url } from "~/app/Url";
1010
import { cli } from "~/core/cli";
1111
import { colors } from "~/core/colors";
1212
import { is_command_available } from "~/core/is_command_available";
13-
import { match_group } from "~/core/match_group";
1413
import { semver_compare } from "~/core/semver_compare";
14+
import * as gh from "~/github/gh";
1515

1616
type Props = {
1717
children: React.ReactNode;
@@ -112,22 +112,23 @@ export function DependencyCheck(props: Props) {
112112
</Ink.Text>
113113
}
114114
function={async () => {
115-
const auth_status = await cli(`gh auth status`, {
116-
ignoreExitCode: true,
117-
});
115+
const options = { ignoreExitCode: true };
116+
const auth_status = await cli(`gh auth status`, options);
118117

119118
if (auth_status.code === 0) {
120-
const username = match_group(
121-
auth_status.stdout,
122-
RE.auth_username,
123-
"username"
124-
);
119+
const username = gh.auth_status(auth_status.stdout);
125120

126-
actions.set((state) => {
127-
state.username = username;
128-
});
121+
if (username) {
122+
actions.set((state) => {
123+
state.username = username;
124+
});
129125

130-
return;
126+
return;
127+
}
128+
}
129+
130+
if (actions.isDebug()) {
131+
actions.error("gh auth status could not find username");
131132
}
132133

133134
actions.output(
@@ -148,8 +149,3 @@ export function DependencyCheck(props: Props) {
148149
</Await>
149150
);
150151
}
151-
152-
const RE = {
153-
// Logged in to github.com as magus
154-
auth_username: /Logged in to github.com as (?<username>[^\s]+)/,
155-
};

src/core/match_group.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ export function match_group(value: string, re: RegExp, group: string) {
88
invariant(result, `match.groups must contain [${group}] ${debug}`);
99
return result;
1010
}
11+
12+
match_group.safe = (value: string, re: RegExp, group: string) => {
13+
try {
14+
return match_group(value, re, group);
15+
} catch (err) {
16+
return null;
17+
}
18+
};

src/github/gh.auth_status.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { test, expect } from "bun:test";
2+
3+
import * as gh from "./gh";
4+
5+
test("logged in as", () => {
6+
const username = gh.auth_status(
7+
" ✓ Logged in to github.com as magus (keyring)\n"
8+
);
9+
10+
expect(username).toBe("magus");
11+
});
12+
13+
test("logged in without as", () => {
14+
const username = gh.auth_status(
15+
"✓ Logged in to github.com account xoxohorses (keyring)"
16+
);
17+
18+
expect(username).toBe("xoxohorses");
19+
});
20+
21+
test("returns null when no match is found", () => {
22+
const username = gh.auth_status("this should not match anything");
23+
expect(username).toBe(null);
24+
});

src/github/gh.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { match_group } from "~/core/match_group";
2+
3+
export function auth_status(output: string) {
4+
let username;
5+
6+
username = match_group.safe(output, RE.logged_in_as, "username");
7+
if (username) return username;
8+
9+
username = match_group.safe(output, RE.logged_in_account, "username");
10+
if (username) return username;
11+
12+
return null;
13+
}
14+
15+
const RE = {
16+
// Logged in to github.com as magus
17+
logged_in_as: /Logged in to github.com as (?<username>[^\s]+)/,
18+
logged_in_account: /Logged in to github.com account (?<username>[^\s]+)/,
19+
};

0 commit comments

Comments
 (0)