openhands-resolver.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. startsWith(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. startsWith(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. startsWith(github.event.comment.body, '@openhands-agent-exp')
  80. ) ||
  81. (
  82. github.event_name == 'pull_request_review' &&
  83. startsWith(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. --target-branch ${{ env.TARGET_BRANCH }}
  171. - name: Check resolution result
  172. id: check_result
  173. run: |
  174. if cd /tmp && grep -q '"success":true' output/output.jsonl; then
  175. echo "RESOLUTION_SUCCESS=true" >> $GITHUB_OUTPUT
  176. else
  177. echo "RESOLUTION_SUCCESS=false" >> $GITHUB_OUTPUT
  178. fi
  179. - name: Upload output.jsonl as artifact
  180. uses: actions/upload-artifact@v4
  181. if: always() # Upload even if the previous steps fail
  182. with:
  183. name: resolver-output
  184. path: /tmp/output/output.jsonl
  185. retention-days: 30 # Keep the artifact for 30 days
  186. - name: Create draft PR or push branch
  187. if: always() # Create PR or branch even if the previous steps fail
  188. env:
  189. GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
  190. GITHUB_USERNAME: ${{ secrets.PAT_USERNAME }}
  191. LLM_MODEL: ${{ secrets.LLM_MODEL }}
  192. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  193. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  194. PYTHONPATH: ""
  195. run: |
  196. if [ "${{ steps.check_result.outputs.RESOLUTION_SUCCESS }}" == "true" ]; then
  197. cd /tmp && python -m openhands.resolver.send_pull_request \
  198. --issue-number ${{ env.ISSUE_NUMBER }} \
  199. --pr-type draft | tee pr_result.txt && \
  200. grep "draft created" pr_result.txt | sed 's/.*\///g' > pr_number.txt
  201. else
  202. cd /tmp && python -m openhands.resolver.send_pull_request \
  203. --issue-number ${{ env.ISSUE_NUMBER }} \
  204. --pr-type branch \
  205. --send-on-failure | tee branch_result.txt && \
  206. grep "branch created" branch_result.txt | sed 's/.*\///g; s/.expand=1//g' > branch_name.txt
  207. fi
  208. - name: Comment on issue
  209. uses: actions/github-script@v7
  210. if: always() # Comment on issue even if the previous steps fail
  211. with:
  212. github-token: ${{secrets.GITHUB_TOKEN}}
  213. script: |
  214. const fs = require('fs');
  215. const issueNumber = ${{ env.ISSUE_NUMBER }};
  216. const success = ${{ steps.check_result.outputs.RESOLUTION_SUCCESS }};
  217. let prNumber = '';
  218. let branchName = '';
  219. let logContent = '';
  220. const noChangesMessage = `No changes to commit for issue #${issueNumber}. Skipping commit.`;
  221. try {
  222. if (success){
  223. logContent = fs.readFileSync('/tmp/pr_result.txt', 'utf8').trim();
  224. } else {
  225. logContent = fs.readFileSync('/tmp/branch_result.txt', 'utf8').trim();
  226. }
  227. } catch (error) {
  228. console.error('Error reading results file:', error);
  229. }
  230. try {
  231. if (success) {
  232. prNumber = fs.readFileSync('/tmp/pr_number.txt', 'utf8').trim();
  233. } else {
  234. branchName = fs.readFileSync('/tmp/branch_name.txt', 'utf8').trim();
  235. }
  236. } catch (error) {
  237. console.error('Error reading file:', error);
  238. }
  239. if (logContent.includes(noChangesMessage)) {
  240. github.rest.issues.createComment({
  241. issue_number: issueNumber,
  242. owner: context.repo.owner,
  243. repo: context.repo.repo,
  244. body: `The workflow to fix this issue encountered an error. Openhands failed to create any code changes.`
  245. });
  246. } else if (success && prNumber) {
  247. github.rest.issues.createComment({
  248. issue_number: issueNumber,
  249. owner: context.repo.owner,
  250. repo: context.repo.repo,
  251. body: `A potential fix has been generated and a draft PR #${prNumber} has been created. Please review the changes.`
  252. });
  253. } else if (!success && branchName) {
  254. github.rest.issues.createComment({
  255. issue_number: issueNumber,
  256. owner: context.repo.owner,
  257. repo: context.repo.repo,
  258. 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.`
  259. });
  260. } else {
  261. github.rest.issues.createComment({
  262. issue_number: issueNumber,
  263. owner: context.repo.owner,
  264. repo: context.repo.repo,
  265. 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.`
  266. });
  267. }