This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris-website.git


The following commit(s) were added to refs/heads/master by this push:
     new fdd8d33e3e1 [ci] add move/delete caused deadlink check to Build 
Check(Required) (#1048)
fdd8d33e3e1 is described below

commit fdd8d33e3e157896a4b1beef7aafd3ea20e20c5f
Author: zclllhhjj <[email protected]>
AuthorDate: Sun Sep 1 23:28:25 2024 +0800

    [ci] add move/delete caused deadlink check to Build Check(Required) (#1048)
---
 .github/workflows/build-check.yml |  22 +++++--
 check_move.py                     | 126 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 144 insertions(+), 4 deletions(-)

diff --git a/.github/workflows/build-check.yml 
b/.github/workflows/build-check.yml
index d525210a08f..bc8f7a65db0 100644
--- a/.github/workflows/build-check.yml
+++ b/.github/workflows/build-check.yml
@@ -31,11 +31,10 @@ jobs:
         runs-on: ubuntu-latest
         environment: Production
         steps:
-            - name: Checkout
-              uses: actions/checkout@master
+            - name: Checkout ${{ github.ref }} ( ${{ 
github.event.pull_request.head.sha }} )
+              uses: actions/checkout@v3
               with:
-                  persist-credentials: false
-                  submodules: recursive
+                ref: ${{ github.event.pull_request.head.sha }}
 
             - name: clone common docs to versioned_docs
               run: |
@@ -90,6 +89,21 @@ jobs:
                   cp -rf common_docs_zh/releasenotes 
i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0
                   cp -rf common_docs_zh/gettingStarted 
i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0
                   
+            - name: Set up Python
+              uses: actions/setup-python@v4
+              with:
+                python-version: '3.9'
+            
+            - name: Run Python script with all commit SHAs
+              run: python check_move.py "${{ 
github.event.pull_request.head.sha }}"
+        
+            - name: Check exit code
+              run: |
+                if [ $? -ne 0 ]; then
+                    echo "Have detected not processed link changes, please fix 
them."
+                    exit 1
+                fi
+            
             - name: Use Node.js
               uses: actions/setup-node@v3
               with:
diff --git a/check_move.py b/check_move.py
new file mode 100644
index 00000000000..825a359ae35
--- /dev/null
+++ b/check_move.py
@@ -0,0 +1,126 @@
+# 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 argparse
+import subprocess
+import re
+import os
+import sys
+from typing import AnyStr, List
+from urllib.parse import urlparse
+
+
+move_pairs = []
+deletes = []
+
+change_detected = False
+
+
+def is_same_file(path1, path2):
+    return os.path.normpath(path1) == os.path.normpath(path2)
+
+
+def remove_suffix(text: str, suffix: str):
+    return text.rsplit(suffix, 1)[0]
+
+
+def process_md_file(file_path):
+    link_pattern = re.compile(r"\[.*?\]\((.*?)\)")
+    global change_detected
+
+    with open(file_path, "r", encoding="utf-8") as f:
+        content = f.read()
+
+        links = link_pattern.findall(content)
+
+        for link in links:
+            if not urlparse(link).scheme and not os.path.isabs(link):
+                full_path: str = os.path.normpath(
+                    os.path.join(os.path.dirname(file_path), link)
+                )
+                if not full_path.endswith(".md") and not 
full_path.endswith(".mdx"):
+                    full_path += ".md"
+
+                for [from_path, to_path] in move_pairs:
+                    # In md, the link relative path starts from the directory 
where the document is located, not the document
+                    relative_to_path = os.path.relpath(
+                        to_path, os.path.dirname(file_path)
+                    )
+                    relative_to_path = remove_suffix(relative_to_path, ".md")
+                    relative_to_path = remove_suffix(relative_to_path, ".mdx")
+
+                    if is_same_file(full_path, from_path):
+                        print(
+                            f"{file_path} has a link moved by this commit: 
from {link} to {relative_to_path}"
+                        )
+                        change_detected = True
+
+                for deleted_path in deletes:
+                    if is_same_file(full_path, deleted_path):
+                        print(f"{file_path} has a link removed by this commit: 
{link}")
+                        change_detected = True
+
+
+def extract_file_changes(git_show_output: List[AnyStr]):
+    print(f"commit lines: {len(git_show_output)}")
+    content = b"".join(git_show_output).decode()
+    # print(content)
+
+    move_pattern = r"rename from (.+?)\nrename to (.+?)\n"
+    move_matches = re.findall(move_pattern, content, re.DOTALL | re.MULTILINE)
+    print(f"moved files: {len(move_matches)}")
+
+    delete_pattern = r"diff --git a/(\S+) b/\1\ndeleted file mode \d+\nindex 
.+"
+    delete_matches = re.findall(delete_pattern, content, re.DOTALL | 
re.MULTILINE)
+    print(f"deleted files: {len(delete_matches)}")
+
+    global move_pairs
+    global deletes
+    move_pairs = move_matches
+    deletes = delete_matches
+
+
+def travel(root_path: str):
+    for root, dirs, files in os.walk(root_path):
+        for file in files:
+            if file.endswith(".md") or file.endswith(".mdx"):
+                md_file_path = os.path.join(root, file)
+                process_md_file(md_file_path)
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(description="Add commit id as arguments 
to check")
+    parser.add_argument("commit_id", type=str, help="id of the commit to 
check")
+    args = parser.parse_args()
+
+    # extract all move/delete files
+    p = subprocess.Popen(
+        "git show " + args.commit_id,
+        shell=True,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.STDOUT,
+    )
+    extract_file_changes(p.stdout.readlines())
+
+    # check docs directories
+    travel("docs")
+    travel("i18n")
+    travel("versioned_docs")
+
+    if change_detected:
+        print("Failed!")
+        sys.exit(1)


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to