name: Use OpenDevin to Resolve GitHub Issue

on:
  issues:
    types: [labeled]

permissions:
  contents: write
  pull-requests: write
  issues: write

jobs:
  open-devin:
    if: github.event.label.name == 'dogfood-this'
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/opendevin/opendevin
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock

    steps:
    - name: install git, github cli
      run: apt-get install -y git gh

    - name: Checkout Repository
      uses: actions/checkout@v4

    - name: Write Task File
      env:
        ISSUE_TITLE: ${{ github.event.issue.title }}
        ISSUE_BODY: ${{ github.event.issue.body }}
      run: |
        echo "TITLE:" > task.txt
        echo "${ISSUE_TITLE}" >> task.txt
        echo "" >> task.txt
        echo "BODY:" >> task.txt
        echo "${ISSUE_BODY}" >> task.txt

    - name: Run OpenDevin
      env:
        ISSUE_TITLE: ${{ github.event.issue.title }}
        ISSUE_BODY: ${{ github.event.issue.body }}
        LLM_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        SANDBOX_TYPE: exec
      run: |
        python ./opendevin/main.py -d "./" -i 50 -f task.txt -d $GITHUB_WORKSPACE
        rm task.txt

    - name: Setup Git, Create Branch, and Commit Changes
      run: |
        # Setup Git configuration
        git config --global --add safe.directory $PWD
        git config --global user.name 'OpenDevin'
        git config --global user.email 'OpenDevin@users.noreply.github.com'

        # Create a unique branch name with a timestamp
        BRANCH_NAME="fix/${{ github.event.issue.number }}-$(date +%Y%m%d%H%M%S)"

        # Checkout new branch
        git checkout -b $BRANCH_NAME

        # Add all changes to staging, except task.txt
        git add --all -- ':!task.txt'

        # Commit the changes, if any
        git commit -m "OpenDevin: Resolve Issue #${{ github.event.issue.number }}"
        if [ $? -ne 0 ]; then
          echo "No changes to commit."
          exit 0
        fi

        # Push changes
        git push --set-upstream origin $BRANCH_NAME

    - name: Fetch Default Branch
      env:
        GH_TOKEN: ${{ github.token }}
      run: |
        # Fetch the default branch using gh cli
        DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef --jq .defaultBranchRef.name)
        echo "Default branch is $DEFAULT_BRANCH"
        echo "DEFAULT_BRANCH=$DEFAULT_BRANCH" >> $GITHUB_ENV

    - name: Generate PR
      env:
        GH_TOKEN: ${{ github.token }}
      run: |
        # Create PR and capture URL
        PR_URL=$(gh pr create \
          --title "OpenDevin: Resolve Issue #2" \
          --body "This PR was generated by OpenDevin to resolve issue #2" \
          --repo "foragerr/OpenDevin" \
          --head "${{ github.head_ref }}" \
          --base "${{ env.DEFAULT_BRANCH }}" \
          | grep -o 'https://github.com/[^ ]*')

        # Extract PR number from URL
        PR_NUMBER=$(echo "$PR_URL" | grep -o '[0-9]\+$')

        # Set environment vars
        echo "PR_URL=$PR_URL" >> $GITHUB_ENV
        echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV

    - name: Post Comment
      env:
        GH_TOKEN: ${{ github.token }}
      run: |
        gh issue comment ${{ github.event.issue.number }} \
          -b "OpenDevin raised [PR #${{ env.PR_NUMBER }}](${{ env.PR_URL }}) to resolve this issue."
