Automatically merge plugin-only PRs #391
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Automatically merge plugin-only PRs | |
# Triggered on all PRs either by | |
# - completion of CI checks, OR | |
# - tagging with label | |
# 1) If PR is labeled "automerge" or "automerge-web" | |
# (all website submissions are tagged "automerge-web"), | |
# checks if Travis tests pass. If yes, THEN | |
# 2) Checks if PR modifies any code outside plugin dirs. | |
# If no changes are made beyond new or revised plugins | |
# (subdirs of /benchmarks, /data, /models, or /metrics) | |
# the PR is automatically approved and merged. | |
on: | |
pull_request: | |
types: [labeled] | |
status: | |
permissions: write-all | |
jobs: | |
setup: | |
name: setup | |
runs-on: ubuntu-latest | |
outputs: | |
BSC_DATABASESECRET: ${{ steps.setenvvars.outputs.BSC_DATABASESECRET }} | |
steps: | |
- name: Check out repository code | |
uses: actions/checkout@v2 | |
- name: Check if main & set dev/prod vars | |
id: setenvvars | |
run: | | |
if [ ${{ github.ref }} != 'refs/heads/main' ]; then | |
echo "is NOT main branch" | |
echo "::set-output name=BSC_DATABASESECRET:: ${{ secrets.BSC_DATABASESECRET_DEV }}" | |
else | |
echo "is main branch" | |
echo "::set-output name=BSC_DATABASESECRET:: ${{ secrets.BSC_DATABASESECRET_PROD }}" | |
fi | |
isautomerge: | |
name: Set as 'automerge' if PR is labeled with 'automerge' or 'automerge-web' | |
runs-on: ubuntu-latest | |
if: | | |
contains( github.event.pull_request.labels.*.name, 'automerge') || | |
contains( github.event.pull_request.labels.*.name, 'automerge-web') | |
outputs: | |
AUTOMERGE: ${{ steps.setautomerge.outputs.AUTOMERGE }} | |
steps: | |
- name: Set 'automerge' to 'True' # job only runs if True | |
id: setautomerge | |
run: | | |
echo "::set-output name=AUTOMERGE::True" | |
travis_success: | |
name: Check if Travis build is successful | |
runs-on: ubuntu-latest | |
needs: [isautomerge] | |
if: ${{ needs.isautomerge.outputs.AUTOMERGE == 'True' }} | |
outputs: | |
TRAVIS_OK: ${{ steps.istravisok.outputs.TRAVIS_OK }} | |
steps: | |
- name: Get Travis build status | |
id: gettravisstatus | |
run: | | |
echo ${{ github.event.pull_request.head.sha }} | |
echo "TRAVIS_CONCLUSION=$(curl -X GET https://api.github.com/repos/brain-score/language/commits/${{ github.event.pull_request.head.sha }}/check-runs | python -c "from __future__ import print_function; import sys,json; print(next(run['conclusion'] for run in json.load(sys.stdin)['check_runs'] if run['name'] == 'Travis CI - Pull Request'))")" >> $GITHUB_ENV | |
- name: Check if Travis was successful | |
id: istravisok | |
run: | | |
if [ "$TRAVIS_CONCLUSION" == "success" ] | |
then | |
travisok=True | |
elif [ "$TRAVIS_CONCLUSION" == "None" ] | |
then | |
travisok=Wait | |
else | |
travisok=False | |
fi | |
echo "::set-output name=TRAVIS_OK::$travisok" | |
email_on_failure: | |
name: If tests fail on an automerge-web PR, send email to submitter | |
runs-on: ubuntu-latest | |
needs: [setup, travis_success] | |
env: | |
BSC_DATABASESECRET: ${{ needs.setup.outputs.BSC_DATABASESECRET }} | |
if: | | |
needs.travis_success.outputs.TRAVIS_OK == 'False' && | |
join(github.event.pull_request.labels.*.name, ',') == 'automerge-web' | |
steps: | |
- name: Parse Brain-Score user ID from PR title | |
run: | | |
echo "BS_UID="$(<<<${{ github.event.pull_request.title }} | sed -E 's/.*\(user:([^)]+)\).*/\1/'"" >> $GITHUB_ENV | |
- name: Build project | |
run: | | |
python -m pip install "." | |
- name: Get email from Brain-Score UID | |
run: | | |
echo "TO_EMAIL=$(python .github/workflows/call_endpoints.py get_user_email '${{ env.BS_UID }}')" >> $GITHUB_ENV | |
- name: Send email to Brain-Score user | |
uses: dawidd6/action-send-mail@v3 | |
with: | |
# Required mail server address: | |
server_address: smtp.gmail.com | |
# Server port, default 25: | |
server_port: 465 | |
# Optional whether this connection use TLS (default is true if server_port is 465) | |
secure: true | |
# Optional (recommended) mail server username: | |
username: ${{ secrets.MAIL_USERNAME }} | |
# Optional (recommended) mail server password: | |
password: ${{ secrets.MAIL_PASSWORD }} | |
# Required mail subject: | |
subject: Brain-Score submission failed | |
# Required recipients' addresses: | |
to: ${{ env.BS_UID }} | |
# Required sender full name: | |
from: Brain-Score | |
# Optional plain body: | |
body: Your Brain-Score submission did not pass checks. Please review the test results and update the PR at https://github.com/brain-score/language/pull/${{ github.event.number }} or send in an updated submission via the website. | |
checkchanges: | |
name: Check if PR only changes plugin files | |
runs-on: ubuntu-latest | |
needs: travis_success | |
if: ${{ needs.travis_success.outputs.TRAVIS_OK == 'True' }} | |
outputs: | |
PLUGIN_INFO: ${{ steps.getpluginfo.outputs.PLUGIN_INFO }} | |
IS_PLUGIN_ONLY: ${{ steps.ispluginonly.outputs.IS_PLUGIN_ONLY }} | |
steps: | |
- name: Check out repository code | |
uses: actions/checkout@v2 | |
- name: Get changed files | |
uses: dorny/[email protected] | |
id: filter | |
with: | |
list-files: shell | |
filters: | | |
changed: | |
- '**' | |
- name: Save changed files to env var | |
run: echo "CHANGED_FILES=${{ steps.filter.outputs.changed_files }}" >> $GITHUB_ENV | |
- name: Parse changed files with python script | |
run: | | |
echo "PLUGIN_INFO=$(python .github/workflows/parse_changed_files.py '${{ env.CHANGED_FILES }}')" >> $GITHUB_ENV | |
- name: Save plugin info to outputs | |
id: getpluginfo | |
run: | | |
echo "PLUGIN_INFO=$PLUGIN_INFO" >> $GITHUB_OUTPUT | |
- name: check if plugin-only | |
id: ispluginonly | |
run: | | |
echo "IS_PLUGIN_ONLY=$(jq -r '.is_plugin_only' <<< "$PLUGIN_INFO")" >> $GITHUB_OUTPUT | |
automerge: | |
name: If plugin-only, approve and merge | |
runs-on: ubuntu-latest | |
needs: checkchanges | |
if: ${{ needs.checkchanges.outputs.IS_PLUGIN_ONLY == 'True' }} | |
steps: | |
- name: Auto Approve | |
uses: hmarr/[email protected] | |
- name: Auto Merge (GitHub submissions) | |
uses: plm9606/[email protected] | |
with: | |
github-token: ${{ secrets.WORKFLOW_TOKEN }} | |
label-name: "automerge" | |
merge-method: "squash" | |
auto-delete: "true" | |
- name: Auto Merge (brain-score.org submissions) | |
uses: plm9606/[email protected] | |
with: | |
github-token: ${{ secrets.WORKFLOW_TOKEN }} | |
label-name: "automerge-web" | |
merge-method: "squash" | |
auto-delete: "true" |