From 94b4c2259429fc175399a13bb87fca57e453a618 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Fri, 21 Jun 2024 21:42:09 +0300 Subject: [PATCH] =?UTF-8?q?ci:=20=F0=9F=91=B7=20check=20notebook=20and=20a?= =?UTF-8?q?utomatically=20create=20colab=20link=20and=20show=20better=20di?= =?UTF-8?q?fference=20in=20reviewNB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- .github/workflows/notebook-bot.yml | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 .github/workflows/notebook-bot.yml diff --git a/.github/workflows/notebook-bot.yml b/.github/workflows/notebook-bot.yml new file mode 100644 index 00000000..e4052a09 --- /dev/null +++ b/.github/workflows/notebook-bot.yml @@ -0,0 +1,64 @@ +name: Notebook Check Pull Request + +on: + pull_request_target: + types: [opened, reopened] + +permissions: + contents: read + +jobs: + comment-welcome: + permissions: + contents: read + pull-requests: write + + runs-on: ubuntu-latest + steps: + - name: Fetch pull request branch + uses: actions/checkout@v2 + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.event.pull_request.head.sha }} + - name: Fetch base develop branch + run: git fetch -u "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" develop:develop + - name: Create message + env: + HEAD_REPOSITORY: ${{ github.event.pull_request.head.repo.full_name }} + HEAD_REF: ${{ github.event.pull_request.head.ref }} + PR_NUM: ${{ github.event.pull_request.number }} + run: | + # Preview links and tool usage only needed for notebook changes. + readarray -t changed_notebooks < <(git diff --name-only develop | grep '\.ipynb$' || true) + if [[ ${#changed_notebooks[@]} == 0 ]]; then + echo "No notebooks modified in this pull request." + else + msg="

Preview

\n" + msg+="Preview and run these notebook edits with Google Colab:\n\n" + + reviewnb_url="https://app.reviewnb.com/${GITHUB_REPOSITORY}/pull/${PR_NUM}/files/" + msg+="Rendered notebook diffs available on ReviewNB.com.\n" + + msg+="If commits are added to the pull request, synchronize your local branch: git pull origin $HEAD_REF\n" + fi + echo "MESSAGE=$msg" >> $GITHUB_ENV + - name: Post comment + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + ISSUE_URL: ${{ github.event.pull_request.issue_url }} + run: | + # Env var defined in previous step. Escape string for JSON. + body="$(echo -n -e $MESSAGE | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))')" + # Add comment to pull request. + curl -X POST \ + -H "Accept: application/vnd.github.v3+json" \ + -H "Authorization: token $GITHUB_TOKEN" \ + "${ISSUE_URL}/comments" \ + --data "{\"body\": $body}"