Skip to content

Commit

Permalink
[CI] add GH workflow to comment with link to docs (#11594)
Browse files Browse the repository at this point in the history
  • Loading branch information
gigiblender authored Jun 15, 2022
1 parent 954a927 commit 3eb372e
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/docs_bot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

name: docs-bot
on:
status
jobs:
run-docs-bot:
if: ${{ github.repository == 'apache/tvm' && github.event.state == 'success' && github.event.context == 'tvm-ci/pr-head' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Comment link to docs
env:
COMMIT_SHA: ${{ github.event.sha }}
TARGET_URL: ${{ github.event.target_url }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -eux
python tests/scripts/github_docs_comment.py
35 changes: 35 additions & 0 deletions tests/python/ci/test_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@ def run(self, *args, **kwargs):
return proc


@pytest.mark.parametrize(
"target_url,base_url,commit_sha,expected_url,expected_body",
[
(
"https://ci.tlcpack.ai/job/tvm/job/PR-11594/3/display/redirect",
"https://pr-docs.tlcpack.ai",
"SHA",
"issues/11594/comments",
"Built docs for commit [SHA](SHA) can be found [here](https://pr-docs.tlcpack.ai/PR-11594/3/docs/index.html).",
)
],
)
def test_docs_comment(
tmpdir_factory, target_url, base_url, commit_sha, expected_url, expected_body
):
docs_comment_script = REPO_ROOT / "tests" / "scripts" / "github_docs_comment.py"

git = TempGit(tmpdir_factory.mktemp("tmp_git_dir"))
git.run("init")
git.run("checkout", "-b", "main")
git.run("remote", "add", "origin", "https://github.com/apache/tvm.git")
proc = subprocess.run(
[str(docs_comment_script), "--dry-run", f"--base-url-docs={base_url}"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env={"TARGET_URL": target_url, "COMMIT_SHA": commit_sha},
encoding="utf-8",
cwd=git.cwd,
)
if proc.returncode != 0:
raise RuntimeError(f"Process failed:\nstdout:\n{proc.stdout}\n\nstderr:\n{proc.stderr}")

assert f"Dry run, would have posted {expected_url} with data {expected_body}." in proc.stderr


def test_cc_reviewers(tmpdir_factory):
reviewers_script = REPO_ROOT / "tests" / "scripts" / "github_cc_reviewers.py"

Expand Down
85 changes: 85 additions & 0 deletions tests/scripts/github_docs_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import os
import logging
import argparse
import sys
from urllib import error

from git_utils import git, GitHubRepo, parse_remote
from cmd_utils import init_log


def build_docs_url(base_url_docs, pr_number, build_number):
return f"{base_url_docs}/PR-{str(pr_number)}/{str(build_number)}/docs/index.html"


def get_pr_and_build_numbers(target_url):
target_url = target_url[target_url.find("PR-") : len(target_url)]
split = target_url.split("/")
pr_number = split[0].strip("PR-")
build_number = split[1]
return {"pr_number": pr_number, "build_number": build_number}


if __name__ == "__main__":
help = "Add comment with link to docs"
parser = argparse.ArgumentParser(description=help)
parser.add_argument("--remote", default="origin", help="ssh remote to parse")
parser.add_argument("--base-url-docs", default="https://pr-docs.tlcpack.ai")
parser.add_argument(
"--dry-run",
action="store_true",
default=False,
help="run but don't send any request to GitHub",
)
args = parser.parse_args()
init_log()

remote = git(["config", "--get", f"remote.{args.remote}.url"])
user, repo = parse_remote(remote)

target_url = os.environ["TARGET_URL"]
pr_and_build = get_pr_and_build_numbers(target_url)

commit_sha = os.environ["COMMIT_SHA"]

docs_url = build_docs_url(
args.base_url_docs, pr_and_build["pr_number"], pr_and_build["build_number"]
)

url = f'issues/{pr_and_build["pr_number"]}/comments'
body = f"Built docs for commit [{commit_sha}]({commit_sha}) can be found [here]({docs_url})."
if not args.dry_run:
github = GitHubRepo(token=os.environ["GITHUB_TOKEN"], user=user, repo=repo)

# For now, only comment for PRs open by driazati, gigiblender and areusch.
get_pr_url = f'pulls/{pr_and_build["pr_number"]}'
pull_request_body = github.get(get_pr_url)
author = pull_request_body["user"]["login"]
if author not in ["driazati", "gigiblender", "areusch"]:
logging.info(f"Skipping this action for user {author}")
sys.exit(0)

try:
github.post(url, {"body": body})
except error.HTTPError as e:
logging.exception(f"Failed to add docs comment {docs_url}: {e}")
else:
logging.info(f"Dry run, would have posted {url} with data {body}.")

0 comments on commit 3eb372e

Please sign in to comment.