|
| 1 | +# Greet new pull requests that modify notebooks with a comment that includes |
| 2 | +# Colab preview links and instructions to use the tensorflow-docs notebook tools. |
| 3 | +name: Welcome |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + types: [opened] |
| 7 | + |
| 8 | +jobs: |
| 9 | + message: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v2 |
| 13 | + - name: Fetch master branch |
| 14 | + run: git fetch -u origin master:master |
| 15 | + - name: Post comment |
| 16 | + env: |
| 17 | + URL: "${{ github.event.pull_request.issue_url }}" |
| 18 | + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 19 | + # Source is the user's forked repo. |
| 20 | + SRC_REPO: "${{ github.event.pull_request.head.repo.full_name }}" |
| 21 | + SRC_BRANCH: "${{ github.event.pull_request.head.ref }}" |
| 22 | + # Dest is repo that's being submitted to. |
| 23 | + DEST_REPO: "${{ github.event.pull_request.base.repo.full_name }}" |
| 24 | + PR_NUM: "${{ github.event.pull_request.number }}" |
| 25 | + run: | |
| 26 | + # Only want notebooks modified in this pull request. |
| 27 | + readarray -t notebooks < <(git diff --name-only master | grep '\.ipynb$' || true) |
| 28 | + if [[ ${#notebooks[@]} -eq 0 ]]; then |
| 29 | + echo "No notebooks modified in this pull request." |
| 30 | + else |
| 31 | + msg="<h4>Preview</h4>\n" |
| 32 | + msg+="Preview and run these notebook edits with Google Colab:\n<ul>\n" |
| 33 | + # Link to PR branch in user's fork that is always current. |
| 34 | + for fp in "${notebooks[@]}"; do |
| 35 | + gh_path="${SRC_REPO}/blob/${SRC_BRANCH}/${fp}" |
| 36 | + colab_url="https://colab.research.google.com/github/${gh_path}" |
| 37 | + msg+="<li><a href='${colab_url}'>${fp}</a></li>\n" |
| 38 | + done |
| 39 | + msg+="</ul>\n" |
| 40 | +
|
| 41 | + msg+="<h4>Format and style</h4>\n" |
| 42 | + msg+="Use the TensorFlow docs <a href='https://github.com/tensorflow/docs/tree/master/tools/tensorflow_docs/tools'>notebook tools</a> to format for consistent source diffs and lint for style:\n" |
| 43 | + msg+="<pre>\n$ python3 -m pip install -U --user git+https://github.com/tensorflow/docs\n<br/>" |
| 44 | + msg+="$ python3 -m tensorflow_docs.tools.nbfmt notebook.ipynb\n<br/>" |
| 45 | + msg+="$ python3 -m tensorflow_docs.tools.nblint --arg=repo:tensorflow/docs notebook.ipynb\n</pre>\n" |
| 46 | +
|
| 47 | + reviewnb_url="https://app.reviewnb.com/${DEST_REPO}/pull/${PR_NUM}/files/" |
| 48 | + msg+="Rendered <a href='${reviewnb_url}'>notebook diffs</a> available on ReviewNB.com.\n" |
| 49 | +
|
| 50 | + # Escape string for JSON |
| 51 | + body="$(echo -n -e $msg | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))')" |
| 52 | + # Post comment |
| 53 | + curl -X POST \ |
| 54 | + -H "Accept: application/vnd.github.v3+json" \ |
| 55 | + -H "Authorization: token $GITHUB_TOKEN" \ |
| 56 | + "${URL}/comments" \ |
| 57 | + --data "{\"body\": $body}" |
| 58 | + fi |
0 commit comments