Skip to content

ci: add scheduled upstream-sync workflow with policy-based conflict resolution - #14

Draft
sjoerd2025 wants to merge 1 commit into
mainfrom
posthog/add-upstream-sync
Draft

sjoerd2025 wants to merge 1 commit into
mainfrom
posthog/add-upstream-sync

Conversation

@sjoerd2025

@sjoerd2025 sjoerd2025 commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Why: keep this fork automatically in sync with its upstream, without a manual setup step — clean upstream merges land directly on the default branch, conflicts are auto-resolved by policy (docs/CI take upstream, fork customizations are preserved via KEEP_OURS), and anything the policy can't resolve is opened as a PR containing the conflict markers.

Schedule and policy are editable at the top of the file. Trigger manually anytime via Run workflow.


Created with PostHog Desktop


Summary by cubic

Adds a scheduled workflow that keeps this fork in sync with its upstream automatically.

Clean upstream merges push directly to the default branch. Conflicts are resolved by policy: docs and CI boilerplate take the upstream version, while fork customizations can be preserved via a KEEP_OURS list. Any conflict the policy can't resolve is pushed to a sync-upstream/conflict-* branch and opened as a PR containing the conflict markers.

The sync runs daily and can be triggered manually anytime via Run workflow. The schedule and conflict policy are configurable via environment variables at the top of .github/workflows/sync-upstream.yml.

Written for commit e0dcd0b. Summary will update on new commits.

View guided diff Turn on auto-fix

Summary by Sourcery

Automate daily upstream synchronization with configurable conflict handling and PR-based escalation for unresolved changes.

New Features:

  • Add a scheduled and manually triggerable workflow that synchronizes the fork with its upstream repository.
  • Automatically push clean or policy-resolved upstream merges, while opening review PRs when direct updates are unavailable or conflicts require manual resolution.

Enhancements:

  • Provide configurable path-based conflict policies for preserving fork customizations or accepting upstream changes.
  • Detect non-fork repositories and no-op when the upstream has no new commits.

CI:

  • Add GitHub Actions automation for daily upstream synchronization with concurrency control and write permissions.

…esolution

Daily scheduled (and manually dispatchable) sync of this fork with its upstream: clean merges push straight to the default branch; conflicts are resolved by KEEP_OURS/KEEP_THEIRS glob policy; anything unresolved lands as a PR with conflict markers.

Generated-By: PostHog Desktop
Task-Id: c432a00f-64b2-454e-8222-a8a49ea4efdc
@changeset-bot

changeset-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: e0dcd0b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Oct 31, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sourcery-ai

sourcery-ai Bot commented Oct 31, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds a daily or manually triggered upstream-sync workflow that discovers the parent repository, merges its default branch, applies configurable path-based conflict policies, and either pushes the result directly or opens a PR for protected-branch or unresolved-conflict cases.

Sequence diagram for scheduled upstream synchronization

sequenceDiagram
    actor Trigger as Scheduler_or_User
    participant Workflow
    participant GitHubAPI
    participant Upstream
    participant Origin

    Trigger->>Workflow: Start scheduled or manual workflow
    Workflow->>GitHubAPI: gh api repository parent and default_branch
    GitHubAPI-->>Workflow: Parent repository and upstream branch
    Workflow->>Upstream: git fetch upstream branch
    Workflow->>Workflow: git merge upstream branch
    alt Already up to date
        Workflow-->>Trigger: Exit without changes
    else Clean merge
        Workflow->>Origin: git push default branch
        alt Direct push rejected
            Workflow->>Origin: git push sync-upstream/pr branch
            Workflow->>GitHubAPI: gh pr create
        end
    else Merge has conflicts
        loop Each conflicted path
            Workflow->>Workflow: Match KEEP_OURS or KEEP_THEIRS policy
            alt Policy matches
                Workflow->>Workflow: git checkout ours or theirs and git add
            else No policy match
                Workflow->>Workflow: git add with conflict markers
            end
        end
        Workflow->>Workflow: git commit
        alt Unresolved conflicts remain
            Workflow->>Origin: git push sync-upstream/conflict branch
            Workflow->>GitHubAPI: gh pr create with conflict details
        else Policy resolved all conflicts
            Workflow->>Origin: git push default branch
        end
    end
