Skip to content

Commit

Permalink
Merge pull request #337 from giselles-ai/fix-github-webhook-command-p…
Browse files Browse the repository at this point in the history
…arsing

Fix GitHub command parsing with robust line ending
  • Loading branch information
satococoa authored Jan 28, 2025
2 parents 87ff659 + 9c8084c commit 901b0ca
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
49 changes: 49 additions & 0 deletions app/webhooks/github/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,52 @@ test("parseCommand3", () => {
content: "Please write a blog.\nTheme is free.\n\nText mood is ....",
});
});

test("parseCommand4 - with \\n line endings", () => {
expect(
parseCommand(`
/giselle hello\n\nPlease write a blog.\nTheme is free.
`),
).toStrictEqual({
callSign: "hello",
content: "Please write a blog.\nTheme is free.",
});
});

test("parseCommand5 - mixed line endings", () => {
expect(
parseCommand(`
/giselle hello\r\n\nPlease write a blog.\r\nTheme is free.
`),
).toStrictEqual({
callSign: "hello",
content: "Please write a blog.\nTheme is free.",
});
});

test("invalid command format returns null", () => {
expect(parseCommand("invalid command")).toBe(null);
expect(parseCommand("/invalid hello")).toBe(null);
expect(parseCommand("giselle hello")).toBe(null);
});

test("command with multiple spaces", () => {
expect(parseCommand("/giselle hello \nsome content")).toStrictEqual({
callSign: "hello",
content: "some content",
});
});

test("command with empty content", () => {
expect(parseCommand("/giselle hello\n")).toStrictEqual({
callSign: "hello",
content: "",
});
});

test("command with no empty line after command", () => {
expect(parseCommand("/giselle hello content")).toStrictEqual({
callSign: "hello",
content: "content",
});
});
22 changes: 16 additions & 6 deletions app/webhooks/github/command.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
export interface Command {
callSign: string;
instruction: string;
content: string;
}

export function parseCommand(text: string) {
const lines = text.trim().split("\r\n");
export function parseCommand(text: string): Command | null {
// Normalize line breaks and split into lines
const normalizedText = text.replace(/\r\n|\r|\n/g, "\n");
const lines = normalizedText.trim().split("\n");

const commandLine = lines[0];
const commandMatch = commandLine.match(/^\/giselle\s+([^\]]+)/);
const commandMatch = commandLine.match(/^\/giselle\s+([^\n]+)/);
if (!commandMatch) {
return null;
}

// Extract callSign (first word) and remaining content
const parts = commandMatch[1].trim().split(/\s+/);
const callSign = parts[0];
const firstLineContent = parts.slice(1).join(" ");

// Combine remaining content
const content = [firstLineContent, ...lines.slice(1)].join("\n").trim();

return {
callSign: commandMatch[1],
content: lines.slice(1).join("\n").trim(),
callSign,
content,
};
}

0 comments on commit 901b0ca

Please sign in to comment.