Skip to content

Commit 61de0b5

Browse files
3w36zj6ultimatile
authored andcommitted
chore: add workflow to merge upstream changes
1 parent e1dc841 commit 61de0b5

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# typst-jp/docs repository specific merge strategies
2+
.github/workflows/* merge=ours
3+
.github/ISSUE_TEMPLATE/* merge=ours
4+
README.md merge=ours
5+
CONTRIBUTING.md merge=ours
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
name: Merge upstream changes
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
upstream_ref:
7+
description: "Upstream tag to merge"
8+
required: true
9+
type: string
10+
11+
permissions:
12+
contents: write
13+
pull-requests: write
14+
15+
jobs:
16+
merge-upstream:
17+
runs-on: ubuntu-24.04
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0
21+
22+
- name: Configure Git
23+
run: |
24+
git config --global user.name 'GitHub Actions'
25+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
26+
# Set up 'ours' merge driver
27+
git config --global merge.ours.driver true
28+
29+
- name: Create new branch
30+
run: |
31+
git checkout -b feature/merge-upstream-${{ github.event.inputs.upstream_ref }}
32+
33+
- name: Add upstream remote
34+
run: |
35+
git remote add upstream https://github.com/typst/typst.git
36+
git fetch upstream
37+
38+
- name: Attempt merge with commit markers
39+
run: |
40+
# Try to merge but allow conflicts
41+
TARGET_COMMIT=$(git rev-parse ${{ github.event.inputs.upstream_ref }})
42+
git merge --no-commit --no-ff --allow-unrelated-histories $TARGET_COMMIT || true
43+
44+
# Remove upstream `.github/` directory
45+
git checkout HEAD -- .github/
46+
git reset HEAD .github/
47+
48+
# Commit the merge with conflict markers
49+
git commit -a -m "Merge upstream changes from ${{ github.event.inputs.upstream_ref }}"
50+
51+
- name: Push branch
52+
run: |
53+
git push --force origin feature/merge-upstream-${{ github.event.inputs.upstream_ref }}
54+
55+
- name: Create Pull Request
56+
id: create-pr
57+
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
58+
env:
59+
PR_BODY: |
60+
> [!CAUTION]
61+
> コンフリクトの解消のため、このPull Requestは必ず**Create a merge commit**でマージしてください。
62+
63+
このPull Requestでは、[上流リポジトリ](https://github.com/typst/typst)の[`${{ github.event.inputs.upstream_ref }}`](https://github.com/typst/typst/releases/tag/${{ github.event.inputs.upstream_ref }})の変更をマージします。
64+
65+
with:
66+
script: |
67+
const pr = await github.rest.pulls.create({
68+
owner: context.repo.owner,
69+
repo: context.repo.repo,
70+
head: "feature/merge-upstream-${{ github.event.inputs.upstream_ref }}",
71+
base: "main",
72+
title: "Merge upstream ${{ github.event.inputs.upstream_ref }}",
73+
body: process.env.PR_BODY
74+
});
75+
core.setOutput("pr_number", pr.data.number);
76+
77+
- name: Check for conflict markers
78+
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
79+
with:
80+
script: |
81+
const fs = require('fs');
82+
const path = require('path');
83+
84+
function findConflictMarkers(dirPath, conflictFiles = []) {
85+
const files = fs.readdirSync(dirPath);
86+
87+
for (const file of files) {
88+
const filePath = path.join(dirPath, file);
89+
const stat = fs.statSync(filePath);
90+
91+
if (stat.isDirectory()) {
92+
if (!['.git', 'node_modules', '.github'].includes(file)) {
93+
findConflictMarkers(filePath, conflictFiles);
94+
}
95+
} else if (!['.lock', '.lockb'].some(ext => file.endsWith(ext))) {
96+
try {
97+
const content = fs.readFileSync(filePath, 'utf8');
98+
if (/^(<{7}|={7}|>{7})/m.test(content)) {
99+
conflictFiles.push(filePath);
100+
}
101+
} catch (e) {
102+
// Skip binary files
103+
}
104+
}
105+
}
106+
107+
return conflictFiles;
108+
}
109+
110+
const conflictFiles = findConflictMarkers('.');
111+
112+
if (conflictFiles.length > 0) {
113+
const fileList = conflictFiles.map(f => `- \`${f}\``).join('\n');
114+
await github.rest.issues.createComment({
115+
owner: context.repo.owner,
116+
repo: context.repo.repo,
117+
issue_number: ${{ steps.create-pr.outputs.pr_number }},
118+
body: `> [!WARNING]\n> コンフリクトマーカーが検出されました。以下のファイルを確認し、コンフリクトを解消してください。\n\n${fileList}`
119+
});
120+
}

0 commit comments

Comments
 (0)