( content: string )
| 9 | } |
| 10 | |
| 11 | export function parseCopilotCommitMessage( |
| 12 | content: string |
| 13 | ): ICopilotCommitMessage { |
| 14 | const jsonMatch = |
| 15 | content.match(/```json\s*([\s\S]*?)```/) || |
| 16 | content.match(/```\s*([\s\S]*?)```/) |
| 17 | const jsonStr = jsonMatch ? jsonMatch[1].trim() : content.trim() |
| 18 | |
| 19 | let parsed: unknown |
| 20 | try { |
| 21 | parsed = JSON.parse(jsonStr) |
| 22 | } catch { |
| 23 | throw new Error( |
| 24 | 'Copilot returned invalid JSON for commit message generation' |
| 25 | ) |
| 26 | } |
| 27 | |
| 28 | if (!isRecord(parsed)) { |
| 29 | throw new Error( |
| 30 | 'Copilot returned an invalid commit message payload: expected an object' |
| 31 | ) |
| 32 | } |
| 33 | |
| 34 | const title = parsed.title |
| 35 | if (typeof title !== 'string' || title.trim().length === 0) { |
| 36 | throw new Error( |
| 37 | 'Copilot returned an invalid commit message payload: "title" must be a non-empty string' |
| 38 | ) |
| 39 | } |
| 40 | |
| 41 | const description = parsed.description |
| 42 | if (description === undefined) { |
| 43 | return { |
| 44 | title, |
| 45 | description: '', |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if (typeof description !== 'string') { |
| 50 | throw new Error( |
| 51 | 'Copilot returned an invalid commit message payload: "description" must be a string when provided' |
| 52 | ) |
| 53 | } |
| 54 | |
| 55 | return { |
| 56 | title, |
| 57 | description, |
| 58 | } |
| 59 | } |
no test coverage detected