8000 parseCoderUsersListOutput · coder/start-workspace-action@c496518 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit c496518

Browse files
committed
parseCoderUsersListOutput
1 parent ca479f0 commit c496518

File tree

6 files changed

+176
-3
lines changed

6 files changed

+176
-3
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnSave": true,
3+
}

bun.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"workspaces": {
44
"": {
55
"name": "start-workspace-action",
6+
"dependencies": {
7+
"dedent": "^1.5.3",
8+
},
69
"devDependencies": {
710
"@types/bun": "latest",
811
"husky": "^8.0.0",
@@ -21,6 +24,8 @@
2124

2225
"bun-types": ["bun-types@1.2.7", "", { "dependencies": { "@types/node": "*", "@types/ws": "*" } }, "sha512-P4hHhk7kjF99acXqKvltyuMQ2kf/rzIw3ylEDpCxDS9Xa0X0Yp/gJu/vDCucmWpiur5qJ0lwB2bWzOXa2GlHqA=="],
2326

27+
"dedent": ["dedent@1.5.3", "", { "peerDependencies": { "babel-plugin-macros": "^3.1.0" }, "optionalPeers": ["babel-plugin-macros"] }, "sha512-NHQtfOOW68WD8lgypbLA5oT+Bt0xXJhiYvoR6SmmNXZfpzOGXwdKWmcwG8N7PwVVWV3eF/68nmD9BaJSsTBhyQ=="],
28+
2429
"husky": ["husky@8.0.3", "", { "bin": { "husky": "lib/bin.js" } }, "sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg=="],
2530

2631
"typescript": ["typescript@5.8.2", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-aJn6wq13/afZp/jT9QZmwEjDqqvSGp1VT5GVg+f/t6/oVyrgXM6BY1h9BRh/O5p3PlUPAe+WuiEZOmb/49RqoQ=="],

dist/index.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1-
// Source hash: 39e1ff1373d5a21bb3cbb5b9b4be13afbaafc73a33293f293d40656449b70de7
1+
// Source hash: da5457c3c88885477f3bac30bb2f34655d70c769ab2d9bbf3e26bb1269c3a7e9
22
// src/index.ts
3-
console.log("Hello via Bun!");
3+
import assert from "assert";
4+
5+
class UserFacingError extends Error {
6+
}
7+
8+
class StartWorkspaceAction {
9+
logger;
10+
githubUsername;
11+
coderUrl;
12+
coderToken;
13+
constructor(logger, githubUsername, coderUrl, coderToken) {
14+
this.logger = logger;
15+
this.githubUsername = githubUsername;
16+
this.coderUrl = coderUrl;
17+
this.coderToken = coderToken;
18+
}
19+
parseCoderUsersListOutput(output) {
20+
const lines = output.trim().split(`
21+
`);
22+
if (lines.length < 2) {
23+
assert(this.githubUsername, "GitHub username is required");
24+
throw new UserFacingError(`No Coder username mapping found for GitHub user @${this.githubUsername}`);
25+
}
26+
if (lines.length > 2) {
27+
const usernames = lines.slice(1).map((line) => line.trim());
28+
this.logger.warn(`Multiple Coder usernames found for GitHub user ${this.githubUsername}: ${usernames.join(", ")}. Using the first one.`);
29+
}
30+
const username = lines[1]?.trim();
31+
assert(username, "Coder username not found in output");
32+
return username;
33+
}
34+
}
35+
var src_default = StartWorkspaceAction;
36+
export {
37+
src_default as default,
38+
UserFacingError,
39+
StartWorkspaceAction
40+
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
"build": "bun build ./src/index.ts --target node --bundle --outfile dist/index.js && bash scripts/update-build-hash.sh",
1515
"verify-build": "bash scripts/verify-build.sh",
1616
"prepare": "husky install"
17+
},
18+
"dependencies": {
19+
"dedent": "^1.5.3"
1720
}
1821
}

src/index.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { describe, expect, it } from "bun:test";
2+
import { StartWorkspaceAction, UserFacingError, type Logger } from ".";
3+
import dedent from "dedent";
4+
5+
class TestLogger implements Logger {
6+
logs: string[] = [];
7+
warns: string[] = [];
8+
errors: string[] = [];
9+
10+
log(message: string) {
11+
this.logs.push(message);
12+
}
13+
warn(message: string) {
14+
this.warns.push(message);
15+
}
16+
error(message: string) {
17+
this.errors.push(message);
18+
}
19+
}
20+
21+
describe("StartWorkspaceAction", () => {
22+
it("parseCoderUsersListOutput", () => {
23+
const logger = new TestLogger();
24+
const action = new StartWorkspaceAction(
25+
logger,
26+
"github-user",
27+
"https://coder.com",
28+
"coder-token"
29+
);
30+
31+
const username = action.parseCoderUsersListOutput(dedent`
32+
USERNAME
33+
hugo
34+
`);
35+
expect(username).toBe("hugo");
36+
37+
expect(logger.logs).toEqual([]);
38+
expect(logger.warns).toEqual([]);
39+
expect(logger.errors).toEqual([]);
40+
41+
const username2 = action.parseCoderUsersListOutput(dedent`
42+
USERNAME
43+
hugo
44+
alice
45+
`);
46+
expect(username2).toBe("hugo");
47+
48+
expect(logger.logs).toEqual([]);
49+
expect(logger.warns).toEqual([
50+
"Multiple Coder usernames found for GitHub user github-user: hugo, alice. Using the first one.",
51+
]);
52+
expect(logger.errors).toEqual([]);
53+
54+
logger.warns = [];
55+
expect(() =>
56+
action.parseCoderUsersListOutput(dedent`
57+
USERNAME
58+
`)
59+
).toThrowError(
60+
new UserFacingError(
61+
"No Coder username mapping found for GitHub user @github-user"
62+
)
63+
);
64+
expect(logger.logs).toEqual([]);
65+
expect(logger.warns).toEqual([]);
66+
expect(logger.errors).toEqual([]);
67+
68+
// Test that the output is trimmed
69+
const username3 = action.parseCoderUsersListOutput(dedent`
70+
USERNAME
71+
hugo
72+
`);
73+
expect(username3).toBe("hugo");
74+
75+
expect(logger.logs).toEqual([]);
76+
expect(logger.warns).toEqual([]);
77+
expect(logger.errors).toEqual([]);
78+
});
79+
});

src/index.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1-
console.log("Hello via Bun!");
1+
import assert from "assert";
2+
3+
export interface Logger {
4+
log(message: string): void;
5+
warn(message: string): void;
6+
error(message: string): void;
7+
}
8+
9+
export class UserFacingError extends Error {}
10+
11+
export class StartWorkspaceAction {
12+
constructor(
13+
private readonly logger: Logger,
14+
private readonly githubUsername: string | undefined,
15+
private readonly coderUrl: string,
16+
private readonly coderToken: string
17+
) {}
18+
19+
/**
20+
* Parse the output of the `coder users list` command.
21+
* @param output The output of the `coder users list` command.
22+
* @returns The username of the Coder user.
23+
*/
24+
parseCoderUsersListOutput(output: string): string {
25+
const lines = output.trim().split("\n");
26+
if (lines.length < 2) {
27+
assert(this.githubUsername, "GitHub username is required");
28+
throw new UserFacingError(
29+
`No Coder username mapping found for GitHub user @${this.githubUsername}`
30+
);
31+
}
32+
if (lines.length > 2) {
33+
const usernames = lines.slice(1).map((line) => line.trim());
34+
this.logger.warn(
35+
`Multiple Coder usernames found for GitHub user ${
36+
this.githubUsername
37+
}: ${usernames.join(", ")}. Using the first one.`
38+
);
39+
}
40+
const username = lines[1]?.trim();
41+
assert(username, "Coder username not found in output");
42+
43+
return username;
44+
}
45+
}
46+
47+
export default StartWorkspaceAction;

0 commit comments

Comments
 (0)
0