Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions packages/opencode/src/cli/cmd/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ export function formatPromptTooLargeError(files: { filename: string; content: st
return `PROMPT_TOO_LARGE: The prompt exceeds the model's context limit.${fileDetails}`
}

/**
* Builds a prompt string from a GitHub issue payload.
* Used when an issue is assigned or opened to auto-extract the issue context.
*/
export function buildIssuePrompt(issue: {
number: number
title: string
body?: string | null
labels?: (string | { name?: string })[]
}): string {
const labels = issue.labels?.map((l) => (typeof l === "string" ? l : l.name)).filter(Boolean)
const labelText = labels?.length ? `\nLabels: ${labels.join(", ")}` : ""
return `Issue #${issue.number}: ${issue.title}${labelText}\n\n${issue.body || "No description provided."}`
}

export const GithubCommand = cmd({
command: "github",
describe: "manage GitHub agent",
Expand Down Expand Up @@ -770,14 +785,27 @@ export const GithubRunCommand = cmd({

async function getUserPrompt() {
const customPrompt = process.env["PROMPT"]
// For repo events and issues events, PROMPT is required since there's no comment to extract from
if (isRepoEvent || isIssuesEvent) {
// For repo events, PROMPT is required since there's no comment/issue to extract from
if (isRepoEvent) {
if (!customPrompt) {
const eventType = isRepoEvent ? "scheduled and workflow_dispatch" : "issues"
throw new Error(`PROMPT input is required for ${eventType} events`)
throw new Error(`PROMPT input is required for scheduled and workflow_dispatch events`)
}
return { userPrompt: customPrompt, promptFiles: [] }
}
// For issues events: auto-extract issue title+body when assigned or opened, otherwise require PROMPT
if (isIssuesEvent) {
if (customPrompt) {
return { userPrompt: customPrompt, promptFiles: [] }
}
const issuesPayload = payload as IssuesEvent
if (issuesPayload.action === "assigned" || issuesPayload.action === "opened") {
return {
userPrompt: buildIssuePrompt(issuesPayload.issue),
promptFiles: [],
}
}
throw new Error(`PROMPT input is required for issues events with action "${issuesPayload.action}"`)
}

if (customPrompt) {
return { userPrompt: customPrompt, promptFiles: [] }
Expand Down
111 changes: 110 additions & 1 deletion packages/opencode/test/cli/github-action.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect, describe } from "bun:test"
import { extractResponseText, formatPromptTooLargeError } from "../../src/cli/cmd/github"
import { extractResponseText, formatPromptTooLargeError, buildIssuePrompt } from "../../src/cli/cmd/github"
import type { MessageV2 } from "../../src/session/message-v2"
import { SessionID, MessageID, PartID } from "../../src/session/schema"

Expand Down Expand Up @@ -196,3 +196,112 @@ describe("formatPromptTooLargeError", () => {
expect(result).toInclude("img3.gif (9 KB)")
})
})

describe("buildIssuePrompt", () => {
test("builds prompt with title and body", () => {
const result = buildIssuePrompt({
number: 42,
title: "Fix login bug",
body: "Users cannot log in with SSO.",
})
expect(result).toBe("Issue #42: Fix login bug\n\nUsers cannot log in with SSO.")
})

test("uses fallback when body is null", () => {
const result = buildIssuePrompt({
number: 1,
title: "No body issue",
body: null,
})
expect(result).toInclude("No description provided.")
expect(result).toStartWith("Issue #1: No body issue")
})

test("uses fallback when body is undefined", () => {
const result = buildIssuePrompt({
number: 1,
title: "No body issue",
})
expect(result).toInclude("No description provided.")
})

test("uses fallback when body is empty string", () => {
const result = buildIssuePrompt({
number: 5,
title: "Empty body",
body: "",
})
expect(result).toInclude("No description provided.")
})

test("includes labels from string array", () => {
const result = buildIssuePrompt({
number: 10,
title: "Labeled issue",
body: "Some body",
labels: ["bug", "priority"],
})
expect(result).toInclude("Labels: bug, priority")
})

test("includes labels from object array", () => {
const result = buildIssuePrompt({
number: 10,
title: "Labeled issue",
body: "Some body",
labels: [{ name: "enhancement" }, { name: "help wanted" }],
})
expect(result).toInclude("Labels: enhancement, help wanted")
})

test("handles mixed label types (string and object)", () => {
const result = buildIssuePrompt({
number: 10,
title: "Mixed labels",
body: "Body",
labels: ["bug", { name: "urgent" }],
})
expect(result).toInclude("Labels: bug, urgent")
})

test("omits labels line when labels array is empty", () => {
const result = buildIssuePrompt({
number: 3,
title: "No labels",
body: "Body text",
labels: [],
})
expect(result).not.toInclude("Labels:")
expect(result).toBe("Issue #3: No labels\n\nBody text")
})

test("omits labels line when labels is undefined", () => {
const result = buildIssuePrompt({
number: 3,
title: "No labels",
body: "Body text",
})
expect(result).not.toInclude("Labels:")
})

test("filters out labels with missing name", () => {
const result = buildIssuePrompt({
number: 7,
title: "Partial labels",
body: "Body",
labels: [{ name: "valid" }, { name: undefined } as any, { name: "" }],
})
expect(result).toInclude("Labels: valid")
})

test("preserves markdown formatting in body", () => {
const body = "## Steps to reproduce\n\n1. Open the app\n2. Click login\n\n```js\nconsole.log('test')\n```"
const result = buildIssuePrompt({
number: 99,
title: "Markdown body",
body,
})
expect(result).toInclude("## Steps to reproduce")
expect(result).toInclude("```js")
})
})
Loading