Loading

File-Level Changes

Change Details Files
Adds a scheduled and manually triggerable GitHub Actions workflow that discovers the fork’s upstream repository and fetches its default branch.
  • Runs daily with repository-level concurrency control and write permissions.
  • Uses full checkout history and GitHub API metadata to identify the parent repository and upstream branch.
  • Skips cleanly when the repository is not a fork or upstream metadata is unavailable.
.github/workflows/sync-upstream.yml
Implements automated upstream merging with configurable path-based conflict resolution.
  • Merges upstream changes with a generated bot commit.
  • Keeps fork files or takes upstream files according to configurable KEEP_OURS and KEEP_THEIRS glob lists.
  • Defaults documentation, CI, and repository metadata paths to the upstream version while leaving custom paths unresolved.
.github/workflows/sync-upstream.yml
Provides fallback delivery paths for protected branches and unresolved conflicts.
  • Pushes clean or fully policy-resolved merges directly to the current branch when permitted.
  • Opens a review PR if direct pushing is rejected by branch protection.
  • Pushes unresolved conflict markers to a timestamped branch and opens a PR listing the affected files.
.github/workflows/sync-upstream.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gitar-bot

gitar-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

Important

You are using the Gitar free plan. Upgrade to unlock code review, CI analysis, auto-apply, custom automations, and more.

Gitar

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 issues found across 1 file

