Skip to content

Commit

Permalink
[CI] Docs bot now edits previous comments (#11909)
Browse files Browse the repository at this point in the history
This PR improves the docs bot to edit a previous comment instead of making new comments.

Fixes #11837
  • Loading branch information
gigiblender committed Jun 29, 2022
1 parent ae33f40 commit 54f8176
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tests/python/ci/test_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def parameterize_named(*values):
"https://pr-docs.tlcpack.ai",
"SHA",
"issues/11594/comments",
"Built docs for commit SHA can be found "
"<!---docs-bot-comment-->\n\nBuilt docs for commit SHA can be found "
"[here](https://pr-docs.tlcpack.ai/PR-11594/3/docs/index.html).",
)
],
Expand Down
3 changes: 3 additions & 0 deletions tests/scripts/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def _request(self, full_url: str, body: Dict[str, Any], method: str) -> Dict[str
def put(self, url: str, data: Dict[str, Any]) -> Dict[str, Any]:
return self._request(self.base + url, data, method="PUT")

def patch(self, url: str, data: Dict[str, Any]) -> Dict[str, Any]:
return self._request(self.base + url, data, method="PATCH")

def post(self, url: str, data: Dict[str, Any]) -> Dict[str, Any]:
return self._request(self.base + url, data, method="POST")

Expand Down
33 changes: 29 additions & 4 deletions tests/scripts/github_docs_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@
from git_utils import git, GitHubRepo, parse_remote
from cmd_utils import init_log

DOCS_BOT_MARKER = "<!---docs-bot-comment-->\n\n"
GITHUB_ACTIONS_BOT_LOGIN = "github-actions[bot]"


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_comments(github, url):
try:
return github.get(url)
except error.HTTPError as e:
logging.exception(f"Failed to retrieve PR comments: {url}: {e}")
return []


def get_pr_and_build_numbers(target_url):
target_url = target_url[target_url.find("PR-") : len(target_url)]
split = target_url.split("/")
Expand All @@ -38,6 +49,16 @@ def get_pr_and_build_numbers(target_url):
return {"pr_number": pr_number, "build_number": build_number}


def search_for_docs_comment(comments):
for comment in comments:
if (
comment["user"]["login"] == GITHUB_ACTIONS_BOT_LOGIN
and DOCS_BOT_MARKER in comment["body"]
):
return comment
return None


if __name__ == "__main__":
help = "Add comment with link to docs"
parser = argparse.ArgumentParser(description=help)
Expand Down Expand Up @@ -65,7 +86,7 @@ def get_pr_and_build_numbers(target_url):
)

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

Expand All @@ -77,9 +98,13 @@ def get_pr_and_build_numbers(target_url):
logging.info(f"Skipping this action for user {author}")
sys.exit(0)

try:
pr_comments = get_pr_comments(github, url)
comment = search_for_docs_comment(pr_comments)

if comment is not None:
comment_url = comment["url"]
github.patch(comment_url, {"body": body})
else:
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 54f8176

Please sign in to comment.