@@ -127,6 +127,7 @@ type IssueQueryResponse = { |
127 | 127 | const AGENT_USERNAME = "opencode-agent[bot]" |
128 | 128 | const AGENT_REACTION = "eyes" |
129 | 129 | const WORKFLOW_FILE = ".github/workflows/opencode.yml" |
| 130 | +const SUPPORTED_EVENTS = ["issue_comment", "pull_request_review_comment", "schedule"] as const |
130 | 131 |
|
131 | 132 | // Parses GitHub remote URLs in various formats: |
132 | 133 | // - https://github.com/owner/repo.git |
@@ -387,22 +388,27 @@ export const GithubRunCommand = cmd({ |
387 | 388 | const isMock = args.token || args.event |
388 | 389 |
|
389 | 390 | const context = isMock ? (JSON.parse(args.event!) as Context) : github.context |
390 | | - if (context.eventName !== "issue_comment" && context.eventName !== "pull_request_review_comment") { |
| 391 | + if (!SUPPORTED_EVENTS.includes(context.eventName as (typeof SUPPORTED_EVENTS)[number])) { |
391 | 392 | core.setFailed(`Unsupported event type: ${context.eventName}`) |
392 | 393 | process.exit(1) |
393 | 394 | } |
| 395 | + const isScheduleEvent = context.eventName === "schedule" |
394 | 396 |
|
395 | 397 | const { providerID, modelID } = normalizeModel() |
396 | 398 | const runId = normalizeRunId() |
397 | 399 | const share = normalizeShare() |
398 | 400 | const oidcBaseUrl = normalizeOidcBaseUrl() |
399 | 401 | const { owner, repo } = context.repo |
400 | | - const payload = context.payload as IssueCommentEvent | PullRequestReviewCommentEvent |
401 | | - const issueEvent = isIssueCommentEvent(payload) ? payload : undefined |
402 | | - const actor = context.actor |
403 | | - |
404 | | - const issueId = |
405 | | - context.eventName === "pull_request_review_comment" |
| 402 | + // For schedule events, payload has no issue/comment data |
| 403 | + const payload = isScheduleEvent |
| 404 | + ? undefined |
| 405 | + : (context.payload as IssueCommentEvent | PullRequestReviewCommentEvent) |
| 406 | + const issueEvent = payload && isIssueCommentEvent(payload) ? payload : undefined |
| 407 | + const actor = isScheduleEvent ? undefined : context.actor |
| 408 | + |
| 409 | + const issueId = isScheduleEvent |
| 410 | + ? undefined |
| 411 | + : context.eventName === "pull_request_review_comment" |
406 | 412 | ? (payload as PullRequestReviewCommentEvent).pull_request.number |
407 | 413 | : (payload as IssueCommentEvent).issue.number |
408 | 414 | const runUrl = `/${owner}/${repo}/actions/runs/${runId}` |
@@ -416,9 +422,13 @@ export const GithubRunCommand = cmd({ |
416 | 422 | let shareId: string | undefined |
417 | 423 | let exitCode = 0 |
418 | 424 | type PromptFiles = Awaited<ReturnType<typeof getUserPrompt>>["promptFiles"] |
419 | | - const triggerCommentId = payload.comment.id |
| 425 | + const triggerCommentId = payload?.comment.id |
420 | 426 | const useGithubToken = normalizeUseGithubToken() |
421 | | - const commentType = context.eventName === "pull_request_review_comment" ? "pr_review" : "issue" |
| 427 | + const commentType = isScheduleEvent |
| 428 | + ? undefined |
| 429 | + : context.eventName === "pull_request_review_comment" |
| 430 | + ? "pr_review" |
| 431 | + : "issue" |
422 | 432 |
|
423 | 433 | try { |
424 | 434 | if (useGithubToken) { |
@@ -442,9 +452,11 @@ export const GithubRunCommand = cmd({ |
442 | 452 | if (!useGithubToken) { |
443 | 453 | await configureGit(appToken) |
444 | 454 | } |
445 | | - await assertPermissions() |
446 | | - |
447 | | - await addReaction(commentType) |
| 455 | + // Skip permission check for schedule events (no actor to check) |
| 456 | + if (!isScheduleEvent) { |
| 457 | + await assertPermissions() |
| 458 | + await addReaction(commentType!) |
| 459 | + } |
448 | 460 |
|
449 | 461 | // Setup opencode session |
450 | 462 | const repoData = await fetchRepo() |
@@ -458,11 +470,31 @@ export const GithubRunCommand = cmd({ |
458 | 470 | })() |
459 | 471 | console.log("opencode session", session.id) |
460 | 472 |
|
461 | | - // Handle 3 cases |
462 | | - // 1. Issue |
463 | | - // 2. Local PR |
464 | | - // 3. Fork PR |
465 | | - if (context.eventName === "pull_request_review_comment" || issueEvent?.issue.pull_request) { |
| 473 | + // Handle 4 cases |
| 474 | + // 1. Schedule (no issue/PR context) |
| 475 | + // 2. Issue |
| 476 | + // 3. Local PR |
| 477 | + // 4. Fork PR |
| 478 | + if (isScheduleEvent) { |
| 479 | + // Schedule event - no issue/PR context, output goes to logs |
| 480 | + const branch = await checkoutNewBranch("schedule") |
| 481 | + const head = (await $`git rev-parse HEAD`).stdout.toString().trim() |
| 482 | + const response = await chat(userPrompt, promptFiles) |
| 483 | + const { dirty, uncommittedChanges } = await branchIsDirty(head) |
| 484 | + if (dirty) { |
| 485 | + const summary = await summarize(response) |
| 486 | + await pushToNewBranch(summary, branch, uncommittedChanges, true) |
| 487 | + const pr = await createPR( |
| 488 | + repoData.data.default_branch, |
| 489 | + branch, |
| 490 | + summary, |
| 491 | + `${response}\n\nTriggered by scheduled workflow${footer({ image: true })}`, |
| 492 | + ) |
| 493 | + console.log(`Created PR #${pr}`) |
| 494 | + } else { |
| 495 | + console.log("Response:", response) |
| 496 | + } |
| 497 | + } else if (context.eventName === "pull_request_review_comment" || issueEvent?.issue.pull_request) { |
466 | 498 | const prData = await fetchPR() |
467 | 499 | // Local PR |
468 | 500 | if (prData.headRepository.nameWithOwner === prData.baseRepository.nameWithOwner) { |
@@ -477,7 +509,7 @@ export const GithubRunCommand = cmd({ |
477 | 509 | } |
478 | 510 | const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${shareBaseUrl}/s/${shareId}`)) |
479 | 511 | await createComment(`${response}${footer({ image: !hasShared })}`) |
480 | | - await removeReaction(commentType) |
| 512 | + await removeReaction(commentType!) |
481 | 513 | } |
482 | 514 | // Fork PR |
483 | 515 | else { |
@@ -492,31 +524,31 @@ export const GithubRunCommand = cmd({ |
492 | 524 | } |
493 | 525 | const hasShared = prData.comments.nodes.some((c) => c.body.includes(`${shareBaseUrl}/s/${shareId}`)) |
494 | 526 | await createComment(`${response}${footer({ image: !hasShared })}`) |
495 | | - await removeReaction(commentType) |
| 527 | + await removeReaction(commentType!) |
496 | 528 | } |
497 | 529 | } |
498 | 530 | // Issue |
499 | 531 | else { |
500 | | - const branch = await checkoutNewBranch() |
| 532 | + const branch = await checkoutNewBranch("issue") |
501 | 533 | const head = (await $`git rev-parse HEAD`).stdout.toString().trim() |
502 | 534 | const issueData = await fetchIssue() |
503 | 535 | const dataPrompt = buildPromptDataForIssue(issueData) |
504 | 536 | const response = await chat(`${userPrompt}\n\n${dataPrompt}`, promptFiles) |
505 | 537 | const { dirty, uncommittedChanges } = await branchIsDirty(head) |
506 | 538 | if (dirty) { |
507 | 539 | const summary = await summarize(response) |
508 | | - await pushToNewBranch(summary, branch, uncommittedChanges) |
| 540 | + await pushToNewBranch(summary, branch, uncommittedChanges, false) |
509 | 541 | const pr = await createPR( |
510 | 542 | repoData.data.default_branch, |
511 | 543 | branch, |
512 | 544 | summary, |
513 | 545 | `${response}\n\nCloses #${issueId}${footer({ image: true })}`, |
514 | 546 | ) |
515 | 547 | await createComment(`Created PR #${pr}${footer({ image: true })}`) |
516 | | - await removeReaction(commentType) |
| 548 | + await removeReaction(commentType!) |
517 | 549 | } else { |
518 | 550 | await createComment(`${response}${footer({ image: true })}`) |
519 | | - await removeReaction(commentType) |
| 551 | + await removeReaction(commentType!) |
520 | 552 | } |
521 | 553 | } |
522 | 554 | } catch (e: any) { |
@@ -528,8 +560,10 @@ export const GithubRunCommand = cmd({ |
528 | 560 | } else if (e instanceof Error) { |
529 | 561 | msg = e.message |
530 | 562 | } |
531 | | - await createComment(`${msg}${footer()}`) |
532 | | - await removeReaction(commentType) |
| 563 | + if (!isScheduleEvent) { |
| 564 | + await createComment(`${msg}${footer()}`) |
| 565 | + await removeReaction(commentType!) |
| 566 | + } |
533 | 567 | core.setFailed(msg) |
534 | 568 | // Also output the clean error message for the action to capture |
535 | 569 | //core.setOutput("prepare_error", e.message); |
@@ -605,6 +639,14 @@ export const GithubRunCommand = cmd({ |
605 | 639 |
|
606 | 640 | async function getUserPrompt() { |
607 | 641 | const customPrompt = process.env["PROMPT"] |
| 642 | + // For schedule events, PROMPT is required since there's no comment to extract from |
| 643 | + if (isScheduleEvent) { |
| 644 | + if (!customPrompt) { |
| 645 | + throw new Error("PROMPT input is required for scheduled events") |
| 646 | + } |
| 647 | + return { userPrompt: customPrompt, promptFiles: [] } |
| 648 | + } |
| 649 | + |
608 | 650 | if (customPrompt) { |
609 | 651 | return { userPrompt: customPrompt, promptFiles: [] } |
610 | 652 | } |
@@ -615,7 +657,7 @@ export const GithubRunCommand = cmd({ |
615 | 657 | .map((m) => m.trim().toLowerCase()) |
616 | 658 | .filter(Boolean) |
617 | 659 | let prompt = (() => { |
618 | | - const body = payload.comment.body.trim() |
| 660 | + const body = payload!.comment.body.trim() |
619 | 661 | const bodyLower = body.toLowerCase() |
620 | 662 | if (mentions.some((m) => bodyLower === m)) { |
621 | 663 | if (reviewContext) { |
@@ -865,9 +907,9 @@ export const GithubRunCommand = cmd({ |
865 | 907 | await $`git config --local ${config} "${gitConfig}"` |
866 | 908 | } |
867 | 909 |
|
868 | | - async function checkoutNewBranch() { |
| 910 | + async function checkoutNewBranch(type: "issue" | "schedule") { |
869 | 911 | console.log("Checking out new branch...") |
870 | | - const branch = generateBranchName("issue") |
| 912 | + const branch = generateBranchName(type) |
871 | 913 | await $`git checkout -b ${branch}` |
872 | 914 | return branch |
873 | 915 | } |
@@ -894,23 +936,32 @@ export const GithubRunCommand = cmd({ |
894 | 936 | await $`git checkout -b ${localBranch} fork/${remoteBranch}` |
895 | 937 | } |
896 | 938 |
|
897 | | - function generateBranchName(type: "issue" | "pr") { |
| 939 | + function generateBranchName(type: "issue" | "pr" | "schedule") { |
898 | 940 | const timestamp = new Date() |
899 | 941 | .toISOString() |
900 | 942 | .replace(/[:-]/g, "") |
901 | 943 | .replace(/\.\d{3}Z/, "") |
902 | 944 | .split("T") |
903 | 945 | .join("") |
| 946 | + if (type === "schedule") { |
| 947 | + const hex = crypto.randomUUID().slice(0, 6) |
| 948 | + return `opencode/scheduled-${hex}-${timestamp}` |
| 949 | + } |
904 | 950 | return `opencode/${type}${issueId}-${timestamp}` |
905 | 951 | } |
906 | 952 |
|
907 | | - async function pushToNewBranch(summary: string, branch: string, commit: boolean) { |
| 953 | + async function pushToNewBranch(summary: string, branch: string, commit: boolean, isSchedule: boolean) { |
908 | 954 | console.log("Pushing to new branch...") |
909 | 955 | if (commit) { |
910 | 956 | await $`git add .` |
911 | | - await $`git commit -m "${summary} |
| 957 | + if (isSchedule) { |
| 958 | + // No co-author for scheduled events - the schedule is operating as the repo |
| 959 | + await $`git commit -m "${summary}"` |
| 960 | + } else { |
| 961 | + await $`git commit -m "${summary} |
912 | 962 |
|
913 | 963 | Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` |
| 964 | + } |
914 | 965 | } |
915 | 966 | await $`git push -u origin ${branch}` |
916 | 967 | } |
@@ -958,14 +1009,15 @@ Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` |
958 | 1009 | } |
959 | 1010 |
|
960 | 1011 | async function assertPermissions() { |
| 1012 | + // Only called for non-schedule events, so actor is defined |
961 | 1013 | console.log(`Asserting permissions for user ${actor}...`) |
962 | 1014 |
|
963 | 1015 | let permission |
964 | 1016 | try { |
965 | 1017 | const response = await octoRest.repos.getCollaboratorPermissionLevel({ |
966 | 1018 | owner, |
967 | 1019 | repo, |
968 | | - username: actor, |
| 1020 | + username: actor!, |
969 | 1021 | }) |
970 | 1022 |
|
971 | 1023 | permission = response.data.permission |
@@ -979,30 +1031,32 @@ Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` |
979 | 1031 | } |
980 | 1032 |
|
981 | 1033 | async function addReaction(commentType: "issue" | "pr_review") { |
| 1034 | + // Only called for non-schedule events, so triggerCommentId is defined |
982 | 1035 | console.log("Adding reaction...") |
983 | 1036 | if (commentType === "pr_review") { |
984 | 1037 | return await octoRest.rest.reactions.createForPullRequestReviewComment({ |
985 | 1038 | owner, |
986 | 1039 | repo, |
987 | | - comment_id: triggerCommentId, |
| 1040 | + comment_id: triggerCommentId!, |
988 | 1041 | content: AGENT_REACTION, |
989 | 1042 | }) |
990 | 1043 | } |
991 | 1044 | return await octoRest.rest.reactions.createForIssueComment({ |
992 | 1045 | owner, |
993 | 1046 | repo, |
994 | | - comment_id: triggerCommentId, |
| 1047 | + comment_id: triggerCommentId!, |
995 | 1048 | content: AGENT_REACTION, |
996 | 1049 | }) |
997 | 1050 | } |
998 | 1051 |
|
999 | 1052 | async function removeReaction(commentType: "issue" | "pr_review") { |
| 1053 | + // Only called for non-schedule events, so triggerCommentId is defined |
1000 | 1054 | console.log("Removing reaction...") |
1001 | 1055 | if (commentType === "pr_review") { |
1002 | 1056 | const reactions = await octoRest.rest.reactions.listForPullRequestReviewComment({ |
1003 | 1057 | owner, |
1004 | 1058 | repo, |
1005 | | - comment_id: triggerCommentId, |
| 1059 | + comment_id: triggerCommentId!, |
1006 | 1060 | content: AGENT_REACTION, |
1007 | 1061 | }) |
1008 | 1062 |
|
@@ -1012,7 +1066,7 @@ Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` |
1012 | 1066 | await octoRest.rest.reactions.deleteForPullRequestComment({ |
1013 | 1067 | owner, |
1014 | 1068 | repo, |
1015 | | - comment_id: triggerCommentId, |
| 1069 | + comment_id: triggerCommentId!, |
1016 | 1070 | reaction_id: eyesReaction.id, |
1017 | 1071 | }) |
1018 | 1072 | return |
@@ -1021,7 +1075,7 @@ Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` |
1021 | 1075 | const reactions = await octoRest.rest.reactions.listForIssueComment({ |
1022 | 1076 | owner, |
1023 | 1077 | repo, |
1024 | | - comment_id: triggerCommentId, |
| 1078 | + comment_id: triggerCommentId!, |
1025 | 1079 | content: AGENT_REACTION, |
1026 | 1080 | }) |
1027 | 1081 |
|
@@ -1031,17 +1085,18 @@ Co-authored-by: ${actor} <${actor}@users.noreply.github.com>"` |
1031 | 1085 | await octoRest.rest.reactions.deleteForIssueComment({ |
1032 | 1086 | owner, |
1033 | 1087 | repo, |
1034 | | - comment_id: triggerCommentId, |
| 1088 | + comment_id: triggerCommentId!, |
1035 | 1089 | reaction_id: eyesReaction.id, |
1036 | 1090 | }) |
1037 | 1091 | } |
1038 | 1092 |
|
1039 | 1093 | async function createComment(body: string) { |
| 1094 | + // Only called for non-schedule events, so issueId is defined |
1040 | 1095 | console.log("Creating comment...") |
1041 | 1096 | return await octoRest.rest.issues.createComment({ |
1042 | 1097 | owner, |
1043 | 1098 | repo, |
1044 | | - issue_number: issueId, |
| 1099 | + issue_number: issueId!, |
1045 | 1100 | body, |
1046 | 1101 | }) |
1047 | 1102 | } |
@@ -1119,10 +1174,11 @@ query($owner: String!, $repo: String!, $number: Int!) { |
1119 | 1174 | } |
1120 | 1175 |
|
1121 | 1176 | function buildPromptDataForIssue(issue: GitHubIssue) { |
| 1177 | + // Only called for non-schedule events, so payload is defined |
1122 | 1178 | const comments = (issue.comments?.nodes || []) |
1123 | 1179 | .filter((c) => { |
1124 | 1180 | const id = parseInt(c.databaseId) |
1125 | | - return id !== payload.comment.id |
| 1181 | + return id !== payload!.comment.id |
1126 | 1182 | }) |
1127 | 1183 | .map((c) => ` - ${c.author.login} at ${c.createdAt}: ${c.body}`) |
1128 | 1184 |
|
@@ -1246,10 +1302,11 @@ query($owner: String!, $repo: String!, $number: Int!) { |
1246 | 1302 | } |
1247 | 1303 |
|
1248 | 1304 | function buildPromptDataForPR(pr: GitHubPullRequest) { |
| 1305 | + // Only called for non-schedule events, so payload is defined |
1249 | 1306 | const comments = (pr.comments?.nodes || []) |
1250 | 1307 | .filter((c) => { |
1251 | 1308 | const id = parseInt(c.databaseId) |
1252 | | - return id !== payload.comment.id |
| 1309 | + return id !== payload!.comment.id |
1253 | 1310 | }) |
1254 | 1311 | .map((c) => `- ${c.author.login} at ${c.createdAt}: ${c.body}`) |
1255 | 1312 |
|
|