|
| 1 | +name: syncweaver-contribute-patch |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + source_path: |
| 7 | + description: Optional tracked source path override; if omitted, source_path is resolved from lockfile. |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + repo_url: |
| 11 | + description: Optional source repository identifier (URL or OWNER/REPO) used to resolve source_path from lockfile. |
| 12 | + required: false |
| 13 | + type: string |
| 14 | + source_repository: |
| 15 | + description: Optional source repository in OWNER/REPO format; if omitted, derives from lockfile repo_url. |
| 16 | + required: false |
| 17 | + type: string |
| 18 | + lockfile: |
| 19 | + description: Path to the syncweaver lockfile. |
| 20 | + required: false |
| 21 | + default: .syncweaver-lock.json |
| 22 | + type: string |
| 23 | + patch_path: |
| 24 | + description: Optional patch path override; if omitted, uses source entry patch value from lockfile. |
| 25 | + required: false |
| 26 | + type: string |
| 27 | + source_base_ref: |
| 28 | + description: Optional source repository base branch for PR; if omitted, uses source entry ref from lockfile. |
| 29 | + required: false |
| 30 | + type: string |
| 31 | + syncweaver-version: |
| 32 | + description: syncweaver version/ref to install (e.g. version tag, branch name, or commit SHA). Will use docker image if available. |
| 33 | + required: false |
| 34 | + type: string |
| 35 | + default: main |
| 36 | + |
| 37 | +permissions: |
| 38 | + contents: read |
| 39 | + |
| 40 | +jobs: |
| 41 | + resolve-patch-metadata: |
| 42 | + runs-on: ubuntu-latest |
| 43 | + outputs: |
| 44 | + source_path: ${{ steps.resolve.outputs.source_path }} |
| 45 | + repo_url: ${{ steps.resolve.outputs.repo_url }} |
| 46 | + source_repository: ${{ steps.resolve.outputs.source_repository }} |
| 47 | + patch_path: ${{ steps.resolve.outputs.patch_path }} |
| 48 | + source_base_ref: ${{ steps.resolve.outputs.source_base_ref }} |
| 49 | + lockfile: ${{ env.LOCKFILE }} |
| 50 | + env: |
| 51 | + SOURCE_PATH: ${{ github.event.inputs.source_path || '' }} |
| 52 | + REPO_URL: ${{ github.event.inputs.repo_url || '' }} |
| 53 | + SOURCE_REPOSITORY: ${{ github.event.inputs.source_repository || '' }} |
| 54 | + LOCKFILE: ${{ github.event.inputs.lockfile || '.syncweaver-lock.json' }} |
| 55 | + PATCH_PATH: ${{ github.event.inputs.patch_path || '' }} |
| 56 | + SOURCE_BASE_REF: ${{ github.event.inputs.source_base_ref || '' }} |
| 57 | + SYNCWEAVER_VERSION: ${{ github.event.inputs['syncweaver-version'] || github.event.client_payload['syncweaver-version'] || 'main' }} |
| 58 | + steps: |
| 59 | + - name: Checkout host repository |
| 60 | + uses: actions/checkout@v6 |
| 61 | + |
| 62 | + - name: Setup syncweaver |
| 63 | + uses: CCBR/syncweaver/actions/setup-syncweaver@main |
| 64 | + with: |
| 65 | + syncweaver-version: ${{ env.SYNCWEAVER_VERSION }} |
| 66 | + |
| 67 | + - name: Resolve source and patch metadata |
| 68 | + id: resolve |
| 69 | + shell: bash |
| 70 | + env: |
| 71 | + SOURCE_PATH_INPUT: ${{ env.SOURCE_PATH }} |
| 72 | + REPO_URL_INPUT: ${{ env.REPO_URL }} |
| 73 | + SOURCE_REPOSITORY_INPUT: ${{ env.SOURCE_REPOSITORY }} |
| 74 | + LOCKFILE_PATH: ${{ env.LOCKFILE }} |
| 75 | + PATCH_PATH_INPUT: ${{ env.PATCH_PATH }} |
| 76 | + SOURCE_BASE_REF_INPUT: ${{ env.SOURCE_BASE_REF }} |
| 77 | + SYNCWEAVER_HOST_CWD: ${{ github.workspace }} |
| 78 | + run: | |
| 79 | + set -euo pipefail |
| 80 | + syncweaver-python -m syncweaver.contribute_patch |
| 81 | +
|
| 82 | + - name: Print resolved metadata |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + set -euo pipefail |
| 86 | + echo "source_path=${{ steps.resolve.outputs.source_path }}" |
| 87 | + echo "repo_url=${{ steps.resolve.outputs.repo_url }}" |
| 88 | + echo "source_repository=${{ steps.resolve.outputs.source_repository }}" |
| 89 | + echo "patch_path=${{ steps.resolve.outputs.patch_path }}" |
| 90 | + echo "source_base_ref=${{ steps.resolve.outputs.source_base_ref }}" |
| 91 | +
|
| 92 | + contribute-patch: |
| 93 | + runs-on: ubuntu-latest |
| 94 | + needs: resolve-patch-metadata |
| 95 | + permissions: |
| 96 | + contents: read |
| 97 | + env: |
| 98 | + SOURCE_PATH: ${{ needs.resolve-patch-metadata.outputs.source_path }} |
| 99 | + REPO_URL: ${{ needs.resolve-patch-metadata.outputs.repo_url }} |
| 100 | + SOURCE_REPOSITORY: ${{ needs.resolve-patch-metadata.outputs.source_repository }} |
| 101 | + PATCH_PATH: ${{ needs.resolve-patch-metadata.outputs.patch_path }} |
| 102 | + SOURCE_BASE_REF: ${{ needs.resolve-patch-metadata.outputs.source_base_ref }} |
| 103 | + LOCKFILE: ${{ needs.resolve-patch-metadata.outputs.lockfile }} |
| 104 | + steps: |
| 105 | + - name: Generate CCBR-bot token |
| 106 | + id: ccbr_bot |
| 107 | + uses: actions/create-github-app-token@v1 |
| 108 | + with: |
| 109 | + app-id: ${{ secrets.CCBR_BOT_APP_ID }} |
| 110 | + private-key: ${{ secrets.CCBR_BOT_PRIVATE_KEY }} |
| 111 | + |
| 112 | + - name: Checkout host repository |
| 113 | + uses: actions/checkout@v6 |
| 114 | + with: |
| 115 | + path: host |
| 116 | + |
| 117 | + - name: Checkout source repository |
| 118 | + uses: actions/checkout@v6 |
| 119 | + with: |
| 120 | + repository: ${{ env.SOURCE_REPOSITORY }} |
| 121 | + ref: ${{ env.SOURCE_BASE_REF }} |
| 122 | + token: ${{ steps.ccbr_bot.outputs.token }} |
| 123 | + path: source |
| 124 | + |
| 125 | + - name: Apply patch to source repository |
| 126 | + id: apply_patch |
| 127 | + shell: bash |
| 128 | + run: | |
| 129 | + set -euo pipefail |
| 130 | +
|
| 131 | + patch_file="$GITHUB_WORKSPACE/host/${PATCH_PATH}" |
| 132 | + if [[ ! -f "$patch_file" ]]; then |
| 133 | + echo "Error: patch file not found in host repository: ${PATCH_PATH}" >&2 |
| 134 | + exit 1 |
| 135 | + fi |
| 136 | +
|
| 137 | + branch_stub=$(printf '%s' "$SOURCE_PATH" | tr '/ ' '--' | tr -cd '[:alnum:]._-') |
| 138 | + if [[ -z "$branch_stub" ]]; then |
| 139 | + branch_stub="patch" |
| 140 | + fi |
| 141 | + branch_name="syncweaver/contribute-patch/${branch_stub}-${GITHUB_RUN_ID}" |
| 142 | +
|
| 143 | + cd "$GITHUB_WORKSPACE/source" |
| 144 | + git config user.name "github-actions[bot]" |
| 145 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 146 | + git switch -c "$branch_name" |
| 147 | +
|
| 148 | + if ! git apply --3way --whitespace=nowarn "$patch_file"; then |
| 149 | + echo "Error: patch failed to apply cleanly to ${SOURCE_REPOSITORY}@${SOURCE_BASE_REF}" >&2 |
| 150 | + exit 1 |
| 151 | + fi |
| 152 | +
|
| 153 | + if git diff --quiet; then |
| 154 | + echo "has_changes=false" >> "$GITHUB_OUTPUT" |
| 155 | + else |
| 156 | + echo "has_changes=true" >> "$GITHUB_OUTPUT" |
| 157 | + fi |
| 158 | + echo "branch_name=$branch_name" >> "$GITHUB_OUTPUT" |
| 159 | +
|
| 160 | + - name: Open pull request in source repository |
| 161 | + if: steps.apply_patch.outputs.has_changes == 'true' |
| 162 | + uses: peter-evans/create-pull-request@v7 |
| 163 | + with: |
| 164 | + token: ${{ steps.ccbr_bot.outputs.token }} |
| 165 | + path: source |
| 166 | + base: ${{ env.SOURCE_BASE_REF }} |
| 167 | + branch: ${{ steps.apply_patch.outputs.branch_name }} |
| 168 | + delete-branch: true |
| 169 | + commit-message: "chore(syncweaver): apply host patch for ${{ env.SOURCE_PATH }}" |
| 170 | + title: "chore(syncweaver): apply host patch for ${{ env.SOURCE_PATH }}" |
| 171 | + body: | |
| 172 | + Automated patch contribution from host repository workflow dispatch. |
| 173 | +
|
| 174 | + Inputs used: |
| 175 | + - source_path: ${{ env.SOURCE_PATH }} |
| 176 | + - repo_url: ${{ env.REPO_URL }} |
| 177 | + - source_repository: ${{ env.SOURCE_REPOSITORY }} |
| 178 | + - source_base_ref: ${{ env.SOURCE_BASE_REF }} |
| 179 | + - patch_path: ${{ env.PATCH_PATH }} |
| 180 | + - lockfile: ${{ env.LOCKFILE }} |
| 181 | +
|
| 182 | + Trigger context: |
| 183 | + - host_repository: ${{ github.repository }} |
| 184 | + - workflow_run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 185 | +
|
| 186 | + - name: Print no-op summary |
| 187 | + if: steps.apply_patch.outputs.has_changes == 'false' |
| 188 | + shell: bash |
| 189 | + run: | |
| 190 | + set -euo pipefail |
| 191 | + echo "Patch applied cleanly but introduced no source changes. No PR created." |
0 commit comments