Watch a branch for pushes¶
Use this when a tool needs to know about every push to a branch, including one
made straight to it with no pull request. A merged pull request and a tag push
each leave their own record. A direct push to main leaves only a push record,
and BranchPushReader reads it.
Which backends implement this
BranchPushReader is defined in forge from v0.38.0. GitLab implements
it from forge-gitlab v0.31.0, GitHub from forge-github v0.30.0 and Gitea
from forge-gitea v0.31.0, where it needs an admin credential. Bitbucket
never will, because its API keeps no record of a push. Check the
capability matrix.
Read the pushes since a time¶
var reader forge.BranchPushReader
if !forge.As(provider, &reader) {
return errCannotWatch
}
pushes, err := reader.ListBranchPushes(ctx, owner, repo, "main", since)
Each push says what it did to the branch (Change), the commits it moved the
branch From and To, who pushed it, and when. A creation has no From. A
deletion has no To, and on Gitea no From either. A merge the forge made is
a push by whoever merged, so a merged pull request shows up here too. Match its
To against the pull requests you already read to tell the two apart.
Pass "" for the branch to read every branch. Name the branch on GitHub,
where a merge queue creates and deletes a branch around every queued merge.
Poll with overlapping windows¶
A forge can record a push after a read has passed its time. GitHub stamps the record with the push time and lists it a few seconds later. GitLab and Gitea stamp it when they write it, so only two records written out of order can be missed. Either way, a window that starts exactly where the last one ended can lose a push.
So start each window earlier than your last read, and drop the pushes you have
already seen by ID:
const overlap = 5 * time.Minute
pushes, err := reader.ListBranchPushes(ctx, owner, repo, "main", lastRead.Add(-overlap))
for _, p := range pushes {
if seen[p.ID] {
continue
}
seen[p.ID] = true
history = append(history, p)
}
Overlap costs only duplicates, so be generous. The measured lag on GitHub was under 20 seconds in every sample, but that is a sample and not a figure GitHub states.
Handle a bulk push¶
GitLab records one push that touches more than three branches without naming
them. A window holding one returns no list and a *forge.BranchBulkPushError,
which wraps forge.ErrIncomplete and carries the whole window:
var bulk *forge.BranchBulkPushError
switch {
case errors.As(err, &bulk):
pushes = bulk.Pushes // every push the forge named, as a normal read
unnamed = bulk.Bulk // the pushes it did not
case err != nil:
return err
}
Treat it as data, not a failure. Every poll whose window holds the bulk push
returns it again, even with a branch named, because the record does not say
which branches it touched. De-duplicate a BranchBulkPush on its ID, as you
do a push.
Find a push the forge did not record¶
On one branch, each push starts where the last one ended. A break in that chain
means a push in between was not seen. Run forge.BranchPushBreaks over the
history you keep:
for _, b := range forge.BranchPushBreaks(history, unnamed) {
// b.After and b.Before are the pushes either side of the gap.
// b.Bulk, when set, is an unnamed push between them that may explain it.
}
A break can be a push recorded late, so check again after your next overlapping read before calling it a loss. A break stays in the history and is returned on every call, so record which ones you have reported, keyed on the two pushes' IDs.
It cannot find everything:
- A dropped push that is the newest on its branch has nothing after it to break the chain, so it is not found until the next push.
- A Gitea deletion has no
From, so the link into it is not judged. - A force push is
BranchUpdatedand links like any other. AskCompareCommitswhetherFromis an ancestor ofToif it matters.