openhands-resolver.yml 13 KB

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