openhands-resolver.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. secrets:
  14. LLM_MODEL:
  15. required: true
  16. LLM_API_KEY:
  17. required: true
  18. LLM_BASE_URL:
  19. required: false
  20. PAT_TOKEN:
  21. required: true
  22. PAT_USERNAME:
  23. required: true
  24. issues:
  25. types: [labeled]
  26. pull_request:
  27. types: [labeled]
  28. issue_comment:
  29. types: [created]
  30. pull_request_review_comment:
  31. types: [created]
  32. pull_request_review:
  33. types: [submitted]
  34. permissions:
  35. contents: write
  36. pull-requests: write
  37. issues: write
  38. jobs:
  39. auto-fix:
  40. if: |
  41. github.event_name == 'workflow_call' ||
  42. github.event.label.name == 'fix-me' ||
  43. github.event.label.name == 'fix-me-experimental' ||
  44. (
  45. ((github.event_name == 'issue_comment' || github.event_name == 'pull_request_review_comment') &&
  46. startsWith(github.event.comment.body, inputs.macro || '@openhands-agent') &&
  47. (github.event.comment.author_association == 'OWNER' || github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'MEMBER')
  48. ) ||
  49. (github.event_name == 'pull_request_review' &&
  50. startsWith(github.event.review.body, inputs.macro || '@openhands-agent') &&
  51. (github.event.review.author_association == 'OWNER' || github.event.review.author_association == 'COLLABORATOR' || github.event.review.author_association == 'MEMBER')
  52. )
  53. )
  54. runs-on: ubuntu-latest
  55. steps:
  56. - name: Checkout repository
  57. uses: actions/checkout@v4
  58. - name: Set up Python
  59. uses: actions/setup-python@v5
  60. with:
  61. python-version: "3.12"
  62. - name: Get latest versions and create requirements.txt
  63. run: |
  64. python -m pip index versions openhands-ai > openhands_versions.txt
  65. OPENHANDS_VERSION=$(head -n 1 openhands_versions.txt | awk '{print $2}' | tr -d '()')
  66. echo "openhands-ai==${OPENHANDS_VERSION}" >> requirements.txt
  67. cat requirements.txt
  68. - name: Cache pip dependencies
  69. if: github.event.label.name != 'fix-me-experimental'
  70. uses: actions/cache@v3
  71. with:
  72. path: ${{ env.pythonLocation }}/lib/python3.12/site-packages/*
  73. key: ${{ runner.os }}-pip-openhands-resolver-${{ hashFiles('requirements.txt') }}
  74. restore-keys: |
  75. ${{ runner.os }}-pip-openhands-resolver-${{ hashFiles('requirements.txt') }}
  76. - name: Check required environment variables
  77. env:
  78. LLM_MODEL: ${{ secrets.LLM_MODEL }}
  79. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  80. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  81. PAT_TOKEN: ${{ secrets.PAT_TOKEN }}
  82. PAT_USERNAME: ${{ secrets.PAT_USERNAME }}
  83. run: |
  84. required_vars=("LLM_MODEL" "LLM_API_KEY" "PAT_TOKEN" "PAT_USERNAME")
  85. for var in "${required_vars[@]}"; do
  86. if [ -z "${!var}" ]; then
  87. echo "Error: Required environment variable $var is not set."
  88. exit 1
  89. fi
  90. done
  91. - name: Set environment variables
  92. run: |
  93. if [ -n "${{ github.event.review.body }}" ]; then
  94. echo "ISSUE_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
  95. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  96. elif [ -n "${{ github.event.issue.pull_request }}" ]; then
  97. echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
  98. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  99. elif [ -n "${{ github.event.pull_request.number }}" ]; then
  100. echo "ISSUE_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
  101. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  102. else
  103. echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
  104. echo "ISSUE_TYPE=issue" >> $GITHUB_ENV
  105. fi
  106. if [ -n "${{ github.event.review.body }}" ]; then
  107. echo "COMMENT_ID=${{ github.event.review.id || 'None' }}" >> $GITHUB_ENV
  108. else
  109. echo "COMMENT_ID=${{ github.event.comment.id || 'None' }}" >> $GITHUB_ENV
  110. fi
  111. echo "MAX_ITERATIONS=${{ inputs.max_iterations || 50 }}" >> $GITHUB_ENV
  112. echo "SANDBOX_ENV_GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV
  113. - name: Comment on issue with start message
  114. uses: actions/github-script@v7
  115. with:
  116. github-token: ${{secrets.GITHUB_TOKEN}}
  117. script: |
  118. const issueType = process.env.ISSUE_TYPE;
  119. github.rest.issues.createComment({
  120. issue_number: ${{ env.ISSUE_NUMBER }},
  121. owner: context.repo.owner,
  122. repo: context.repo.repo,
  123. 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}).`
  124. });
  125. - name: Install OpenHands
  126. run: |
  127. if [ "${{ github.event.label.name }}" == "fix-me-experimental" ]; then
  128. python -m pip install --upgrade pip
  129. pip install git+https://github.com/all-hands-ai/openhands.git
  130. else
  131. python -m pip install --upgrade -r requirements.txt
  132. fi
  133. - name: Attempt to resolve issue
  134. env:
  135. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  136. GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }}
  137. LLM_MODEL: ${{ secrets.LLM_MODEL }}
  138. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  139. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  140. PYTHONPATH: ""
  141. run: |
  142. cd /tmp && python -m openhands.resolver.resolve_issue \
  143. --repo ${{ github.repository }} \
  144. --issue-number ${{ env.ISSUE_NUMBER }} \
  145. --issue-type ${{ env.ISSUE_TYPE }} \
  146. --max-iterations ${{ env.MAX_ITERATIONS }} \
  147. --comment-id ${{ env.COMMENT_ID }}
  148. - name: Check resolution result
  149. id: check_result
  150. run: |
  151. if cd /tmp && grep -q '"success":true' output/output.jsonl; then
  152. echo "RESOLUTION_SUCCESS=true" >> $GITHUB_OUTPUT
  153. else
  154. echo "RESOLUTION_SUCCESS=false" >> $GITHUB_OUTPUT
  155. fi
  156. - name: Upload output.jsonl as artifact
  157. uses: actions/upload-artifact@v4
  158. if: always() # Upload even if the previous steps fail
  159. with:
  160. name: resolver-output
  161. path: /tmp/output/output.jsonl
  162. retention-days: 30 # Keep the artifact for 30 days
  163. - name: Create draft PR or push branch
  164. env:
  165. GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
  166. GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }}
  167. LLM_MODEL: ${{ secrets.LLM_MODEL }}
  168. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  169. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  170. PYTHONPATH: ""
  171. run: |
  172. if [ "${{ steps.check_result.outputs.RESOLUTION_SUCCESS }}" == "true" ]; then
  173. cd /tmp && python -m openhands.resolver.send_pull_request \
  174. --issue-number ${{ env.ISSUE_NUMBER }} \
  175. --pr-type draft | tee pr_result.txt && \
  176. grep "draft created" pr_result.txt | sed 's/.*\///g' > pr_number.txt
  177. else
  178. cd /tmp && python -m openhands.resolver.send_pull_request \
  179. --issue-number ${{ env.ISSUE_NUMBER }} \
  180. --pr-type branch \
  181. --send-on-failure | tee branch_result.txt && \
  182. grep "branch created" branch_result.txt | sed 's/.*\///g; s/.expand=1//g' > branch_name.txt
  183. fi
  184. - name: Comment on issue
  185. uses: actions/github-script@v7
  186. with:
  187. github-token: ${{secrets.GITHUB_TOKEN}}
  188. script: |
  189. const fs = require('fs');
  190. const issueNumber = ${{ env.ISSUE_NUMBER }};
  191. const success = ${{ steps.check_result.outputs.RESOLUTION_SUCCESS }};
  192. let prNumber = '';
  193. let branchName = '';
  194. let logContent = '';
  195. const noChangesMessage = `No changes to commit for issue #${issueNumber}. Skipping commit.`;
  196. try {
  197. if (success){
  198. logContent = fs.readFileSync('/tmp/pr_result.txt', 'utf8').trim();
  199. } else {
  200. logContent = fs.readFileSync('/tmp/branch_result.txt', 'utf8').trim();
  201. }
  202. } catch (error) {
  203. console.error('Error reading results file:', error);
  204. }
  205. try {
  206. if (success) {
  207. prNumber = fs.readFileSync('/tmp/pr_number.txt', 'utf8').trim();
  208. } else {
  209. branchName = fs.readFileSync('/tmp/branch_name.txt', 'utf8').trim();
  210. }
  211. } catch (error) {
  212. console.error('Error reading file:', error);
  213. }
  214. if (logContent.includes(noChangesMessage)) {
  215. github.rest.issues.createComment({
  216. issue_number: issueNumber,
  217. owner: context.repo.owner,
  218. repo: context.repo.repo,
  219. body: `The workflow to fix this issue encountered an error. Openhands failed to create any code changes.`
  220. });
  221. } else if (success && prNumber) {
  222. github.rest.issues.createComment({
  223. issue_number: issueNumber,
  224. owner: context.repo.owner,
  225. repo: context.repo.repo,
  226. body: `A potential fix has been generated and a draft PR #${prNumber} has been created. Please review the changes.`
  227. });
  228. } else if (!success && branchName) {
  229. github.rest.issues.createComment({
  230. issue_number: issueNumber,
  231. owner: context.repo.owner,
  232. repo: context.repo.repo,
  233. body: `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.`
  234. });
  235. } else {
  236. github.rest.issues.createComment({
  237. issue_number: issueNumber,
  238. owner: context.repo.owner,
  239. repo: context.repo.repo,
  240. 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.`
  241. });
  242. }