Skip to content

Commit e4ed2ca

Browse files
authored
Merge pull request #58 from ByteInternet/detect_renamed_files
2 parents 3557b10 + 9fb18cb commit e4ed2ca

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Detect renamed docs
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
detect_renamed_docs:
8+
runs-on: ubuntu-latest
9+
name: Detect renamed docs
10+
steps:
11+
- uses: actions/checkout@v3
12+
with:
13+
fetch-depth: 0
14+
- name: Get changed files
15+
id: changed_files
16+
run: |
17+
result="$(python3 ci/bin/detect_rewrites.py ${{ github.event.pull_request.base.sha }} ${{github.event.pull_request.head.sha}})"
18+
echo "$result"
19+
echo "CHANGED_FILES<<EOF" >> $GITHUB_OUTPUT
20+
echo "$result" >> $GITHUB_OUTPUT
21+
echo "EOF" >> $GITHUB_OUTPUT
22+
shell: bash
23+
- name: Comment hostname on PR
24+
if: ${{ steps.changed_files.outputs.CHANGED_FILES }}
25+
uses: thollander/actions-comment-pull-request@v1
26+
with:
27+
message: "${{ steps.changed_files.outputs.CHANGED_FILES }}"

ci/bin/detect_rewrites.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import argparse
2+
import re
3+
import subprocess
4+
from typing import Tuple
5+
6+
RENAME_PATTERN = re.compile(r"^R[0-9]+")
7+
8+
9+
def parse_args() -> Tuple[str, str]:
10+
parser = argparse.ArgumentParser()
11+
parser.add_argument("start_commit")
12+
parser.add_argument("end_commit")
13+
14+
args = parser.parse_args()
15+
16+
return args.start_commit, args.end_commit
17+
18+
19+
def main():
20+
start_commit, end_commit = parse_args()
21+
diff_command = [
22+
"git",
23+
"diff",
24+
"--name-status",
25+
"-M",
26+
"--stat",
27+
start_commit,
28+
end_commit,
29+
]
30+
output = subprocess.check_output(diff_command, encoding="utf-8")
31+
renamed_files = filter(RENAME_PATTERN.match, output.splitlines())
32+
renamed_files = map(lambda x: x.split("\t")[1:], renamed_files)
33+
renamed_markdown_files = filter(lambda x: x[0].endswith(".md"), renamed_files)
34+
renamed_markdown_files = list(renamed_markdown_files)
35+
36+
if renamed_markdown_files:
37+
print("Detected renamed/moved files:")
38+
print("```")
39+
for file_from, file_to in renamed_markdown_files:
40+
print("{} => {}".format(file_from, file_to))
41+
print("```")
42+
print("To prevent 404 pages, consider adding the redirects to the new pages:")
43+
print("```markdown")
44+
print("---")
45+
print("redirect_from:")
46+
for file_from, _ in renamed_markdown_files:
47+
print(" - {}".format(file_from))
48+
print("---")
49+
print("```")
50+
51+
52+
if __name__ == "__main__":
53+
main()

0 commit comments

Comments
 (0)