Просмотр исходного кода

github action: truncate PR titles to 256 chars to avoid GH api errors (#3727)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: rekram1-node <[email protected]>
opencode-agent[bot] 3 месяцев назад
Родитель
Сommit
2663415d47
1 измененных файлов с 45 добавлено и 15 удалено
  1. 45 15
      github/index.ts

+ 45 - 15
github/index.ts

@@ -168,7 +168,9 @@ try {
         const summary = await summarize(response)
         await pushToLocalBranch(summary)
       }
-      const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${useShareUrl()}/s/${shareId}`))
+      const hasShared = prData.comments.nodes.some((c) =>
+        c.body.includes(`${useShareUrl()}/s/${shareId}`),
+      )
       await updateComment(`${response}${footer({ image: !hasShared })}`)
     }
     // Fork PR
@@ -180,7 +182,9 @@ try {
         const summary = await summarize(response)
         await pushToForkBranch(summary, prData)
       }
-      const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${useShareUrl()}/s/${shareId}`))
+      const hasShared = prData.comments.nodes.some((c) =>
+        c.body.includes(`${useShareUrl()}/s/${shareId}`),
+      )
       await updateComment(`${response}${footer({ image: !hasShared })}`)
     }
   }
@@ -361,7 +365,9 @@ async function getAccessToken() {
 
   if (!response.ok) {
     const responseJson = (await response.json()) as { error?: string }
-    throw new Error(`App token exchange failed: ${response.status} ${response.statusText} - ${responseJson.error}`)
+    throw new Error(
+      `App token exchange failed: ${response.status} ${response.statusText} - ${responseJson.error}`,
+    )
   }
 
   const responseJson = (await response.json()) as { token: string }
@@ -402,8 +408,12 @@ async function getUserPrompt() {
   // ie. <img alt="Image" src="https://github.com/user-attachments/assets/xxxx" />
   // ie. [api.json](https://github.com/user-attachments/files/21433810/api.json)
   // ie. ![Image](https://github.com/user-attachments/assets/xxxx)
-  const mdMatches = prompt.matchAll(/!?\[.*?\]\((https:\/\/github\.com\/user-attachments\/[^)]+)\)/gi)
-  const tagMatches = prompt.matchAll(/<img .*?src="(https:\/\/github\.com\/user-attachments\/[^"]+)" \/>/gi)
+  const mdMatches = prompt.matchAll(
+    /!?\[.*?\]\((https:\/\/github\.com\/user-attachments\/[^)]+)\)/gi,
+  )
+  const tagMatches = prompt.matchAll(
+    /<img .*?src="(https:\/\/github\.com\/user-attachments\/[^"]+)" \/>/gi,
+  )
   const matches = [...mdMatches, ...tagMatches].sort((a, b) => a.index - b.index)
   console.log("Images", JSON.stringify(matches, null, 2))
 
@@ -430,7 +440,8 @@ async function getUserPrompt() {
 
     // Replace img tag with file path, ie. @image.png
     const replacement = `@${filename}`
-    prompt = prompt.slice(0, start + offset) + replacement + prompt.slice(start + offset + tag.length)
+    prompt =
+      prompt.slice(0, start + offset) + replacement + prompt.slice(start + offset + tag.length)
     offset += replacement.length - tag.length
 
     const contentType = res.headers.get("content-type")
@@ -498,7 +509,12 @@ async function subscribeSessionEvents() {
                     ? JSON.stringify(part.state.input)
                     : "Unknown"
                 console.log()
-                console.log(color + `|`, "\x1b[0m\x1b[2m" + ` ${tool.padEnd(7, " ")}`, "", "\x1b[0m" + title)
+                console.log(
+                  color + `|`,
+                  "\x1b[0m\x1b[2m" + ` ${tool.padEnd(7, " ")}`,
+                  "",
+                  "\x1b[0m" + title,
+                )
               }
 
               if (part.type === "text") {
@@ -710,7 +726,8 @@ async function assertPermissions() {
     throw new Error(`Failed to check permissions for user ${actor}: ${error}`)
   }
 
-  if (!["admin", "write"].includes(permission)) throw new Error(`User ${actor} does not have write permissions`)
+  if (!["admin", "write"].includes(permission))
+    throw new Error(`User ${actor} does not have write permissions`)
 }
 
 async function updateComment(body: string) {
@@ -730,12 +747,13 @@ async function updateComment(body: string) {
 async function createPR(base: string, branch: string, title: string, body: string) {
   console.log("Creating pull request...")
   const { repo } = useContext()
+  const truncatedTitle = title.length > 256 ? title.slice(0, 253) + "..." : title
   const pr = await octoRest.rest.pulls.create({
     owner: repo.owner,
     repo: repo.repo,
     head: branch,
     base,
-    title,
+    title: truncatedTitle,
     body,
   })
   return pr.data.number
@@ -753,7 +771,9 @@ function footer(opts?: { image?: boolean }) {
 
     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`
   })()
-  const shareUrl = shareId ? `[opencode session](${useShareUrl()}/s/${shareId})&nbsp;&nbsp;|&nbsp;&nbsp;` : ""
+  const shareUrl = shareId
+    ? `[opencode session](${useShareUrl()}/s/${shareId})&nbsp;&nbsp;|&nbsp;&nbsp;`
+    : ""
   return `\n\n${image}${shareUrl}[github run](${useEnvRunUrl()})`
 }
 
@@ -936,9 +956,13 @@ function buildPromptDataForPR(pr: GitHubPullRequest) {
     })
     .map((c) => `- ${c.author.login} at ${c.createdAt}: ${c.body}`)
 
-  const files = (pr.files.nodes || []).map((f) => `- ${f.path} (${f.changeType}) +${f.additions}/-${f.deletions}`)
+  const files = (pr.files.nodes || []).map(
+    (f) => `- ${f.path} (${f.changeType}) +${f.additions}/-${f.deletions}`,
+  )
   const reviewData = (pr.reviews.nodes || []).map((r) => {
-    const comments = (r.comments.nodes || []).map((c) => `    - ${c.path}:${c.line ?? "?"}: ${c.body}`)
+    const comments = (r.comments.nodes || []).map(
+      (c) => `    - ${c.path}:${c.line ?? "?"}: ${c.body}`,
+    )
     return [
       `- ${r.author.login} at ${r.submittedAt}:`,
       `  - Review body: ${r.body}`,
@@ -960,9 +984,15 @@ function buildPromptDataForPR(pr: GitHubPullRequest) {
     `Deletions: ${pr.deletions}`,
     `Total Commits: ${pr.commits.totalCount}`,
     `Changed Files: ${pr.files.nodes.length} files`,
-    ...(comments.length > 0 ? ["<pull_request_comments>", ...comments, "</pull_request_comments>"] : []),
-    ...(files.length > 0 ? ["<pull_request_changed_files>", ...files, "</pull_request_changed_files>"] : []),
-    ...(reviewData.length > 0 ? ["<pull_request_reviews>", ...reviewData, "</pull_request_reviews>"] : []),
+    ...(comments.length > 0
+      ? ["<pull_request_comments>", ...comments, "</pull_request_comments>"]
+      : []),
+    ...(files.length > 0
+      ? ["<pull_request_changed_files>", ...files, "</pull_request_changed_files>"]
+      : []),
+    ...(reviewData.length > 0
+      ? ["<pull_request_reviews>", ...reviewData, "</pull_request_reviews>"]
+      : []),
     "</pull_request>",
   ].join("\n")
 }