openhands-resolver.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. # Handle pull request events first
  130. if [ -n "${{ github.event.pull_request.number }}" ]; then
  131. echo "ISSUE_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
  132. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  133. # Handle pull request review events
  134. elif [ -n "${{ github.event.review.body }}" ]; then
  135. echo "ISSUE_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
  136. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  137. # Handle issue comment events that reference a PR
  138. elif [ -n "${{ github.event.issue.pull_request }}" ]; then
  139. echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
  140. echo "ISSUE_TYPE=pr" >> $GITHUB_ENV
  141. # Handle regular issue events
  142. else
  143. echo "ISSUE_NUMBER=${{ github.event.issue.number }}" >> $GITHUB_ENV
  144. echo "ISSUE_TYPE=issue" >> $GITHUB_ENV
  145. fi
  146. if [ -n "${{ github.event.review.body }}" ]; then
  147. echo "COMMENT_ID=${{ github.event.review.id || 'None' }}" >> $GITHUB_ENV
  148. else
  149. echo "COMMENT_ID=${{ github.event.comment.id || 'None' }}" >> $GITHUB_ENV
  150. fi
  151. echo "MAX_ITERATIONS=${{ inputs.max_iterations || 50 }}" >> $GITHUB_ENV
  152. echo "SANDBOX_ENV_GITHUB_TOKEN=${{ secrets.PAT_TOKEN || github.token }}" >> $GITHUB_ENV
  153. echo "SANDBOX_ENV_BASE_CONTAINER_IMAGE=${{ inputs.base_container_image }}" >> $GITHUB_ENV
  154. # Set branch variables
  155. echo "TARGET_BRANCH=${{ inputs.target_branch }}" >> $GITHUB_ENV
  156. - name: Comment on issue with start message
  157. uses: actions/github-script@v7
  158. with:
  159. github-token: ${{ secrets.PAT_TOKEN || github.token }}
  160. script: |
  161. const issueType = process.env.ISSUE_TYPE;
  162. github.rest.issues.createComment({
  163. issue_number: ${{ env.ISSUE_NUMBER }},
  164. owner: context.repo.owner,
  165. repo: context.repo.repo,
  166. 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}).`
  167. });
  168. - name: Install OpenHands
  169. uses: actions/github-script@v7
  170. with:
  171. script: |
  172. const commentBody = `${{ github.event.comment.body || '' }}`.trim();
  173. const reviewBody = `${{ github.event.review.body || '' }}`.trim();
  174. const labelName = `${{ github.event.label.name || '' }}`.trim();
  175. const eventName = `${{ github.event_name }}`.trim();
  176. // Check conditions
  177. const isExperimentalLabel = labelName === "fix-me-experimental";
  178. const isIssueCommentExperimental =
  179. (eventName === "issue_comment" || eventName === "pull_request_review_comment") &&
  180. commentBody.includes("@openhands-agent-exp");
  181. const isReviewCommentExperimental =
  182. eventName === "pull_request_review" && reviewBody.includes("@openhands-agent-exp");
  183. // Perform package installation
  184. if (isExperimentalLabel || isIssueCommentExperimental || isReviewCommentExperimental) {
  185. console.log("Installing experimental OpenHands...");
  186. await exec.exec("python -m pip install --upgrade pip");
  187. await exec.exec("pip install git+https://github.com/all-hands-ai/openhands.git");
  188. } else {
  189. console.log("Installing from requirements.txt...");
  190. await exec.exec("python -m pip install --upgrade pip");
  191. await exec.exec("pip install -r requirements.txt");
  192. }
  193. - name: Attempt to resolve issue
  194. env:
  195. GITHUB_TOKEN: ${{ secrets.PAT_TOKEN || github.token }}
  196. GITHUB_USERNAME: ${{ secrets.PAT_USERNAME || 'openhands-agent' }}
  197. LLM_MODEL: ${{ secrets.LLM_MODEL || inputs.LLM_MODEL }}
  198. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  199. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  200. PYTHONPATH: ""
  201. run: |
  202. cd /tmp && python -m openhands.resolver.resolve_issue \
  203. --repo ${{ github.repository }} \
  204. --issue-number ${{ env.ISSUE_NUMBER }} \
  205. --issue-type ${{ env.ISSUE_TYPE }} \
  206. --max-iterations ${{ env.MAX_ITERATIONS }} \
  207. --comment-id ${{ env.COMMENT_ID }}
  208. - name: Check resolution result
  209. id: check_result
  210. run: |
  211. if cd /tmp && grep -q '"success":true' output/output.jsonl; then
  212. echo "RESOLUTION_SUCCESS=true" >> $GITHUB_OUTPUT
  213. else
  214. echo "RESOLUTION_SUCCESS=false" >> $GITHUB_OUTPUT
  215. fi
  216. - name: Upload output.jsonl as artifact
  217. uses: actions/upload-artifact@v4
  218. if: always() # Upload even if the previous steps fail
  219. with:
  220. name: resolver-output
  221. path: /tmp/output/output.jsonl
  222. retention-days: 30 # Keep the artifact for 30 days
  223. - name: Create draft PR or push branch
  224. if: always() # Create PR or branch even if the previous steps fail
  225. env:
  226. GITHUB_TOKEN: ${{ secrets.PAT_TOKEN || github.token }}
  227. GITHUB_USERNAME: ${{ secrets.PAT_USERNAME || 'openhands-agent' }}
  228. LLM_MODEL: ${{ secrets.LLM_MODEL || inputs.LLM_MODEL }}
  229. LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
  230. LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
  231. PYTHONPATH: ""
  232. run: |
  233. if [ "${{ steps.check_result.outputs.RESOLUTION_SUCCESS }}" == "true" ]; then
  234. cd /tmp && python -m openhands.resolver.send_pull_request \
  235. --issue-number ${{ env.ISSUE_NUMBER }} \
  236. --pr-type draft \
  237. --reviewer ${{ github.actor }} | tee pr_result.txt && \
  238. grep "draft created" pr_result.txt | sed 's/.*\///g' > pr_number.txt
  239. else
  240. cd /tmp && python -m openhands.resolver.send_pull_request \
  241. --issue-number ${{ env.ISSUE_NUMBER }} \
  242. --pr-type branch \
  243. --send-on-failure | tee branch_result.txt && \
  244. grep "branch created" branch_result.txt | sed 's/.*\///g; s/.expand=1//g' > branch_name.txt
  245. fi
  246. - name: Comment on issue
  247. uses: actions/github-script@v7
  248. if: always() # Comment on issue even if the previous steps fail
  249. with:
  250. github-token: ${{ secrets.PAT_TOKEN || github.token }}
  251. script: |
  252. const fs = require('fs');
  253. const issueNumber = ${{ env.ISSUE_NUMBER }};
  254. const success = ${{ steps.check_result.outputs.RESOLUTION_SUCCESS }};
  255. let prNumber = '';
  256. let branchName = '';
  257. let logContent = '';
  258. const noChangesMessage = `No changes to commit for issue #${issueNumber}. Skipping commit.`;
  259. try {
  260. if (success){
  261. logContent = fs.readFileSync('/tmp/pr_result.txt', 'utf8').trim();
  262. } else {
  263. logContent = fs.readFileSync('/tmp/branch_result.txt', 'utf8').trim();
  264. }
  265. } catch (error) {
  266. console.error('Error reading results file:', error);
  267. }
  268. try {
  269. if (success) {
  270. prNumber = fs.readFileSync('/tmp/pr_number.txt', 'utf8').trim();
  271. } else {
  272. branchName = fs.readFileSync('/tmp/branch_name.txt', 'utf8').trim();
  273. }
  274. } catch (error) {
  275. console.error('Error reading file:', error);
  276. }
  277. if (logContent.includes(noChangesMessage)) {
  278. github.rest.issues.createComment({
  279. issue_number: issueNumber,
  280. owner: context.repo.owner,
  281. repo: context.repo.repo,
  282. body: `The workflow to fix this issue encountered an error. Openhands failed to create any code changes.`
  283. });
  284. } else if (success && prNumber) {
  285. github.rest.issues.createComment({
  286. issue_number: issueNumber,
  287. owner: context.repo.owner,
  288. repo: context.repo.repo,
  289. body: `A potential fix has been generated and a draft PR #${prNumber} has been created. Please review the changes.`
  290. });
  291. } else if (!success && branchName) {
  292. github.rest.issues.createComment({
  293. issue_number: issueNumber,
  294. owner: context.repo.owner,
  295. repo: context.repo.repo,
  296. 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.`
  297. });
  298. } else {
  299. github.rest.issues.createComment({
  300. issue_number: issueNumber,
  301. owner: context.repo.owner,
  302. repo: context.repo.repo,
  303. 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.`
  304. });
  305. }