aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/ci.yml
blob: 710359d0e899a997d57642fd511f069feaf36624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
name: Python CI

on:
  push:
    branches: [ master, ci-test* ]
    paths-ignore:
      - 'tests/Auto-GPT-test-cassettes'
      - 'tests/challenges/current_score.json'
  pull_request:
    branches: [ stable, master, release-* ]
  pull_request_target:
    branches: [ master, release-*, ci-test* ]

concurrency:
  group: ${{ format('ci-{0}', github.head_ref && format('{0}-{1}', github.event_name, github.event.pull_request.number) || github.sha) }}
  cancel-in-progress: ${{ startsWith(github.event_name, 'pull_request') }}

jobs:
  lint:
    # eliminate duplicate runs
    if: github.event_name == 'push' || (github.event.pull_request.head.repo.fork == (github.event_name == 'pull_request_target'))

    runs-on: ubuntu-latest
    env:
      min-python-version: "3.10"

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          ref: ${{ github.event.pull_request.head.ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}

      - name: Set up Python ${{ env.min-python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ env.min-python-version }}

      - id: get_date
        name: Get date
        run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT

      - name: Set up Python dependency cache
        uses: actions/cache@v3
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}-${{ steps.get_date.outputs.date }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Lint with flake8
        run: flake8

      - name: Check black formatting
        run: black . --check
        if: success() || failure()

      - name: Check isort formatting
        run: isort . --check
        if: success() || failure()

      - name: Check mypy formatting
        run: mypy
        if: success() || failure()

      - name: Check for unused imports and pass statements
        run: |
          cmd="autoflake --remove-all-unused-imports --recursive --ignore-init-module-imports --ignore-pass-after-docstring autogpt tests"
          $cmd --check || (echo "You have unused imports or pass statements, please run '${cmd} --in-place'" && exit 1)

  test:
    # eliminate duplicate runs
    if: github.event_name == 'push' || (github.event.pull_request.head.repo.fork == (github.event_name == 'pull_request_target'))

    permissions:
      # Gives the action the necessary permissions for publishing new
      # comments in pull requests.
      pull-requests: write
      # Gives the action the necessary permissions for pushing data to the
      # python-coverage-comment-action branch, and for editing existing
      # comments (to avoid publishing multiple comments in the same PR)
      contents: write
    runs-on: ubuntu-latest
    timeout-minutes: 30
    strategy:
      matrix:
        python-version: ["3.10"]

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          ref: ${{ github.event.pull_request.head.ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          submodules: true

      - name: Configure git user Auto-GPT-Bot
        run: |
          git config --global user.name "Auto-GPT-Bot"
          git config --global user.email "github-bot@agpt.co"

      - name: Checkout cassettes
        if: ${{ startsWith(github.event_name, 'pull_request') }}
        run: |
          cassette_branch="${{ github.event.pull_request.user.login }}-${{ github.event.pull_request.head.ref }}"
          cd tests/Auto-GPT-test-cassettes

          if git ls-remote --exit-code --heads origin $cassette_branch ; then
            git fetch origin $cassette_branch
            git fetch origin ${{ github.event.pull_request.base.ref }}

            git checkout $cassette_branch

            # Pick non-conflicting cassette updates from the base branch
            git merge --no-commit --strategy-option=ours origin/${{ github.event.pull_request.base.ref }}
            echo "Using cassettes from mirror branch '$cassette_branch'," \
              "synced to upstream branch '${{ github.event.pull_request.base.ref }}'."
          else
            git checkout -b $cassette_branch
            echo "Branch '$cassette_branch' does not exist in cassette submodule." \
              "Using cassettes from '${{ github.event.pull_request.base.ref }}'."
          fi

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - id: get_date
        name: Get date
        run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT

      - name: Set up Python dependency cache
        uses: actions/cache@v3
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}-${{ steps.get_date.outputs.date }}

      - name: Install Python dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Run pytest with coverage
        run: |
          pytest -vv --cov=autogpt --cov-branch --cov-report term-missing --cov-report xml \
            --numprocesses=logical --durations=10 \
            tests/unit tests/integration tests/challenges
          python tests/challenges/utils/build_current_score.py
        env:
          CI: true
          PROXY: ${{ secrets.PROXY }}
          AGENT_MODE: ${{ secrets.AGENT_MODE }}
          AGENT_TYPE: ${{ secrets.AGENT_TYPE }}
          PLAIN_OUTPUT: True

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v3

      - id: setup_git_auth
        name: Set up git token authentication
        # Cassettes may be pushed even when tests fail
        if: success() || failure()
        run: |
          config_key="http.${{ github.server_url }}/.extraheader"
          base64_pat=$(echo -n "pat:${{ secrets.PAT_REVIEW }}" | base64 -w0)

          git config "$config_key" \
            "Authorization: Basic $base64_pat"

          cd tests/Auto-GPT-test-cassettes
          git config "$config_key" \
            "Authorization: Basic $base64_pat"

          echo "config_key=$config_key" >> $GITHUB_OUTPUT

      - name: Push updated challenge scores
        if: github.event_name == 'push'
        run: |
          score_file="tests/challenges/current_score.json"

          if ! git diff --quiet $score_file; then
            git add $score_file
            git commit -m "Update challenge scores"
            git push origin HEAD:${{ github.ref_name }}
          else
            echo "The challenge scores didn't change."
          fi

      - id: push_cassettes
        name: Push updated cassettes
        # For pull requests, push updated cassettes even when tests fail
        if: github.event_name == 'push' || success() || failure()
        run: |
          if [ "${{ startsWith(github.event_name, 'pull_request') }}" = "true" ]; then
            is_pull_request=true
            cassette_branch="${{ github.event.pull_request.user.login }}-${{ github.event.pull_request.head.ref }}"
          else
            cassette_branch="${{ github.ref_name }}"
          fi

          cd tests/Auto-GPT-test-cassettes
          # Commit & push changes to cassettes if any
          if ! git diff --quiet; then
            git add .
            git commit -m "Auto-update cassettes"
            git push origin HEAD:$cassette_branch
            if [ ! $is_pull_request ]; then
              cd ../..
              git add tests/Auto-GPT-test-cassettes
              git commit -m "Update cassette submodule"
              git push origin HEAD:$cassette_branch
            fi
            echo "updated=true" >> $GITHUB_OUTPUT
          else
            echo "updated=false" >> $GITHUB_OUTPUT
            echo "No cassette changes to commit"
          fi

      - name: Post Set up git token auth
        if: steps.setup_git_auth.outcome == 'success'
        run: |
          git config --unset-all '${{ steps.setup_git_auth.outputs.config_key }}'
          git submodule foreach git config --unset-all '${{ steps.setup_git_auth.outputs.config_key }}'

      - name: Apply "behaviour change" label and comment on PR
        if: ${{ startsWith(github.event_name, 'pull_request') }}
        run: |
          PR_NUMBER=${{ github.event.pull_request.number }}
          TOKEN=${{ secrets.PAT_REVIEW }}
          REPO=${{ github.repository }}

          if [[ "${{ steps.push_cassettes.outputs.updated }}" == "true" ]]; then
            echo "Adding label and comment..."
            curl -X POST \
            -H "Authorization: Bearer $TOKEN" \
            -H "Accept: application/vnd.github.v3+json" \
            https://api.github.com/repos/$REPO/issues/$PR_NUMBER/labels \
            -d '{"labels":["behaviour change"]}'

            echo $TOKEN | gh auth login --with-token
            gh api repos/$REPO/issues/$PR_NUMBER/comments -X POST -F body="You changed AutoGPT's behaviour. The cassettes have been updated and will be merged to the submodule when this Pull Request gets merged."
          fi

      - name: Upload logs to artifact
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: test-logs
          path: logs/