diff --git a/github/index.ts b/github/index.ts index 73378894cd3..c5611bb87c8 100644 --- a/github/index.ts +++ b/github/index.ts @@ -120,6 +120,7 @@ let commentId: number let gitConfig: string let session: { id: string; title: string; version: string } let shareId: string | undefined +let shareUrl: string | undefined let exitCode = 0 type PromptFiles = Awaited>["promptFiles"] @@ -145,15 +146,16 @@ try { const repoData = await fetchRepo() session = await client.session.create().then((r) => r.data) await subscribeSessionEvents() - shareId = await (async () => { - if (useEnvShare() === false) return - if (!useEnvShare() && repoData.data.private) return - await client.session.share({ path: session }) - return session.id.slice(-8) - })() + const share = useEnvShare() + const canShare = share !== false && (share || !repoData.data.private) + if (canShare) { + const updated = await client.session.share({ path: session }).then((r) => r.data) + shareUrl = updated.share?.url + shareId = session.id.slice(-8) + } console.log("opencode session", session.id) - if (shareId) { - console.log("Share link:", `${useShareUrl()}/s/${shareId}`) + if (shareUrl) { + console.log("Share link:", shareUrl) } // Handle 3 cases @@ -162,30 +164,29 @@ try { // 3. Fork PR if (isPullRequest()) { const prData = await fetchPR() - // Local PR - if (prData.headRepository.nameWithOwner === prData.baseRepository.nameWithOwner) { + const isLocal = prData.headRepository.nameWithOwner === prData.baseRepository.nameWithOwner + const hasShared = shareUrl ? prData.comments.nodes.some((c) => c.body.includes(shareUrl)) : false + + if (isLocal) { await checkoutLocalBranch(prData) - const dataPrompt = buildPromptDataForPR(prData) - const response = await chat(`${userPrompt}\n\n${dataPrompt}`, promptFiles) - if (await branchIsDirty()) { - const summary = await summarize(response) - await pushToLocalBranch(summary) - } - const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${useShareUrl()}/s/${shareId}`)) - await updateComment(`${response}${footer({ image: !hasShared })}`) } - // Fork PR - else { + + if (!isLocal) { await checkoutForkBranch(prData) - const dataPrompt = buildPromptDataForPR(prData) - const response = await chat(`${userPrompt}\n\n${dataPrompt}`, promptFiles) - if (await branchIsDirty()) { - const summary = await summarize(response) + } + + const dataPrompt = buildPromptDataForPR(prData) + const response = await chat(`${userPrompt}\n\n${dataPrompt}`, promptFiles) + if (await branchIsDirty()) { + const summary = await summarize(response) + if (isLocal) { + await pushToLocalBranch(summary) + } + if (!isLocal) { await pushToForkBranch(summary, prData) } - const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${useShareUrl()}/s/${shareId}`)) - await updateComment(`${response}${footer({ image: !hasShared })}`) } + await updateComment(`${response}${footer({ image: !hasShared })}`) } // Issue else { @@ -361,10 +362,6 @@ function useIssueId() { return payload.issue.number } -function useShareUrl() { - return isMock() ? "https://dev.opencode.ai" : "https://opencode.ai" -} - async function getAccessToken() { const { repo } = useContext() @@ -817,14 +814,15 @@ function footer(opts?: { image?: boolean }) { const image = (() => { if (!shareId) return "" if (!opts?.image) return "" + if (!shareUrl) return "" const titleAlt = encodeURIComponent(session.title.substring(0, 50)) const title64 = Buffer.from(session.title.substring(0, 700), "utf8").toString("base64") - return `${titleAlt}\n` + return `${titleAlt}\n` })() - const shareUrl = shareId ? `[opencode session](${useShareUrl()}/s/${shareId})  |  ` : "" - return `\n\n${image}${shareUrl}[github run](${useEnvRunUrl()})` + const shareLink = shareUrl ? `[opencode session](${shareUrl})  |  ` : "" + return `\n\n${image}${shareLink}[github run](${useEnvRunUrl()})` } async function fetchRepo() { diff --git a/packages/opencode/src/cli/cmd/github.ts b/packages/opencode/src/cli/cmd/github.ts index 927c964c9d8..eb868915dce 100644 --- a/packages/opencode/src/cli/cmd/github.ts +++ b/packages/opencode/src/cli/cmd/github.ts @@ -467,7 +467,6 @@ export const GithubRunCommand = cmd({ ? (payload as IssueCommentEvent | IssuesEvent).issue.number : (payload as PullRequestEvent | PullRequestReviewCommentEvent).pull_request.number const runUrl = `/${owner}/${repo}/actions/runs/${runId}` - const shareBaseUrl = isMock ? "https://dev.opencode.ai" : "https://opencode.ai" let appToken: string let octoRest: Octokit @@ -475,6 +474,7 @@ export const GithubRunCommand = cmd({ let gitConfig: string let session: { id: string; title: string; version: string } let shareId: string | undefined + let shareUrl: string | undefined let exitCode = 0 type PromptFiles = Awaited>["promptFiles"] const triggerCommentId = isCommentEvent @@ -527,13 +527,16 @@ export const GithubRunCommand = cmd({ ], }) subscribeSessionEvents() - shareId = await (async () => { - if (share === false) return - if (!share && repoData.data.private) return - await Session.share(session.id) - return session.id.slice(-8) - })() + const canShare = share !== false && (share || !repoData.data.private) + if (canShare) { + const shared = await Session.share(session.id) + shareUrl = shared.url + shareId = session.id.slice(-8) + } console.log("opencode session", session.id) + if (shareUrl) { + console.log("Share link:", shareUrl) + } // Handle event types: // REPO_EVENTS (schedule, workflow_dispatch): no issue/PR context, output to logs/PR only @@ -580,7 +583,8 @@ export const GithubRunCommand = cmd({ const summary = await summarize(response) await pushToLocalBranch(summary, uncommittedChanges) } - const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${shareBaseUrl}/s/${shareId}`)) + const hasShared = shareUrl ? prData.comments.nodes.some((c) => c.body.includes(shareUrl!)) : false + await createComment(`${response}${footer({ image: !hasShared })}`) await removeReaction(commentType) } @@ -595,7 +599,8 @@ export const GithubRunCommand = cmd({ const summary = await summarize(response) await pushToForkBranch(summary, prData, uncommittedChanges) } - const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${shareBaseUrl}/s/${shareId}`)) + const hasShared = shareUrl ? prData.comments.nodes.some((c) => c.body.includes(shareUrl!)) : false + await createComment(`${response}${footer({ image: !hasShared })}`) await removeReaction(commentType) } @@ -1300,14 +1305,15 @@ Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` const image = (() => { if (!shareId) return "" if (!opts?.image) return "" + if (!shareUrl) return "" const titleAlt = encodeURIComponent(session.title.substring(0, 50)) const title64 = Buffer.from(session.title.substring(0, 700), "utf8").toString("base64") - return `${titleAlt}\n` + return `${titleAlt}\n` })() - const shareUrl = shareId ? `[opencode session](${shareBaseUrl}/s/${shareId})  |  ` : "" - return `\n\n${image}${shareUrl}[github run](${runUrl})` + const shareLink = shareUrl ? `[opencode session](${shareUrl})  |  ` : "" + return `\n\n${image}${shareLink}[github run](${runUrl})` } async function fetchRepo() {