|
| 1 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import argparse |
| 17 | +import importlib |
| 18 | +import inspect |
| 19 | +import re |
| 20 | +from typing import TYPE_CHECKING, Any |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from collections.abc import Callable |
| 24 | + |
| 25 | +import paddle # noqa: F401 |
| 26 | + |
| 27 | + |
| 28 | +def load_api_by_name(path: str) -> Callable[..., Any] | None: |
| 29 | + """ |
| 30 | + Recursively resolves a string path to a Python object. |
| 31 | + """ |
| 32 | + if not path: |
| 33 | + return None |
| 34 | + |
| 35 | + # First, try to import the entire path as a module (e.g., "paddle" or "paddle.autograd"). |
| 36 | + try: |
| 37 | + return importlib.import_module(path) |
| 38 | + except ImportError: |
| 39 | + # If the import fails, it might be an object within a module. |
| 40 | + # If there's no dot, it was a failed top-level import, so we can't proceed. |
| 41 | + if "." not in path: |
| 42 | + return None |
| 43 | + |
| 44 | + # Split the path into its parent and the final object name. |
| 45 | + # e.g., "paddle.Tensor" -> parent="paddle", child="Tensor" |
| 46 | + parent_path, child_name = path.rsplit('.', 1) |
| 47 | + parent_obj = load_api_by_name(parent_path) |
| 48 | + |
| 49 | + # If the parent object could not be resolved, we can't find the child. |
| 50 | + if parent_obj is None: |
| 51 | + return None |
| 52 | + |
| 53 | + # Use getattr with a default value to safely get the child object. |
| 54 | + return getattr(parent_obj, child_name, None) |
| 55 | + |
| 56 | + |
| 57 | +def generate_comment_body(doc_diff: str, pr_id: int) -> str: |
| 58 | + if not doc_diff: |
| 59 | + return "" |
| 60 | + |
| 61 | + output_lines: list[str] = [] |
| 62 | + base_url = f"http://preview-paddle-pr-{pr_id}.paddle-docs-preview.paddlepaddle.org.cn/documentation/docs/en/api" |
| 63 | + |
| 64 | + # Extract API names like 'paddle.autograd.backward' from lines like: |
| 65 | + # - paddle.autograd.backward (ArgSpec(...), ('document', ...)) |
| 66 | + # + paddle.autograd.backward (ArgSpec(...), ('document', ...)) |
| 67 | + apis: list[str] = sorted( |
| 68 | + set(re.findall(r"^[+]\s*([a-zA-Z0-9_.]+)\s*\(", doc_diff, re.MULTILINE)) |
| 69 | + ) |
| 70 | + # All apis should be loaded, this seems a explicitly check. |
| 71 | + unload_apis: list[str] = [] |
| 72 | + |
| 73 | + if not apis: |
| 74 | + return "" |
| 75 | + |
| 76 | + for api in apis: |
| 77 | + api_obj = load_api_by_name(api) |
| 78 | + |
| 79 | + if api_obj is None: |
| 80 | + unload_apis.append(api) |
| 81 | + continue |
| 82 | + |
| 83 | + api_path = api.replace('.', '/') |
| 84 | + url = f"{base_url}/{api_path}_en.html" |
| 85 | + |
| 86 | + if "." in api: |
| 87 | + parent_path, child_name = api.rsplit('.', 1) |
| 88 | + parent_obj = load_api_by_name(parent_path) |
| 89 | + if inspect.isclass(parent_obj) and not inspect.isclass(api_obj): |
| 90 | + parent_api_path = parent_path.replace('.', '/') |
| 91 | + url = f"{base_url}/{parent_api_path}_en.html#{child_name}" |
| 92 | + |
| 93 | + output_lines.append(f"- **{api}**: [Preview]({url})") |
| 94 | + unload_error_msg = ( |
| 95 | + f"@ooooo-create, following apis cannot be loaded, please check it: {', '.join(unload_apis)}" |
| 96 | + if unload_apis |
| 97 | + else "" |
| 98 | + ) |
| 99 | + |
| 100 | + if not output_lines: |
| 101 | + return unload_error_msg |
| 102 | + |
| 103 | + api_links = "\n".join(output_lines) |
| 104 | + comment_body = f"""<details> |
| 105 | +<summary>📚 Preview documentation links for API changes in this PR (Click to expand)</summary> |
| 106 | +
|
| 107 | +{unload_error_msg} |
| 108 | +
|
| 109 | +<table> |
| 110 | +<tr> |
| 111 | +<td> |
| 112 | +ℹ️ <b>Preview Notice</b><br> |
| 113 | +Please wait for the <code>Doc-Preview</code> workflow to complete before clicking the preview links below, otherwise you may see outdated content. |
| 114 | +</td> |
| 115 | +</tr> |
| 116 | +</table> |
| 117 | +
|
| 118 | +The following are preview links for new or modified API documentation in this PR: |
| 119 | +
|
| 120 | +{api_links} |
| 121 | +
|
| 122 | +</details>""" |
| 123 | + |
| 124 | + return comment_body |
| 125 | + |
| 126 | + |
| 127 | +def cli(): |
| 128 | + parser = argparse.ArgumentParser( |
| 129 | + description="Generate documentation comment for PR with API changes" |
| 130 | + ) |
| 131 | + parser.add_argument( |
| 132 | + "doc_diff_path", help="Path to the documentation diff file", type=str |
| 133 | + ) |
| 134 | + parser.add_argument("pr_id", help="Pull request ID", type=int) |
| 135 | + return parser.parse_args() |
| 136 | + |
| 137 | + |
| 138 | +def main(): |
| 139 | + args = cli() |
| 140 | + |
| 141 | + with open(args.doc_diff_path, 'r') as f: |
| 142 | + doc_diff_content = f.read() |
| 143 | + |
| 144 | + comment = generate_comment_body(doc_diff_content, args.pr_id) |
| 145 | + print(comment) |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + main() |
0 commit comments