|
| 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 | + id: check-conflicts |
| 79 | + run: | |
| 80 | + CONFLICT_FILES=$(git grep -l "^<<<<<<<\|^=======\|^>>>>>>>" HEAD || echo "") |
| 81 | + if [ -n "$CONFLICT_FILES" ]; then |
| 82 | + echo "has_conflicts=true" >> $GITHUB_OUTPUT |
| 83 | + echo "conflict_files<<EOF" >> $GITHUB_OUTPUT |
| 84 | + echo "$CONFLICT_FILES" >> $GITHUB_OUTPUT |
| 85 | + echo "EOF" >> $GITHUB_OUTPUT |
| 86 | + fi |
| 87 | +
|
| 88 | + - name: Comment on Pull Request if conflicts found |
| 89 | + if: steps.check-conflicts.outputs.has_conflicts == 'true' |
| 90 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0 |
| 91 | + with: |
| 92 | + script: | |
| 93 | + const files = `${{ steps.check-conflicts.outputs.conflict_files }}`.split('\n').filter(f => f); |
| 94 | + const fileList = files.map(f => `- \`${f}\``).join('\n'); |
| 95 | + await github.rest.issues.createComment({ |
| 96 | + owner: context.repo.owner, |
| 97 | + repo: context.repo.repo, |
| 98 | + issue_number: ${{ steps.create-pr.outputs.pr_number }}, |
| 99 | + body: `> [!WARNING]\n> コンフリクトマーカーが検出されました。以下のファイルを確認し、コンフリクトを解消してください。\n\n${fileList}` |
| 100 | + }); |
0 commit comments