commit-push-pr.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import type { Command } from '../commands.js'
  2. import {
  3. getAttributionTexts,
  4. getEnhancedPRAttribution,
  5. } from '../utils/attribution.js'
  6. import { getDefaultBranch } from '../utils/git.js'
  7. import { executeShellCommandsInPrompt } from '../utils/promptShellExecution.js'
  8. import { getUndercoverInstructions, isUndercover } from '../utils/undercover.js'
  9. const ALLOWED_TOOLS = [
  10. 'Bash(git checkout --branch:*)',
  11. 'Bash(git checkout -b:*)',
  12. 'Bash(git add:*)',
  13. 'Bash(git status:*)',
  14. 'Bash(git push:*)',
  15. 'Bash(git commit:*)',
  16. 'Bash(gh pr create:*)',
  17. 'Bash(gh pr edit:*)',
  18. 'Bash(gh pr view:*)',
  19. 'Bash(gh pr merge:*)',
  20. 'ToolSearch',
  21. 'mcp__slack__send_message',
  22. 'mcp__claude_ai_Slack__slack_send_message',
  23. ]
  24. function getPromptContent(
  25. defaultBranch: string,
  26. prAttribution?: string,
  27. ): string {
  28. const { commit: commitAttribution, pr: defaultPrAttribution } =
  29. getAttributionTexts()
  30. // Use provided PR attribution or fall back to default
  31. const effectivePrAttribution = prAttribution ?? defaultPrAttribution
  32. const safeUser = process.env.SAFEUSER || ''
  33. const username = process.env.USER || ''
  34. let prefix = ''
  35. let reviewerArg = ' and `--reviewer anthropics/claude-code`'
  36. let addReviewerArg = ' (and add `--add-reviewer anthropics/claude-code`)'
  37. let changelogSection = `
  38. ## Changelog
  39. <!-- CHANGELOG:START -->
  40. [If this PR contains user-facing changes, add a changelog entry here. Otherwise, remove this section.]
  41. <!-- CHANGELOG:END -->`
  42. let slackStep = `
  43. 5. After creating/updating the PR, check if the user's CLAUDE.md mentions posting to Slack channels. If it does, use ToolSearch to search for "slack send message" tools. If ToolSearch finds a Slack tool, ask the user if they'd like you to post the PR URL to the relevant Slack channel. Only post if the user confirms. If ToolSearch returns no results or errors, skip this step silently—do not mention the failure, do not attempt workarounds, and do not try alternative approaches.`
  44. if (process.env.USER_TYPE === 'ant' && isUndercover()) {
  45. prefix = getUndercoverInstructions() + '\n'
  46. reviewerArg = ''
  47. addReviewerArg = ''
  48. changelogSection = ''
  49. slackStep = ''
  50. }
  51. return `${prefix}## Context
  52. - \`SAFEUSER\`: ${safeUser}
  53. - \`whoami\`: ${username}
  54. - \`git status\`: !\`git status\`
  55. - \`git diff HEAD\`: !\`git diff HEAD\`
  56. - \`git branch --show-current\`: !\`git branch --show-current\`
  57. - \`git diff ${defaultBranch}...HEAD\`: !\`git diff ${defaultBranch}...HEAD\`
  58. - \`gh pr view --json number 2>/dev/null || true\`: !\`gh pr view --json number 2>/dev/null || true\`
  59. ## Git Safety Protocol
  60. - NEVER update the git config
  61. - NEVER run destructive/irreversible git commands (like push --force, hard reset, etc) unless the user explicitly requests them
  62. - NEVER skip hooks (--no-verify, --no-gpg-sign, etc) unless the user explicitly requests it
  63. - NEVER run force push to main/master, warn the user if they request it
  64. - Do not commit files that likely contain secrets (.env, credentials.json, etc)
  65. - Never use git commands with the -i flag (like git rebase -i or git add -i) since they require interactive input which is not supported
  66. ## Your task
  67. Analyze all changes that will be included in the pull request, making sure to look at all relevant commits (NOT just the latest commit, but ALL commits that will be included in the pull request from the git diff ${defaultBranch}...HEAD output above).
  68. Based on the above changes:
  69. 1. Create a new branch if on ${defaultBranch} (use SAFEUSER from context above for the branch name prefix, falling back to whoami if SAFEUSER is empty, e.g., \`username/feature-name\`)
  70. 2. Create a single commit with an appropriate message using heredoc syntax${commitAttribution ? `, ending with the attribution text shown in the example below` : ''}:
  71. \`\`\`
  72. git commit -m "$(cat <<'EOF'
  73. Commit message here.${commitAttribution ? `\n\n${commitAttribution}` : ''}
  74. EOF
  75. )"
  76. \`\`\`
  77. 3. Push the branch to origin
  78. 4. If a PR already exists for this branch (check the gh pr view output above), update the PR title and body using \`gh pr edit\` to reflect the current diff${addReviewerArg}. Otherwise, create a pull request using \`gh pr create\` with heredoc syntax for the body${reviewerArg}.
  79. - IMPORTANT: Keep PR titles short (under 70 characters). Use the body for details.
  80. \`\`\`
  81. gh pr create --title "Short, descriptive title" --body "$(cat <<'EOF'
  82. ## Summary
  83. <1-3 bullet points>
  84. ## Test plan
  85. [Bulleted markdown checklist of TODOs for testing the pull request...]${changelogSection}${effectivePrAttribution ? `\n\n${effectivePrAttribution}` : ''}
  86. EOF
  87. )"
  88. \`\`\`
  89. You have the capability to call multiple tools in a single response. You MUST do all of the above in a single message.${slackStep}
  90. Return the PR URL when you're done, so the user can see it.`
  91. }
  92. const command = {
  93. type: 'prompt',
  94. name: 'commit-push-pr',
  95. description: 'Commit, push, and open a PR',
  96. allowedTools: ALLOWED_TOOLS,
  97. get contentLength() {
  98. // Use 'main' as estimate for content length calculation
  99. return getPromptContent('main').length
  100. },
  101. progressMessage: 'creating commit and PR',
  102. source: 'builtin',
  103. async getPromptForCommand(args, context) {
  104. // Get default branch and enhanced PR attribution
  105. const [defaultBranch, prAttribution] = await Promise.all([
  106. getDefaultBranch(),
  107. getEnhancedPRAttribution(context.getAppState),
  108. ])
  109. let promptContent = getPromptContent(defaultBranch, prAttribution)
  110. // Append user instructions if args provided
  111. const trimmedArgs = args?.trim()
  112. if (trimmedArgs) {
  113. promptContent += `\n\n## Additional instructions from user\n\n${trimmedArgs}`
  114. }
  115. const finalContent = await executeShellCommandsInPrompt(
  116. promptContent,
  117. {
  118. ...context,
  119. getAppState() {
  120. const appState = context.getAppState()
  121. return {
  122. ...appState,
  123. toolPermissionContext: {
  124. ...appState.toolPermissionContext,
  125. alwaysAllowRules: {
  126. ...appState.toolPermissionContext.alwaysAllowRules,
  127. command: ALLOWED_TOOLS,
  128. },
  129. },
  130. }
  131. },
  132. },
  133. '/commit-push-pr',
  134. )
  135. return [{ type: 'text', text: finalContent }]
  136. },
  137. } satisfies Command
  138. export default command