Confidence score: 2/5

  • In .github/workflows/sync-upstream.yml, interpolating ref values into shell source can let a crafted ref execute commands on the runner. Pass the values through step env variables and use quoted shell expansions.
  • A manual run on a non-default ref can push to that ref or target its PR there instead of using the default branch. Resolve and check out the fork’s default branch, or reject runs from other refs.
  • The unquoted loop expansion makes the default .github/** policy miss nested workflow files. Parse the policy strings into arrays before matching.
  • Unresolved conflicts can create a new timestamped branch and PR every day. Check for an existing open PR before creating another.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name=".github/workflows/sync-upstream.yml">

<violation number="1" location=".github/workflows/sync-upstream.yml:77">
P1: `ubranch` and `github.ref_name` are interpolated into shell source before Bash parses it, so a valid ref containing `$(...)` can execute on the runner. Pass ref values through step `env` variables and use quoted shell variables.</violation>

<violation number="2" location=".github/workflows/sync-upstream.yml:78">
P2: A manual dispatch on a non-default ref makes `BRANCH` that ref, so the sync pushes there or opens its PR against it instead of the default branch. Resolve and check out the fork's default branch, or reject runs on other refs.</violation>

<violation number="3" location=".github/workflows/sync-upstream.yml:128">
P2: The unquoted loop expansion turns matching policy globs into workspace paths before `[[ ... ]]` sees them, so the default `.github/**` policy misses nested workflow files. Parse both policy strings into arrays (or disable pathname expansion) before iterating.</violation>

<violation number="4" location=".github/workflows/sync-upstream.yml:147">
P2: Each daily run creates a new timestamped `sync-upstream/conflict-*` branch and a new PR for the same unresolved files, so unresolved conflicts produce a PR every day. Before creating the branch, check for an open PR from a stable branch name with `gh pr list --head` and reuse it, or use one stable branch name.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

run: |
set -euo pipefail
PARENT="${{ steps.upstream.outputs.parent }}"
UBRANCH="${{ steps.upstream.outputs.ubranch }}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: ubranch and github.ref_name are interpolated into shell source before Bash parses it, so a valid ref containing $(...) can execute on the runner. Pass ref values through step env variables and use quoted shell variables.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At .github/workflows/sync-upstream.yml, line 77:

<comment>`ubranch` and `github.ref_name` are interpolated into shell source before Bash parses it, so a valid ref containing `$(...)` can execute on the runner. Pass ref values through step `env` variables and use quoted shell variables.</comment>

<file context>
@@ -0,0 +1,162 @@
+        run: |
+          set -euo pipefail
+          PARENT="${{ steps.upstream.outputs.parent }}"
+          UBRANCH="${{ steps.upstream.outputs.ubranch }}"
+          BRANCH="${{ github.ref_name }}"
+          MSG="chore: sync with upstream $PARENT ($UBRANCH)"
</file context>


if [ "${#unresolved[@]}" -gt 0 ]; then
printf 'Unresolvable conflicts (opened a PR): %s\n' "${unresolved[*]}" >> "$GITHUB_STEP_SUMMARY"
cb="sync-upstream/conflict-$(date +%Y%m%d-%H%M%S)"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Each daily run creates a new timestamped sync-upstream/conflict-* branch and a new PR for the same unresolved files, so unresolved conflicts produce a PR every day. Before creating the branch, check for an open PR from a stable branch name with gh pr list --head and reuse it, or use one stable branch name.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At .github/workflows/sync-upstream.yml, line 147:

<comment>Each daily run creates a new timestamped `sync-upstream/conflict-*` branch and a new PR for the same unresolved files, so unresolved conflicts produce a PR every day. Before creating the branch, check for an open PR from a stable branch name with `gh pr list --head` and reuse it, or use one stable branch name.</comment>

<file context>
@@ -0,0 +1,162 @@
+
+          if [ "${#unresolved[@]}" -gt 0 ]; then
+            printf 'Unresolvable conflicts (opened a PR): %s\n' "${unresolved[*]}" >> "$GITHUB_STEP_SUMMARY"
+            cb="sync-upstream/conflict-$(date +%Y%m%d-%H%M%S)"
+            git push origin "HEAD:refs/heads/$cb"
+            {
</file context>

set -euo pipefail
PARENT="${{ steps.upstream.outputs.parent }}"
UBRANCH="${{ steps.upstream.outputs.ubranch }}"
BRANCH="${{ github.ref_name }}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: A manual dispatch on a non-default ref makes BRANCH that ref, so the sync pushes there or opens its PR against it instead of the default branch. Resolve and check out the fork's default branch, or reject runs on other refs.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At .github/workflows/sync-upstream.yml, line 78:

<comment>A manual dispatch on a non-default ref makes `BRANCH` that ref, so the sync pushes there or opens its PR against it instead of the default branch. Resolve and check out the fork's default branch, or reject runs on other refs.</comment>

<file context>
@@ -0,0 +1,162 @@
+          set -euo pipefail
+          PARENT="${{ steps.upstream.outputs.parent }}"
+          UBRANCH="${{ steps.upstream.outputs.ubranch }}"
+          BRANCH="${{ github.ref_name }}"
+          MSG="chore: sync with upstream $PARENT ($UBRANCH)"
+
</file context>

[[ "$f" == $pat ]] && resolved="ours" && break
done
if [ -z "$resolved" ]; then
for pat in $KEEP_THEIRS; do

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The unquoted loop expansion turns matching policy globs into workspace paths before [[ ... ]] sees them, so the default .github/** policy misses nested workflow files. Parse both policy strings into arrays (or disable pathname expansion) before iterating.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At .github/workflows/sync-upstream.yml, line 128:

<comment>The unquoted loop expansion turns matching policy globs into workspace paths before `[[ ... ]]` sees them, so the default `.github/**` policy misses nested workflow files. Parse both policy strings into arrays (or disable pathname expansion) before iterating.</comment>

<file context>
@@ -0,0 +1,162 @@
+              [[ "$f" == $pat ]] && resolved="ours" && break
+            done
+            if [ -z "$resolved" ]; then
+              for pat in $KEEP_THEIRS; do
+                # shellcheck disable=SC2053
+                [[ "$f" == $pat ]] && resolved="theirs" && break
</file context>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant