openhands-resolver.yml 12 KB

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