Skip to content

Commit 61e5048

Browse files
authored
ci: configure git user for committing latest_translation_commit.json … (#21409)
1 parent cbb6f79 commit 61e5048

2 files changed

Lines changed: 125 additions & 2 deletions

File tree

.github/workflows/translation-zh-switch.yaml

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,55 @@ jobs:
3838
git push origin "${{ github.event.inputs.to }}"
3939
cd ..
4040
41+
- name: Force merge to_base branch excluding tidb-cloud folder
42+
run: |
43+
cd docs
44+
git config user.name github-actions
45+
git config user.email github-actions@github.com
46+
47+
# Fetch the to_base branch
48+
git fetch origin "${{ github.event.inputs.to_base }}"
49+
50+
# Simple and reliable approach: merge everything first, then restore tidb-cloud
51+
52+
# Store the current target branch state before merge
53+
TARGET_BRANCH_HEAD=$(git rev-parse HEAD)
54+
55+
# Create a temporary branch from to_base
56+
git checkout -b temp-merge-branch "origin/${{ github.event.inputs.to_base }}"
57+
58+
# Switch back to target branch
59+
git checkout "${{ github.event.inputs.to }}"
60+
61+
# Merge the temporary branch with allow-unrelated-histories
62+
git merge temp-merge-branch --no-edit --allow-unrelated-histories -X theirs
63+
64+
# After merge, restore tidb-cloud folder from the original target branch state
65+
# Use the stored commit hash to restore the original tidb-cloud folder
66+
git checkout $TARGET_BRANCH_HEAD -- tidb-cloud/ || true
67+
68+
# If there are any conflicts in tidb-cloud, resolve them by keeping the target branch version
69+
git add tidb-cloud/ || true
70+
71+
# Clean up temporary branch
72+
git branch -D temp-merge-branch
73+
74+
# Get the latest filterCloudInitFiles.js from master branch
75+
git fetch origin master
76+
git checkout origin/master -- scripts/filterCloudInitFiles.js || true
77+
78+
# Push the changes
79+
git push origin "${{ github.event.inputs.to }}"
80+
cd ..
81+
4182
- name: Update latest_translation_commit.json
4283
run: |
4384
cd docs
44-
echo '{"target":"${{ github.event.inputs.to_base }}","sha":""}' > latest_translation_commit.json
85+
# Get the latest commit SHA from to_base branch
86+
TO_BASE_SHA=$(git rev-parse origin/${{ github.event.inputs.to_base }})
87+
echo '{"target":"${{ github.event.inputs.to_base }}","sha":"'$TO_BASE_SHA'"}' > latest_translation_commit.json
88+
git config user.name github-actions
89+
git config user.email github-actions@github.com
4590
git add latest_translation_commit.json
4691
git commit -m "ci: update latest_translation_commit.json for ${{ github.event.inputs.to_base }}"
4792
git push origin "${{ github.event.inputs.to }}"
@@ -60,7 +105,7 @@ jobs:
60105
export GH_TOKEN=${{github.token}}
61106
cd docs
62107
npm i
63-
node scripts/filterUpdateFiles.js --cloud --init
108+
node scripts/filterCloudInitFiles.js
64109
tree tmp
65110
cd ..
66111

scripts/filterCloudInitFiles.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import * as fs from "fs";
2+
import path from "path";
3+
4+
// Read the TOC file
5+
const tocContent = fs.readFileSync("TOC-tidb-cloud.md", "utf8");
6+
7+
// Regular expression to match markdown links
8+
const linkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
9+
10+
// Set to store unique file paths
11+
const filePaths = new Set();
12+
13+
// Extract all file paths from markdown links
14+
let match;
15+
while ((match = linkRegex.exec(tocContent)) !== null) {
16+
const filePath = match[2];
17+
18+
// Skip external links (starting with http/https)
19+
if (filePath.startsWith("http")) {
20+
continue;
21+
}
22+
23+
// Skip anchor links (starting with #)
24+
if (filePath.startsWith("#")) {
25+
continue;
26+
}
27+
28+
// Remove leading slash if present
29+
const cleanPath = filePath.startsWith("/") ? filePath.slice(1) : filePath;
30+
31+
// Skip files in tidb-cloud folder
32+
if (cleanPath.startsWith("tidb-cloud/")) {
33+
continue;
34+
}
35+
36+
filePaths.add(cleanPath);
37+
}
38+
39+
// Create tmp directory if it doesn't exist
40+
const tmpDir = "tmp";
41+
if (!fs.existsSync(tmpDir)) {
42+
fs.mkdirSync(tmpDir, { recursive: true });
43+
}
44+
45+
// Copy files to tmp directory
46+
let copiedCount = 0;
47+
let skippedCount = 0;
48+
49+
for (const filePath of filePaths) {
50+
const sourcePath = filePath;
51+
const targetPath = path.join(tmpDir, filePath);
52+
53+
// Create target directory if it doesn't exist
54+
const targetDir = path.dirname(targetPath);
55+
if (!fs.existsSync(targetDir)) {
56+
fs.mkdirSync(targetDir, { recursive: true });
57+
}
58+
59+
// Check if source file exists
60+
if (fs.existsSync(sourcePath)) {
61+
try {
62+
fs.copyFileSync(sourcePath, targetPath);
63+
console.log(`✓ Copied: ${filePath}`);
64+
copiedCount++;
65+
} catch (error) {
66+
console.error(`✗ Error copying ${filePath}: ${error.message}`);
67+
}
68+
} else {
69+
console.log(`⚠ Skipped (not found): ${filePath}`);
70+
skippedCount++;
71+
}
72+
}
73+
74+
console.log(`\nSummary:`);
75+
console.log(`- Total files referenced: ${filePaths.size}`);
76+
console.log(`- Files copied: ${copiedCount}`);
77+
console.log(`- Files skipped: ${skippedCount}`);
78+
console.log(`- Files copied to: ${tmpDir}/`);

0 commit comments

Comments
 (0)