@@ -105,7 +105,7 @@ jobs:
105105 git config --global user.email "github-bot@agpt.co"
106106
107107 - name : Checkout cassettes
108- if : ${{ startsWith(github.event_name, 'pull_request') }}
108+ if : startsWith(github.event_name, 'pull_request')
109109 run : |
110110 cassette_branch="${{ github.event.pull_request.user.login }}-${{ github.event.pull_request.head.ref }}"
111111 cd tests/Auto-GPT-test-cassettes
@@ -157,13 +157,40 @@ jobs:
157157 AGENT_MODE : ${{ vars.AGENT_MODE }}
158158 AGENT_TYPE : ${{ vars.AGENT_TYPE }}
159159
160+ - id : get_cassettes_diff
161+ name : Detect if cassettes changed
162+ if : startsWith(github.event_name, 'pull_request')
163+ run : |
164+ cd tests/Auto-GPT-test-cassettes
165+ cassette_diff=$(git diff --name-only --diff-filter=M -- '**/*.yaml')
166+
167+ if [ -n "$cassette_diff" ]; then
168+ echo "Cassettes changed: $cassette_diff"
169+ echo "cassettes_changed=true" >> $GITHUB_OUTPUT
170+ else
171+ echo "Cassettes did not change."
172+ echo "cassettes_changed=false" >> $GITHUB_OUTPUT
173+ fi
174+
175+ - name : Attempt to beat Challenges if cassettes changed
176+ if : startsWith(github.event_name, 'pull_request') && steps.get_cassettes_diff.outputs.cassettes_changed == 'true'
177+ run : |
178+ pytest -n auto --beat-challenges --cov=autogpt --cov-branch --cov-report term-missing --cov-report xml \
179+ tests/unit tests/integration tests/challenges
180+ python tests/challenges/utils/build_current_score.py
181+ env :
182+ CI : true
183+ PROXY : ${{ secrets.PROXY }}
184+ AGENT_MODE : ${{ vars.AGENT_MODE }}
185+ AGENT_TYPE : ${{ vars.AGENT_TYPE }}
186+
160187 - name : Upload coverage reports to Codecov
161188 uses : codecov/codecov-action@v3
162189
163190 - id : setup_git_auth
164191 name : Set up git token authentication
165- # Cassettes may be pushed even when tests fail
166- if : success () || failure( )
192+ # Cassettes may be pushed even when tests fail.
193+ if : always () && (startsWith(github.event_name, 'pull_request') || success() )
167194 run : |
168195 config_key="http.${{ github.server_url }}/.extraheader"
169196 base64_pat=$(echo -n "pat:${{ secrets.PAT_REVIEW }}" | base64 -w0)
@@ -177,23 +204,33 @@ jobs:
177204
178205 echo "config_key=$config_key" >> $GITHUB_OUTPUT
179206
180- - name : Push updated challenge scores
181- if : github.event_name == 'push'
207+ - id : push_challenge_scores
208+ name : Push updated challenge scores
182209 run : |
183210 score_file="tests/challenges/current_score.json"
184-
211+ challenge_scores_branch="challenge-scores-${{ github.event.pull_request.number }}"
212+ echo "challenge_scores_branch=$challenge_scores_branch" >> $GITHUB_OUTPUT
213+
185214 if ! git diff --quiet $score_file; then
186215 git add $score_file
187216 git commit -m "Update challenge scores"
188- git push origin HEAD:${{ github.ref_name }}
217+ if [ "${{ startsWith(github.event_name, 'pull_request') }}" = "true" ]; then
218+ git checkout -b $challenge_scores_branch
219+ git remote add base_repo \
220+ https://github.com/${{ github.event.pull_request.base.repo.owner.login }}/${{ github.event.pull_request.base.repo.name }}.git
221+ git push -f base_repo $challenge_scores_branch
222+ echo "updated_score=true" >> $GITHUB_OUTPUT
223+ else
224+ git push origin HEAD:${{ github.ref }}
225+ fi
189226 else
190227 echo "The challenge scores didn't change."
191228 fi
192229
193230 - id : push_cassettes
194231 name : Push updated cassettes
195232 # For pull requests, push updated cassettes even when tests fail
196- if : github.event_name == 'push' || success() || failure( )
233+ if : always() && (startsWith( github.event_name, 'pull_request') || success())
197234 run : |
198235 if [ "${{ startsWith(github.event_name, 'pull_request') }}" = "true" ]; then
199236 is_pull_request=true
@@ -214,9 +251,7 @@ jobs:
214251 git commit -m "Update cassette submodule"
215252 git push origin HEAD:$cassette_branch
216253 fi
217- echo "updated=true" >> $GITHUB_OUTPUT
218254 else
219- echo "updated=false" >> $GITHUB_OUTPUT
220255 echo "No cassette changes to commit"
221256 fi
222257
@@ -225,22 +260,72 @@ jobs:
225260 run : |
226261 git config --unset-all '${{ steps.setup_git_auth.outputs.config_key }}'
227262 git submodule foreach git config --unset-all '${{ steps.setup_git_auth.outputs.config_key }}'
263+
264+ echo ${{ secrets.PAT_REVIEW }} | gh auth login --with-token
265+
266+ - id : create_pr
267+ name : Create Pull Request if score changed, if not cleanup branch and PR
268+ if : startsWith(github.event_name, 'pull_request')
269+ run : |
270+ base_repo_owner=${{ github.event.pull_request.base.repo.owner.login }}
271+ base_repo_name=${{ github.event.pull_request.base.repo.name }}
272+ challenge_scores_branch=${{steps.push_challenge_scores.outputs.challenge_scores_branch}}
273+
274+ pr_number=$(gh api repos/$base_repo_owner/$base_repo_name/pulls -q ".[] | select(.head.ref == \"$challenge_scores_branch\") | .number")
275+
276+ if [ -z "${{steps.push_challenge_scores.outputs.updated_score}}" ]; then
277+ if [ -n "$pr_number" ]; then
278+ gh api -X PATCH repos/$base_repo_owner/$base_repo_name/pulls/$pr_number -f state=closed
279+ echo "Close PR $pr_number if not already closed"
280+ fi
281+ # Delete branch
282+ gh api -X DELETE repos/$base_repo_owner/$base_repo_name/git/refs/heads/$challenge_scores_branch \
283+ && echo "Branch $challenge_scores_branch deleted." \
284+ || echo "Branch $challenge_scores_branch not found or already deleted."
285+ echo "Score didn't change."
286+ exit 0
287+ fi
288+
289+ if [ -z "$pr_number" ] || [ "$(gh api repos/$base_repo_owner/$base_repo_name/pulls/$pr_number --jq ".state")" = "closed" ]; then
290+ pr_url=$(gh pr create --title "Update Score In Pull Request Number ${{ github.event.pull_request.number }}" \
291+ --head "$base_repo_owner:$challenge_scores_branch" \
292+ --base "${{ github.event.pull_request.head.ref }}" \
293+ --body "This pull request updates the challenge scores of Auto-GPT. Please check the files changed and merge the pull request." \
294+ --repo ${{ github.event.pull_request.head.repo.full_name }})
295+ else
296+ pr_url=$(gh pr view $pr_number --json url --jq '.url')
297+ fi
298+
299+ echo "pr_url=$pr_url" >> $GITHUB_OUTPUT
228300
229301 - name : Apply "behaviour change" label and comment on PR
230- if : ${{ startsWith(github.event_name, 'pull_request') }}
302+ if : startsWith(github.event_name, 'pull_request')
231303 run : |
232- PR_NUMBER=${{ github.event.pull_request.number }}
233- TOKEN=${{ secrets.PAT_REVIEW }}
234304 REPO=${{ github.repository }}
235-
236- if [[ "${{ steps.push_cassettes.outputs.updated }}" == "true" ]]; then
305+ COMMENT_BODY=""
306+ SCORE_CHANGED_MESSAGE="The pipeline will fail until you merge the challenge scores: ${{ steps.create_pr.outputs.pr_url }}"
307+ if [[ "${{ steps.get_cassettes_diff.outputs.cassettes_changed }}" == "true" ]]; then
237308 echo "Adding label and comment..."
238309 curl -X POST \
239- -H "Authorization: Bearer $TOKEN " \
310+ -H "Authorization: Bearer ${{ secrets.PAT_REVIEW }} " \
240311 -H "Accept: application/vnd.github.v3+json" \
241- https://api.github.com/repos/$REPO/issues/$PR_NUMBER /labels \
312+ https://api.github.com/repos/$REPO/issues/${{ github.event.pull_request.number }} /labels \
242313 -d '{"labels":["behaviour change"]}'
243-
244- echo $TOKEN | gh auth login --with-token
245- 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."
314+ COMMENT_BODY="You changed Auto-GPT's behaviour. The cassettes have been updated and will be merged to the submodule when this Pull Request gets merged. "
315+ echo "Added comment and label informing of cassette update."
316+ else
317+ echo "Cassettes did not change so no label or comment added."
318+ fi
319+ if [[ -n "${{ steps.create_pr.outputs.pr_url }}" ]]; then
320+ COMMENT_BODY="$COMMENT_BODY$SCORE_CHANGED_MESSAGE"
321+ echo "Added comment informing of Pull Request to update the challenge scores."
322+ else
323+ echo "Challenge scores did not change so no comment added."
324+ fi
325+ if [ ! -z "$COMMENT_BODY" ]; then
326+ gh api repos/$REPO/issues/${{ github.event.pull_request.number }}/comments -X POST -F body="$COMMENT_BODY"
327+ fi
328+ if [[ -n "${{ steps.create_pr.outputs.pr_url }}" ]]; then
329+ echo "$SCORE_CHANGED_MESSAGE"
330+ exit 1
246331 fi
0 commit comments