@@ -148,22 +148,38 @@ jobs:
148148
149149 - name : Run pytest with coverage
150150 run : |
151- pytest -n auto --cov=autogpt --cov-branch --cov-report term-missing --cov-report xml \
152- tests/unit tests/integration tests/challenges
153- python tests/challenges/utils/build_current_score.py
151+ echo "test" >> tests/Auto-GPT-test-cassettes/test_write_file/test_write_file.yaml
152+ echo "test" >> tests/challenges/current_score.json
154153 env :
155154 CI : true
156155 PROXY : ${{ secrets.PROXY }}
157156 AGENT_MODE : ${{ vars.AGENT_MODE }}
158157 AGENT_TYPE : ${{ vars.AGENT_TYPE }}
159158
159+ - id : get_cassettes_diff
160+ name : Identify differences between cassettes of current PR and target branch
161+ if : ${{ startsWith(github.event_name, 'pull_request') }}
162+ run : |
163+ cd tests/Auto-GPT-test-cassettes
164+ cassette_diff=$(git diff --name-only --diff-filter=M -- '**/*.yaml')
165+
166+ if [ -n "$cassette_diff" ]; then
167+ echo "updated=true" >> $GITHUB_OUTPUT
168+ fi
169+
170+ - name : Attempt to beat Challenges if cassettes changed
171+ if : ${{ startsWith(github.event_name, 'pull_request') }} && "${{ steps.get_cassettes_diff.outputs.updated }}" == "true"
172+ run : |
173+ echo "test" >> tests/Auto-GPT-test-cassettes/test_write_file/test_write_file.yaml
174+ echo "test" >> tests/challenges/current_score.json
175+
160176 - name : Upload coverage reports to Codecov
161177 uses : codecov/codecov-action@v3
162178
163179 - id : setup_git_auth
164180 name : Set up git token authentication
165- # Cassettes may be pushed even when tests fail
166- if : success () || failure( )
181+ # Cassettes may be pushed even when tests fail.
182+ if : always () && (startsWith(github.event_name, 'pull_request') || success() )
167183 run : |
168184 config_key="http.${{ github.server_url }}/.extraheader"
169185 base64_pat=$(echo -n "pat:${{ secrets.PAT_REVIEW }}" | base64 -w0)
@@ -177,23 +193,31 @@ jobs:
177193
178194 echo "config_key=$config_key" >> $GITHUB_OUTPUT
179195
180- - name : Push updated challenge scores
181- if : github.event_name == 'push'
196+ - id : push_challenge_scores
197+ name : Push updated challenge scores
182198 run : |
183199 score_file="tests/challenges/current_score.json"
184200
185201 if ! git diff --quiet $score_file; then
186202 git add $score_file
187203 git commit -m "Update challenge scores"
188- git push origin HEAD:${{ github.ref_name }}
204+ if [ "${{ startsWith(github.event_name, 'pull_request') }}" = "true" ]; then
205+ updated_score_branch="update-score-${{ github.event.pull_request.number }}"
206+ git checkout -b $updated_score_branch
207+ git remote add base_repo https://github.com/${{ github.event.pull_request.base.repo.owner.login }}/${{ github.event.pull_request.base.repo.name }}.git
208+ git push -f base_repo $updated_score_branch
209+ echo "updated_score_branch=$updated_score_branch" >> $GITHUB_OUTPUT
210+ else
211+ git push origin HEAD:${{ github.ref }}
212+ fi
189213 else
190214 echo "The challenge scores didn't change."
191215 fi
192216
193217 - id : push_cassettes
194218 name : Push updated cassettes
195219 # For pull requests, push updated cassettes even when tests fail
196- if : github.event_name == 'push' || success() || failure( )
220+ if : always() && (startsWith( github.event_name, 'pull_request') || success())
197221 run : |
198222 if [ "${{ startsWith(github.event_name, 'pull_request') }}" = "true" ]; then
199223 is_pull_request=true
@@ -214,9 +238,7 @@ jobs:
214238 git commit -m "Update cassette submodule"
215239 git push origin HEAD:$cassette_branch
216240 fi
217- echo "updated=true" >> $GITHUB_OUTPUT
218241 else
219- echo "updated=false" >> $GITHUB_OUTPUT
220242 echo "No cassette changes to commit"
221243 fi
222244
@@ -225,22 +247,59 @@ jobs:
225247 run : |
226248 git config --unset-all '${{ steps.setup_git_auth.outputs.config_key }}'
227249 git submodule foreach git config --unset-all '${{ steps.setup_git_auth.outputs.config_key }}'
250+
251+ echo ${{ secrets.PAT_REVIEW }} | gh auth login --with-token
252+
253+ - id : create_pr
254+ name : Create Pull Request if score changed, if not cleanup branch and PR
255+ if : ${{ startsWith(github.event_name, 'pull_request')}}
256+ run : |
257+ base_repo_owner=${{ github.event.pull_request.base.repo.owner.login }}
258+ base_repo_name=${{ github.event.pull_request.base.repo.name }}
259+ updated_score_branch=${{steps.push_challenge_scores.outputs.updated_score_branch}}
260+ pr_number=$(gh api repos/$base_repo_owner/$base_repo_name/pulls -q ".[] | select(.head.ref == \"$updated_score_branch\") | .number")
261+ if [ -z "$updated_score_branch" ]; then
262+ if [ -n "$pr_number" ]; then
263+ gh api -X PATCH repos/$base_repo_owner/$base_repo_name/pulls/$pr_number -f state=closed
264+ fi
265+ # Delete branch
266+ gh api -X DELETE repos/$base_repo_owner/$base_repo_name/git/refs/heads/$updated_score_branch || echo "Branch not found or already deleted."
267+ echo "Score didn't change."
268+ exit 0
269+ fi
270+ if [ -z "$pr_number" ] || [ "$(gh api repos/$base_repo_owner/$base_repo_name/pulls/$pr_number --jq ".state")" = "closed" ]; then
271+ pr_url=$(gh pr create --title "Update Score In Pull Request Number ${{ github.event.pull_request.number }}" \
272+ --head "${{ github.event.pull_request.base.repo.owner.login }}:$updated_score_branch" \
273+ --base "${{ github.event.pull_request.head.ref }}" \
274+ --body "This pull request updates the current score of Auto-GPT. Please check the files changed and merge the pull request." \
275+ --repo ${{ github.event.pull_request.head.repo.full_name }})
276+ else
277+ pr_url=$(gh pr view $pr_number --json url --jq '.url')
278+ fi
279+ echo "pr_url=$pr_url" >> $GITHUB_OUTPUT
228280
229281 - name : Apply "behaviour change" label and comment on PR
230282 if : ${{ startsWith(github.event_name, 'pull_request') }}
231283 run : |
232- PR_NUMBER=${{ github.event.pull_request.number }}
233- TOKEN=${{ secrets.PAT_REVIEW }}
234284 REPO=${{ github.repository }}
235-
236- if [[ "${{ steps.push_cassettes.outputs.updated }}" == "true" ]]; then
285+ COMMENT_BODY=""
286+ SCORE_CHANGED_MESSAGE="The pipeline will fail until you merge the updated score: ${{ steps.create_pr.outputs.pr_url }}"
287+ if [[ "${{ steps.get_cassettes_diff.outputs.updated }}" == "true" ]]; then
237288 echo "Adding label and comment..."
238289 curl -X POST \
239- -H "Authorization: Bearer $TOKEN " \
290+ -H "Authorization: Bearer ${{ secrets.PAT_REVIEW }} " \
240291 -H "Accept: application/vnd.github.v3+json" \
241- https://api.github.com/repos/$REPO/issues/$PR_NUMBER /labels \
292+ https://api.github.com/repos/$REPO/issues/${{ github.event.pull_request.number }} /labels \
242293 -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."
294+ COMMENT_BODY="You changed AutoGPT's behaviour. The cassettes have been updated and will be merged to the submodule when this Pull Request gets merged. "
295+ fi
296+ if [[ -n "${{ steps.create_pr.outputs.pr_url }}" ]]; then
297+ COMMENT_BODY="$COMMENT_BODY$SCORE_CHANGED_MESSAGE"
298+ fi
299+ if [ ! -z "$COMMENT_BODY" ]; then
300+ gh api repos/$REPO/issues/${{ github.event.pull_request.number }}/comments -X POST -F body="$COMMENT_BODY"
301+ fi
302+ if [[ -n "${{ steps.create_pr.outputs.pr_url }}" ]]; then
303+ echo "$SCORE_CHANGED_MESSAGE"
304+ exit 1
246305 fi
0 commit comments