Skip to content

Commit e2a06ef

Browse files
committed
Mirror master and release branches.
Having postgresql-cfbot/postgresql mirror the master branch keeps ccache recent. Since master is the default branch, other branches are allowed to use its ccache.
1 parent 2060106 commit e2a06ef

4 files changed

Lines changed: 73 additions & 11 deletions

File tree

cfbot_api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ def github_webhook():
5353
cfbot_github.handle_workflow_run_webhook(conn, event)
5454
conn.commit()
5555
return "OK"
56+
elif event_type == "push":
57+
cfbot_github.handle_push_webhook(conn, event)
58+
conn.commit()
59+
return "OK"
5660
else:
5761
return "unhandled event type " + event_type
5862
except:

cfbot_github.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,27 @@ def handle_workflow_job_webhook(conn, event):
800800
ingest_task(conn, build_id, task_id, task_status, task_name, "webhook")
801801

802802

803+
def handle_push(conn, event):
804+
repo = event["repository"]["full_name"]
805+
if repo != cfbot_config.GITHUB_MIRROR_FULL_REPO:
806+
# We only mirror branches from postgres/postgres (which is itself a
807+
# mirror of the actual self-hosted postgresql.org repo).
808+
return
809+
ref = event["ref"]
810+
if not ref.startswith("refs/head/"):
811+
# We don't mirror refs/tag/... should we?
812+
return
813+
branch = ref[10:]
814+
if not re.match(cfbot_config.GITHUB_MIRROR_BRANCH_PATTERN, branch):
815+
# Only mirror branches that match our configured pattern (though
816+
# we don't actually expect postgres/postgres to have any non-matching
817+
# branches).
818+
return
819+
cursor = conn.cursor()
820+
# Actual mirroring work is handed off to a cfbot worker.
821+
cfbot_work_queue.insert_work_queue(cursor, "push-mirror-branch", branch)
822+
823+
803824
# ======================================================================
804825
# Functions called by cfbot workers servicing work_queue items.
805826
# ======================================================================

cfbot_patch.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,20 @@ def choose_submission(conn, cf_ids):
165165
return commitfest_id, submission_id
166166

167167

168-
def update_patchbase_tree(repo_dir):
169-
"""Pull changes from PostgreSQL master and return the HEAD commit ID."""
168+
def checkout_patchbase_branch(repo_dir, branch):
169+
"""Switch patchbase repo to a given branch."""
170170
subprocess.call(
171-
"cd %s && git checkout . -q > /dev/null && git clean -fd > /dev/null && git checkout -q master && git pull -q"
172-
% repo_dir,
171+
"cd %s && git checkout . -q > /dev/null && git clean -fd > /dev/null && git checkout -q %s"
172+
% (repo_dir, branch),
173+
shell=True,
174+
)
175+
176+
177+
def update_patchbase_tree(repo_dir, branch):
178+
"""Switch branch and pull changes from tracked remote."""
179+
checkout_patchbase_branch(repo_dir, branch)
180+
subprocess.call(
181+
"cd %s && git pull -q" % (repo_dir, branch),
173182
shell=True,
174183
)
175184

@@ -310,6 +319,29 @@ def update_submission(conn, message_id, commit_id, commitfest_id, submission_id)
310319
)
311320

312321

322+
def mirror_branch(branch):
323+
template_repo_path = patchburner_ctl("template-repo-path").strip()
324+
325+
# Everything else in this file assumes that
326+
# cfbot_periodic_minutely.py acquired the "big lock" while
327+
# applying patches etc, but this function is reached by cfbot
328+
# workers, and needs interlocking so it does that explicitly here.
329+
with cfbot_util.lock():
330+
logging.info("updating branch %s in template repo", branch)
331+
update_patchbase_tree(template_repo_path, branch)
332+
if cfbot_config.GIT_REMOTE_NAME:
333+
logging.info("pushing branch %s (automatic mirror)", branch)
334+
my_env = os.environ.copy()
335+
my_env["GIT_SSH_COMMAND"] = cfbot_config.GIT_SSH_COMMAND
336+
subprocess.check_call(
337+
"cd %s && git push -q -f %s %s"
338+
% (template_repo_path, cfbot_config.GIT_REMOTE_NAME, branch),
339+
env=my_env,
340+
shell=True,
341+
stderr=subprocess.DEVNULL,
342+
)
343+
344+
313345
def reset_repo_to_good_master_commit(conn, repo_path):
314346
# find the most recent commit ID that succeeded on master
315347
cursor = conn.cursor()
@@ -324,16 +356,18 @@ def reset_repo_to_good_master_commit(conn, repo_path):
324356
(good_commit_id,) = result
325357
else:
326358
good_commit_id = "origin/master"
359+
logging.info("selected master commit %s as base", good_commit_id)
327360

361+
checkout_patchbase_branch(repo_path, "master")
328362
commit_id = get_commit_id(repo_path)
329-
if commit_id != good_commit_id:
330-
logging.info("updating repo to find last good master commit %s", good_commit_id)
331-
update_patchbase_tree(repo_path)
332363

333-
reset_commit_id(repo_path, good_commit_id)
334-
commit_id = get_commit_id(repo_path)
335-
if good_commit_id != "origin/master":
336-
assert commit_id == good_commit_id
364+
if commit_id != good_commit_id:
365+
logging.info("updating template repo")
366+
update_patchbase_tree(repo_path, "master")
367+
reset_commit_id(repo_path, good_commit_id)
368+
commit_id = get_commit_id(repo_path)
369+
if good_commit_id != "origin/master":
370+
assert commit_id == good_commit_id
337371

338372
return commit_id
339373

cfbot_work_queue.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ def process_one_job(conn, fetch_only):
138138
cfbot_github.poll_github_commit(conn, key)
139139
elif type == "poll-github-run":
140140
cfbot_github.poll_github_run(conn, key)
141+
# Mirroring master, REL_*_STABLE
142+
elif type == "push-mirror-branch":
143+
cfbot_patch.mirror_branch(key)
141144
# Notifying the Commitfest app
142145
elif type == "post-task-status":
143146
cfbot_commitfest.post_task_status(conn, key)

0 commit comments

Comments
 (0)