Skip to content

Commit 2663415

Browse files
github action: truncate PR titles to 256 chars to avoid GH api errors (anomalyco#3727)
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: rekram1-node <rekram1-node@users.noreply.github.com>
1 parent 51be67c commit 2663415

1 file changed

Lines changed: 45 additions & 15 deletions

File tree

github/index.ts

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ try {
168168
const summary = await summarize(response)
169169
await pushToLocalBranch(summary)
170170
}
171-
const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${useShareUrl()}/s/${shareId}`))
171+
const hasShared = prData.comments.nodes.some((c) =>
172+
c.body.includes(`${useShareUrl()}/s/${shareId}`),
173+
)
172174
await updateComment(`${response}${footer({ image: !hasShared })}`)
173175
}
174176
// Fork PR
@@ -180,7 +182,9 @@ try {
180182
const summary = await summarize(response)
181183
await pushToForkBranch(summary, prData)
182184
}
183-
const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${useShareUrl()}/s/${shareId}`))
185+
const hasShared = prData.comments.nodes.some((c) =>
186+
c.body.includes(`${useShareUrl()}/s/${shareId}`),
187+
)
184188
await updateComment(`${response}${footer({ image: !hasShared })}`)
185189
}
186190
}
@@ -361,7 +365,9 @@ async function getAccessToken() {
361365

362366
if (!response.ok) {
363367
const responseJson = (await response.json()) as { error?: string }
364-
throw new Error(`App token exchange failed: ${response.status} ${response.statusText} - ${responseJson.error}`)
368+
throw new Error(
369+
`App token exchange failed: ${response.status} ${response.statusText} - ${responseJson.error}`,
370+
)
365371
}
366372

367373
const responseJson = (await response.json()) as { token: string }
@@ -402,8 +408,12 @@ async function getUserPrompt() {
402408
// ie. <img alt="Image" src="https://github.com/user-attachments/assets/xxxx" />
403409
// ie. [api.json](https://github.com/user-attachments/files/21433810/api.json)
404410
// ie. ![Image](https://github.com/user-attachments/assets/xxxx)
405-
const mdMatches = prompt.matchAll(/!?\[.*?\]\((https:\/\/github\.com\/user-attachments\/[^)]+)\)/gi)
406-
const tagMatches = prompt.matchAll(/<img .*?src="(https:\/\/github\.com\/user-attachments\/[^"]+)" \/>/gi)
411+
const mdMatches = prompt.matchAll(
412+
/!?\[.*?\]\((https:\/\/github\.com\/user-attachments\/[^)]+)\)/gi,
413+
)
414+
const tagMatches = prompt.matchAll(
415+
/<img .*?src="(https:\/\/github\.com\/user-attachments\/[^"]+)" \/>/gi,
416+
)
407417
const matches = [...mdMatches, ...tagMatches].sort((a, b) => a.index - b.index)
408418
console.log("Images", JSON.stringify(matches, null, 2))
409419

