opened #697
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
# Greet new pull requests with a welcome comment and apply labels. | |
# This workflow must initiate from an authenticated bot repo collaborator. | |
# Webhook events: Pull requests | |
name: New pull request | |
on: | |
repository_dispatch: | |
types: [opened, reopened] | |
permissions: | |
contents: read # to fetch code (actions/checkout) | |
jobs: | |
comment-welcome: | |
permissions: | |
contents: read # to fetch code (actions/checkout) | |
pull-requests: write # to comment on pull-request | |
if: ${{ github.actor == 'tfdocsbot' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Fetch pull request branch | |
uses: actions/checkout@v2 | |
with: | |
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }} | |
ref: ${{ github.event.client_payload.pull_request.head.sha }} | |
- name: Fetch base master branch | |
run: git fetch -u "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" master:master | |
- name: Create message | |
env: | |
HEAD_REPOSITORY: ${{ github.event.client_payload.pull_request.head.repo.full_name }} | |
HEAD_REF: ${{ github.event.client_payload.pull_request.head.ref }} | |
PR_NUM: ${{ github.event.client_payload.pull_request.number }} | |
run: | | |
# Preview links and tool usage only needed for notebook changes. | |
readarray -t changed_notebooks < <(git diff --name-only master | grep '\.ipynb$' || true) | |
if [[ ${#changed_notebooks[@]} == 0 ]]; then | |
echo "No notebooks modified in this pull request." | |
else | |
msg="<h4>Preview</h4>\n" | |
msg+="Preview and run these notebook edits with Google Colab:\n<ul>\n" | |
# Link to PR branch in user's fork that is always current. | |
for fp in "${changed_notebooks[@]}"; do | |
gh_path="${HEAD_REPOSITORY}/blob/${HEAD_REF}/${fp}" | |
colab_url="https://colab.research.google.com/github/${gh_path}" | |
msg+="<li><a href='${colab_url}'>${fp}</a></li>\n" | |
done | |
msg+="</ul>\n" | |
reviewnb_url="https://app.reviewnb.com/${GITHUB_REPOSITORY}/pull/${PR_NUM}/files/" | |
msg+="Rendered <a href='${reviewnb_url}'>notebook diffs</a> available on ReviewNB.com.\n" | |
msg+="<h4>Format and style</h4>\n" | |
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" | |
msg+="<pre>\n$ python3 -m pip install -U --user git+https://github.com/tensorflow/docs\n<br/>" | |
msg+="$ python3 -m tensorflow_docs.tools.nbfmt notebook.ipynb\n<br/>" | |
msg+="$ python3 -m tensorflow_docs.tools.nblint --arg=repo:tensorflow/docs notebook.ipynb\n</pre>\n" | |
msg+="If commits are added to the pull request, synchronize your local branch: <code>git pull origin $HEAD_REF</code>\n" | |
fi | |
echo "MESSAGE=$msg" >> $GITHUB_ENV | |
- name: Post comment | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
ISSUE_URL: ${{ github.event.client_payload.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}" |