openhands-resolver.yml 12 KB

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