@@ -430,7 +440,8 @@ async function getUserPrompt() {
430440

431441
// Replace img tag with file path, ie. @image.png
432442
const replacement = `@${filename}`
433-
prompt = prompt.slice(0, start + offset) + replacement + prompt.slice(start + offset + tag.length)
443+
prompt =
444+
prompt.slice(0, start + offset) + replacement + prompt.slice(start + offset + tag.length)
434445
offset += replacement.length - tag.length
435446

436447
const contentType = res.headers.get("content-type")
@@ -498,7 +509,12 @@ async function subscribeSessionEvents() {
498509
? JSON.stringify(part.state.input)
499510
: "Unknown"
500511
console.log()
501-
console.log(color + `|`, "\x1b[0m\x1b[2m" + ` ${tool.padEnd(7, " ")}`, "", "\x1b[0m" + title)
512+
console.log(
513+
color + `|`,
514+
"\x1b[0m\x1b[2m" + ` ${tool.padEnd(7, " ")}`,
515+
"",
516+
"\x1b[0m" + title,
517+
)
502518
}
503519

504520
if (part.type === "text") {
@@ -710,7 +726,8 @@ async function assertPermissions() {
710726
throw new Error(`Failed to check permissions for user ${actor}: ${error}`)
711727
}
712728

713-
if (!["admin", "write"].includes(permission)) throw new Error(`User ${actor} does not have write permissions`)
729+
if (!["admin", "write"].includes(permission))
730+
throw new Error(`User ${actor} does not have write permissions`)
714731
}
715732

716733
async function updateComment(body: string) {
@@ -730,12 +747,13 @@ async function updateComment(body: string) {
730747
async function createPR(base: string, branch: string, title: string, body: string) {
731748
console.log("Creating pull request...")
732749
const { repo } = useContext()
750+
const truncatedTitle = title.length > 256 ? title.slice(0, 253) + "..." : title
733751
const pr = await octoRest.rest.pulls.create({
734752
owner: repo.owner,
735753
repo: repo.repo,
736754
head: branch,
737755
base,
738-
title,
756+
title: truncatedTitle,
739757
body,
740758
})
741759
return pr.data.number
@@ -753,7 +771,9 @@ function footer(opts?: { image?: boolean }) {
753771

754772
return `<a href="${useShareUrl()}/s/${shareId}"><img width="200" alt="${titleAlt}" src="https://social-cards.sst.dev/opencode-share/${title64}.png?model=${providerID}/${modelID}&version=${session.version}&id=${shareId}" /></a>\n`
755773
})()
756-
const shareUrl = shareId ? `[opencode session](${useShareUrl()}/s/${shareId})&nbsp;&nbsp;|&nbsp;&nbsp;` : ""
774+
const shareUrl = shareId
775+
? `[opencode session](${useShareUrl()}/s/${shareId})&nbsp;&nbsp;|&nbsp;&nbsp;`
776+
: ""
757777
return `\n\n${image}${shareUrl}[github run](${useEnvRunUrl()})`
758778
}
759779

@@ -936,9 +956,13 @@ function buildPromptDataForPR(pr: GitHubPullRequest) {
936956
})
937957
.map((c) => `- ${c.author.login} at ${c.createdAt}: ${c.body}`)
938958

939-
const files = (pr.files.nodes || []).map((f) => `- ${f.path} (${f.changeType}) +${f.additions}/-${f.deletions}`)
959+
const files = (pr.files.nodes || []).map(
960+
(f) => `- ${f.path} (${f.changeType}) +${f.additions}/-${f.deletions}`,
961+
)
940962
const reviewData = (pr.reviews.nodes || []).map((r) => {
941-
const comments = (r.comments.nodes || []).map((c) => ` - ${c.path}:${c.line ?? "?"}: ${c.body}`)
963+
const comments = (r.comments.nodes || []).map(
964+
(c) => ` - ${c.path}:${c.line ?? "?"}: ${c.body}`,
965+
)
942966
return [
943967
`- ${r.author.login} at ${r.submittedAt}:`,
944968
` - Review body: ${r.body}`,
@@ -960,9 +984,15 @@ function buildPromptDataForPR(pr: GitHubPullRequest) {
960984
`Deletions: ${pr.deletions}`,
961985
`Total Commits: ${pr.commits.totalCount}`,
962986
`Changed Files: ${pr.files.nodes.length} files`,
963-
...(comments.length > 0 ? ["<pull_request_comments>", ...comments, "</pull_request_comments>"] : []),
964-
...(files.length > 0 ? ["<pull_request_changed_files>", ...files, "</pull_request_changed_files>"] : []),
965-
...(reviewData.length > 0 ? ["<pull_request_reviews>", ...reviewData, "</pull_request_reviews>"] : []),
987+
...(comments.length > 0
988+
? ["<pull_request_comments>", ...comments, "</pull_request_comments>"]
989+
: []),
990+
...(files.length > 0
991+
? ["<pull_request_changed_files>", ...files, "</pull_request_changed_files>"]
992+
: []),
993+
...(reviewData.length > 0
994+
? ["<pull_request_reviews>", ...reviewData, "</pull_request_reviews>"]
995+
: []),
966996
"</pull_request>",
967997
].join("\n")
968998
}

0 commit comments

Comments
 (0)