openhands-resolver.yml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. name: Auto-Fix Tagged Issue with OpenHands
  2. on:
  3. workflow_call:
  4. inputs:
  5. max_iterations:
  6. required: false
  7. type: number
  8. default: 50
  9. macro:
  10. required: false
  11. type: string
  12. default: "@openhands-agent"
  13. target_branch:
  14. required: false
  15. type: string
  16. default: "main"
  17. description: "Target branch to pull and create PR against"
  18. LLM_MODEL:
  19. required: false
  20. type: string
  21. default: "anthropic/claude-3-5-sonnet-20241022"
  22. base_container_image:
  23. required: false
  24. type: string
  25. default: ""
  26. description: "Custom sandbox env"
  27. secrets:
  28. LLM_MODEL:
  29. required: false
  30. LLM_API_KEY:
  31. required: true
  32. LLM_BASE_URL:
  33. required: false
  34. PAT_TOKEN:
  35. required: false
  36. PAT_USERNAME:
  37. required: false
  38. issues:
  39. types: [labeled]
  40. pull_request:
  41. types: [labeled]
  42. issue_comment:
  43. types: [created]
  44. pull_request_review_comment:
  45. types: [created]
  46. pull_request_review:
  47. types: [submitted]
  48. permissions:
  49. contents: write
  50. pull-requests: write
  51. issues: write
  52. jobs:
  53. auto-fix:
  54. if: |
  55. github.event_name == 'workflow_call' ||
  56. github.event.label.name == 'fix-me' ||
  57. github.event.label.name == 'fix-me-experimental' ||
  58. (
  59. ((github.event_name == 'issue_comment' || github.event_name == 'pull_request_review_comment') &&
  60. contains(github.event.comment.body, inputs.macro || '@openhands-agent') &&
  61. (github.event.comment.author_association == 'OWNER' || github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'MEMBER')
  62. ) ||
  63. (github.event_name == 'pull_request_review' &&
  64. contains(github.event.review.body, inputs.macro || '@openhands-agent') &&
  65. (github.event.review.author_association == 'OWNER' || github.event.review.author_association == 'COLLABORATOR' || github.event.review.author_association == 'MEMBER')
  66. )
  67. )
  68. runs-on: ubuntu-latest
  69. steps:
  70. - name: Checkout repository
  71. uses: actions/checkout@v4
  72. - name: Set up Python
  73. uses: actions/setup-python@v5
  74. with:
  75. python-version: "3.12"
  76. - name: Get latest versions and create requirements.txt
  77. run: |
  78. python -m pip index versions openhands-ai > openhands_versions.txt
  79. OPENHANDS_VERSION=$(head -n 1 openhands_versions.txt | awk '{print $2}' | tr -d '()')
  80. echo "openhands-ai==${OPENHANDS_VERSION}" >> requirements.txt
  81. cat requirements.txt
  82. - name: Cache pip dependencies
  83. if: |
  84. !(
  85. github.event.label.name == 'fix-me-experimental' ||
  86. (
  87. (github.event_name == 'issue_comment' || github.event_name == 'pull_request_review_comment') &&
  88. contains(github.event.comment.body, '@openhands-agent-exp')
  89. ) ||
  90. (
  91. github.event_name == 'pull_request_review' &&
  92. contains(github.event.review.body, '@openhands-agent-exp')
  93. )
  94. )
  95. uses: actions/cache@v3
  96. with:
  97. path: ${{ env.pythonLocation }}/lib/python3.12/site-packages/*
  98. key: ${{ runner.os }}-pip-openhands-resolver-${{ hashFiles('requirements.txt') }}
  99. restore-keys: |
  100. ${{ runner.os }}-pip-openhands-resolver-${{ hashFiles('requirements.txt') }}
  101. - name: Check required environment variables
  102. env:
  103. LLM_MODEL: ${{ secrets.LLM_MODEL || inputs.LLM_MODEL }}
  104. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  105. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  106. PAT_TOKEN: ${{ secrets.PAT_TOKEN }}
  107. PAT_USERNAME: ${{ secrets.PAT_USERNAME }}
  108. GITHUB_TOKEN: ${{ github.token }}
  109. run: |
  110. required_vars=("LLM_API_KEY")
  111. for var in "${required_vars[@]}"; do
  112. if [ -z "${!var}" ]; then
  113. echo "Error: Required environment variable $var is not set."
  114. exit 1
  115. fi
  116. done
  117. # Check optional variables and warn about fallbacks
  118. if [ -z "$LLM_BASE_URL" ]; then
  119. echo "Warning: LLM_BASE_URL is not set, will use default API endpoint"
  120. fi
  121. if [ -z "$PAT_TOKEN" ]; then
  122. echo "Warning: PAT_TOKEN is not set, falling back to GITHUB_TOKEN"
  123. fi
  124. if [ -z "$PAT_USERNAME" ]; then
  125. echo "Warning: PAT_USERNAME is not set, will use openhands-agent"
  126. fi
  127. - name: Set environment variables
  128. run: |
  129. # Handle pull request events first
  130. if [ -n "${{ github.event.pull_request.number }}" ]; then
  131. echo "ISSUE_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
  132. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  133. # Handle pull request review events
  134. elif [ -n "${{ github.event.review.body }}" ]; then
  135. echo "ISSUE_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
  136. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  137. # Handle issue comment events that reference a PR
  138. elif [ -n "${{ github.event.issue.pull_request }}" ]; then
  139. echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
  140. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  141. # Handle regular issue events
  142. else
  143. echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
  144. echo "ISSUE_TYPE=issue" >> $GITHUB_ENV
  145. fi
  146. if [ -n "${{ github.event.review.body }}" ]; then
  147. echo "COMMENT_ID=${{ github.event.review.id || 'None' }}" >> $GITHUB_ENV
  148. else
  149. echo "COMMENT_ID=${{ github.event.comment.id || 'None' }}" >> $GITHUB_ENV
  150. fi
  151. echo "MAX_ITERATIONS=${{ inputs.max_iterations || 50 }}" >> $GITHUB_ENV
  152. echo "SANDBOX_ENV_GITHUB_TOKEN=${{ secrets.PAT_TOKEN || github.token }}" >> $GITHUB_ENV
  153. echo "SANDBOX_ENV_BASE_CONTAINER_IMAGE=${{ inputs.base_container_image }}" >> $GITHUB_ENV
  154. # Set branch variables
  155. echo "TARGET_BRANCH=${{ inputs.target_branch }}" >> $GITHUB_ENV
  156. - name: Comment on issue with start message
  157. uses: actions/github-script@v7
  158. with:
  159. github-token: ${{ secrets.PAT_TOKEN || github.token }}
  160. script: |
  161. const issueType = process.env.ISSUE_TYPE;
  162. github.rest.issues.createComment({
  163. issue_number: ${{ env.ISSUE_NUMBER }},
  164. owner: context.repo.owner,
  165. repo: context.repo.repo,
  166. body: `[OpenHands](https://github.com/All-Hands-AI/OpenHands) started fixing the ${issueType}! You can monitor the progress [here](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}).`
  167. });
  168. - name: Install OpenHands
  169. uses: actions/github-script@v7
  170. with:
  171. script: |
  172. const commentBody = `${{ github.event.comment.body || '' }}`.trim();
  173. const reviewBody = `${{ github.event.review.body || '' }}`.trim();
  174. const labelName = `${{ github.event.label.name || '' }}`.trim();
  175. const eventName = `${{ github.event_name }}`.trim();
  176. // Check conditions
  177. const isExperimentalLabel = labelName === "fix-me-experimental";
  178. const isIssueCommentExperimental =
  179. (eventName === "issue_comment" || eventName === "pull_request_review_comment") &&
  180. commentBody.includes("@openhands-agent-exp");
  181. const isReviewCommentExperimental =
  182. eventName === "pull_request_review" && reviewBody.includes("@openhands-agent-exp");
  183. // Perform package installation
  184. if (isExperimentalLabel || isIssueCommentExperimental || isReviewCommentExperimental) {
  185. console.log("Installing experimental OpenHands...");
  186. await exec.exec("python -m pip install --upgrade pip");
  187. await exec.exec("pip install git+https://github.com/all-hands-ai/openhands.git");
  188. } else {
  189. console.log("Installing from requirements.txt...");
  190. await exec.exec("python -m pip install --upgrade pip");
  191. await exec.exec("pip install -r requirements.txt");
  192. }
  193. - name: Attempt to resolve issue
  194. env:
  195. GITHUB_TOKEN: ${{ secrets.PAT_TOKEN || github.token }}
  196. GITHUB_USERNAME: ${{ secrets.PAT_USERNAME || 'openhands-agent' }}
  197. LLM_MODEL: ${{ secrets.LLM_MODEL || inputs.LLM_MODEL }}
  198. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  199. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  200. PYTHONPATH: ""
  201. run: |
  202. cd /tmp && python -m openhands.resolver.resolve_issue \
  203. --repo ${{ github.repository }} \
  204. --issue-number ${{ env.ISSUE_NUMBER }} \
  205. --issue-type ${{ env.ISSUE_TYPE }} \
  206. --max-iterations ${{ env.MAX_ITERATIONS }} \
  207. --comment-id ${{ env.COMMENT_ID }}
  208. - name: Check resolution result
  209. id: check_result
  210. run: |
  211. if cd /tmp && grep -q '"success":true' output/output.jsonl; then
  212. echo "RESOLUTION_SUCCESS=true" >> $GITHUB_OUTPUT
  213. else
  214. echo "RESOLUTION_SUCCESS=false" >> $GITHUB_OUTPUT
  215. fi
  216. - name: Upload output.jsonl as artifact
  217. uses: actions/upload-artifact@v4
  218. if: always() # Upload even if the previous steps fail
  219. with:
  220. name: resolver-output
  221. path: /tmp/output/output.jsonl
  222. retention-days: 30 # Keep the artifact for 30 days
  223. - name: Create draft PR or push branch
  224. if: always() # Create PR or branch even if the previous steps fail
  225. env:
  226. GITHUB_TOKEN: ${{ secrets.PAT_TOKEN || github.token }}
  227. GITHUB_USERNAME: ${{ secrets.PAT_USERNAME || 'openhands-agent' }}
  228. LLM_MODEL: ${{ secrets.LLM_MODEL || inputs.LLM_MODEL }}
  229. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  230. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  231. PYTHONPATH: ""
  232. run: |
  233. if [ "${{ steps.check_result.outputs.RESOLUTION_SUCCESS }}" == "true" ]; then
  234. cd /tmp && python -m openhands.resolver.send_pull_request \
  235. --issue-number ${{ env.ISSUE_NUMBER }} \
  236. --pr-type draft \
  237. --reviewer ${{ github.actor }} | tee pr_result.txt && \
  238. grep "draft created" pr_result.txt | sed 's/.*\///g' > pr_number.txt
  239. else
  240. cd /tmp && python -m openhands.resolver.send_pull_request \
  241. --issue-number ${{ env.ISSUE_NUMBER }} \
  242. --pr-type branch \
  243. --send-on-failure | tee branch_result.txt && \
  244. grep "branch created" branch_result.txt | sed 's/.*\///g; s/.expand=1//g' > branch_name.txt
  245. fi
  246. # Step leaves comment for when agent is invoked on PR
  247. - name: Analyze Push Logs (Updated PR or No Changes) # Skip comment if PR update was successful OR leave comment if the agent made no code changes
  248. uses: actions/github-script@v7
  249. if: always()
  250. env:
  251. AGENT_RESPONDED: ${{ env.AGENT_RESPONDED || 'false' }}
  252. with:
  253. github-token: ${{ secrets.PAT_TOKEN || github.token }}
  254. script: |
  255. const fs = require('fs');
  256. const issueNumber = ${{ env.ISSUE_NUMBER }};
  257. let logContent = '';
  258. try {
  259. logContent = fs.readFileSync('/tmp/pr_result.txt', 'utf8').trim();
  260. } catch (error) {
  261. console.error('Error reading pr_result.txt file:', error);
  262. }
  263. const noChangesMessage = `No changes to commit for issue #${issueNumber}. Skipping commit.`;
  264. // Check logs from send_pull_request.py (pushes code to GitHub)
  265. if (logContent.includes("Updated pull request")) {
  266. console.log("Updated pull request found. Skipping comment.");
  267. process.env.AGENT_RESPONDED = 'true';
  268. } else if (logContent.includes(noChangesMessage)) {
  269. github.rest.issues.createComment({
  270. issue_number: issueNumber,
  271. owner: context.repo.owner,
  272. repo: context.repo.repo,
  273. body: `The workflow to fix this issue encountered an error. Openhands failed to create any code changes.`
  274. });
  275. process.env.AGENT_RESPONDED = 'true';
  276. }
  277. # Step leaves comment for when agent is invoked on issue
  278. - name: Comment on issue # Comment link to either PR or branch created by agent
  279. uses: actions/github-script@v7
  280. if: always() # Comment on issue even if the previous steps fail
  281. env:
  282. AGENT_RESPONDED: ${{ env.AGENT_RESPONDED || 'false' }}
  283. with:
  284. github-token: ${{ secrets.PAT_TOKEN || github.token }}
  285. script: |
  286. const fs = require('fs');
  287. const issueNumber = ${{ env.ISSUE_NUMBER }};
  288. const success = ${{ steps.check_result.outputs.RESOLUTION_SUCCESS }};
  289. let prNumber = '';
  290. let branchName = '';
  291. let successExplanation = '';
  292. try {
  293. if (success) {
  294. prNumber = fs.readFileSync('/tmp/pr_number.txt', 'utf8').trim();
  295. } else {
  296. branchName = fs.readFileSync('/tmp/branch_name.txt', 'utf8').trim();
  297. }
  298. } catch (error) {
  299. console.error('Error reading file:', error);
  300. }
  301. try {
  302. if (!success){
  303. // Read success_explanation from JSON file for failed resolution
  304. const outputFilePath = path.resolve('/tmp/output/output.jsonl');
  305. if (fs.existsSync(outputFilePath)) {
  306. const outputContent = fs.readFileSync(outputFilePath, 'utf8');
  307. const jsonLines = outputContent.split('\n').filter(line => line.trim() !== '');
  308. if (jsonLines.length > 0) {
  309. // First entry in JSON lines has the key 'success_explanation'
  310. const firstEntry = JSON.parse(jsonLines[0]);
  311. successExplanation = firstEntry.success_explanation || '';
  312. }
  313. }
  314. } catch (error){
  315. console.error('Error reading file:', error);
  316. }
  317. // Check "success" log from resolver output
  318. if (success && prNumber) {
  319. github.rest.issues.createComment({
  320. issue_number: issueNumber,
  321. owner: context.repo.owner,
  322. repo: context.repo.repo,
  323. body: `A potential fix has been generated and a draft PR #${prNumber} has been created. Please review the changes.`
  324. });
  325. process.env.AGENT_RESPONDED = 'true';
  326. } else if (!success && branchName) {
  327. let commentBody = `An attempt was made to automatically fix this issue, but it was unsuccessful. A branch named '${branchName}' has been created with the attempted changes. You can view the branch [here](https://github.com/${context.repo.owner}/${context.repo.repo}/tree/${branchName}). Manual intervention may be required.`;
  328. if (successExplanation) {
  329. commentBody += `\n\nAdditional details about the failure:\n${successExplanation}`;
  330. }
  331. github.rest.issues.createComment({
  332. issue_number: issueNumber,
  333. owner: context.repo.owner,
  334. repo: context.repo.repo,
  335. body: commentBody
  336. });
  337. process.env.AGENT_RESPONDED = 'true';
  338. }
  339. # Leave error comment when both PR/Issue comment handling fail
  340. - name: Fallback Error Comment
  341. uses: actions/github-script@v7
  342. if: ${{ env.AGENT_RESPONDED == 'false' }} # Only run if no conditions were met in previous steps
  343. with:
  344. github-token: ${{ secrets.PAT_TOKEN || github.token }}
  345. script: |
  346. const issueNumber = ${{ env.ISSUE_NUMBER }};
  347. github.rest.issues.createComment({
  348. issue_number: issueNumber,
  349. owner: context.repo.owner,
  350. repo: context.repo.repo,
  351. body: `The workflow to fix this issue encountered an error. Please check the [workflow logs](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}) for more information.`
  352. });