openhands-resolver.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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: |
  70. !(
  71. github.event.label.name == 'fix-me-experimental' ||
  72. (
  73. (github.event_name == 'issue_comment' || github.event_name == 'pull_request_review_comment') &&
  74. startsWith(github.event.comment.body, '@openhands-agent-exp')
  75. ) ||
  76. (
  77. github.event_name == 'pull_request_review' &&
  78. startsWith(github.event.review.body, '@openhands-agent-exp')
  79. )
  80. )
  81. uses: actions/cache@v3
  82. with:
  83. path: ${{ env.pythonLocation }}/lib/python3.12/site-packages/*
  84. key: ${{ runner.os }}-pip-openhands-resolver-${{ hashFiles('requirements.txt') }}
  85. restore-keys: |
  86. ${{ runner.os }}-pip-openhands-resolver-${{ hashFiles('requirements.txt') }}
  87. - name: Check required environment variables
  88. env:
  89. LLM_MODEL: ${{ secrets.LLM_MODEL }}
  90. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  91. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  92. PAT_TOKEN: ${{ secrets.PAT_TOKEN }}
  93. PAT_USERNAME: ${{ secrets.PAT_USERNAME }}
  94. run: |
  95. required_vars=("LLM_MODEL" "LLM_API_KEY" "PAT_TOKEN" "PAT_USERNAME")
  96. for var in "${required_vars[@]}"; do
  97. if [ -z "${!var}" ]; then
  98. echo "Error: Required environment variable $var is not set."
  99. exit 1
  100. fi
  101. done
  102. - name: Set environment variables
  103. run: |
  104. if [ -n "${{ github.event.review.body }}" ]; then
  105. echo "ISSUE_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
  106. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  107. elif [ -n "${{ github.event.issue.pull_request }}" ]; then
  108. echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
  109. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  110. elif [ -n "${{ github.event.pull_request.number }}" ]; then
  111. echo "ISSUE_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
  112. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  113. else
  114. echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
  115. echo "ISSUE_TYPE=issue" >> $GITHUB_ENV
  116. fi
  117. if [ -n "${{ github.event.review.body }}" ]; then
  118. echo "COMMENT_ID=${{ github.event.review.id || 'None' }}" >> $GITHUB_ENV
  119. else
  120. echo "COMMENT_ID=${{ github.event.comment.id || 'None' }}" >> $GITHUB_ENV
  121. fi
  122. echo "MAX_ITERATIONS=${{ inputs.max_iterations || 50 }}" >> $GITHUB_ENV
  123. echo "SANDBOX_ENV_GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV
  124. - name: Comment on issue with start message
  125. uses: actions/github-script@v7
  126. with:
  127. github-token: ${{secrets.GITHUB_TOKEN}}
  128. script: |
  129. const issueType = process.env.ISSUE_TYPE;
  130. github.rest.issues.createComment({
  131. issue_number: ${{ env.ISSUE_NUMBER }},
  132. owner: context.repo.owner,
  133. repo: context.repo.repo,
  134. 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}).`
  135. });
  136. - name: Install OpenHands
  137. run: |
  138. if [[ "${{ github.event.label.name }}" == "fix-me-experimental" ]] ||
  139. ([[ "${{ github.event_name }}" == "issue_comment" || "${{ github.event_name }}" == "pull_request_review_comment" ]] &&
  140. [[ "${{ github.event.comment.body }}" == "@openhands-agent-exp"* ]]) ||
  141. ([[ "${{ github.event_name }}" == "pull_request_review" ]] &&
  142. [[ "${{ github.event.review.body }}" == "@openhands-agent-exp"* ]]); then
  143. python -m pip install --upgrade pip
  144. pip install git+https://github.com/all-hands-ai/openhands.git
  145. else
  146. python -m pip install --upgrade -r requirements.txt
  147. fi
  148. - name: Attempt to resolve issue
  149. env:
  150. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  151. GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }}
  152. LLM_MODEL: ${{ secrets.LLM_MODEL }}
  153. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  154. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  155. PYTHONPATH: ""
  156. run: |
  157. cd /tmp && python -m openhands.resolver.resolve_issue \
  158. --repo ${{ github.repository }} \
  159. --issue-number ${{ env.ISSUE_NUMBER }} \
  160. --issue-type ${{ env.ISSUE_TYPE }} \
  161. --max-iterations ${{ env.MAX_ITERATIONS }} \
  162. --comment-id ${{ env.COMMENT_ID }}
  163. - name: Check resolution result
  164. id: check_result
  165. run: |
  166. if cd /tmp && grep -q '"success":true' output/output.jsonl; then
  167. echo "RESOLUTION_SUCCESS=true" >> $GITHUB_OUTPUT
  168. else
  169. echo "RESOLUTION_SUCCESS=false" >> $GITHUB_OUTPUT
  170. fi
  171. - name: Upload output.jsonl as artifact
  172. uses: actions/upload-artifact@v4
  173. if: always() # Upload even if the previous steps fail
  174. with:
  175. name: resolver-output
  176. path: /tmp/output/output.jsonl
  177. retention-days: 30 # Keep the artifact for 30 days
  178. - name: Create draft PR or push branch
  179. if: always() # Create PR or branch even if the previous steps fail
  180. env:
  181. GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
  182. GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }}
  183. LLM_MODEL: ${{ secrets.LLM_MODEL }}
  184. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  185. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  186. PYTHONPATH: ""
  187. run: |
  188. if [ "${{ steps.check_result.outputs.RESOLUTION_SUCCESS }}" == "true" ]; then
  189. cd /tmp && python -m openhands.resolver.send_pull_request \
  190. --issue-number ${{ env.ISSUE_NUMBER }} \
  191. --pr-type draft | tee pr_result.txt && \
  192. grep "draft created" pr_result.txt | sed 's/.*\///g' > pr_number.txt
  193. else
  194. cd /tmp && python -m openhands.resolver.send_pull_request \
  195. --issue-number ${{ env.ISSUE_NUMBER }} \
  196. --pr-type branch \
  197. --send-on-failure | tee branch_result.txt && \
  198. grep "branch created" branch_result.txt | sed 's/.*\///g; s/.expand=1//g' > branch_name.txt
  199. fi
  200. - name: Comment on issue
  201. uses: actions/github-script@v7
  202. if: always() # Comment on issue even if the previous steps fail
  203. with:
  204. github-token: ${{secrets.GITHUB_TOKEN}}
  205. script: |
  206. const fs = require('fs');
  207. const issueNumber = ${{ env.ISSUE_NUMBER }};
  208. const success = ${{ steps.check_result.outputs.RESOLUTION_SUCCESS }};
  209. let prNumber = '';
  210. let branchName = '';
  211. let logContent = '';
  212. const noChangesMessage = `No changes to commit for issue #${issueNumber}. Skipping commit.`;
  213. try {
  214. if (success){
  215. logContent = fs.readFileSync('/tmp/pr_result.txt', 'utf8').trim();
  216. } else {
  217. logContent = fs.readFileSync('/tmp/branch_result.txt', 'utf8').trim();
  218. }
  219. } catch (error) {
  220. console.error('Error reading results file:', error);
  221. }
  222. try {
  223. if (success) {
  224. prNumber = fs.readFileSync('/tmp/pr_number.txt', 'utf8').trim();
  225. } else {
  226. branchName = fs.readFileSync('/tmp/branch_name.txt', 'utf8').trim();
  227. }
  228. } catch (error) {
  229. console.error('Error reading file:', error);
  230. }
  231. if (logContent.includes(noChangesMessage)) {
  232. github.rest.issues.createComment({
  233. issue_number: issueNumber,
  234. owner: context.repo.owner,
  235. repo: context.repo.repo,
  236. body: `The workflow to fix this issue encountered an error. Openhands failed to create any code changes.`
  237. });
  238. } else if (success && prNumber) {
  239. github.rest.issues.createComment({
  240. issue_number: issueNumber,
  241. owner: context.repo.owner,
  242. repo: context.repo.repo,
  243. body: `A potential fix has been generated and a draft PR #${prNumber} has been created. Please review the changes.`
  244. });
  245. } else if (!success && branchName) {
  246. github.rest.issues.createComment({
  247. issue_number: issueNumber,
  248. owner: context.repo.owner,
  249. repo: context.repo.repo,
  250. 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.`
  251. });
  252. } else {
  253. github.rest.issues.createComment({
  254. issue_number: issueNumber,
  255. owner: context.repo.owner,
  256. repo: context.repo.repo,
  257. 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.`
  258. });
  259. }