Skip to content

Commit 8424e4a

Browse files
fix(backport-batch): trust cozystack-ci[bot] when no bot is named
With no BACKPORT_BATCH_BOT, a maintainer's run by hand took the batch pull request the scheduled run had opened as the App's bot for a look-alike. It warned, started a batch of its own from the line, and opened a second batch pull request for the line on a branch with that day's date; on the day the scheduled batch opened, its push of the same branch name was not a fast-forward instead. Unset, the variable now means cozystack-ci[bot], the bot the workflow runs as; the workflow still names it from the token, and set empty still trusts no bot. Assisted-By: LLM Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent c1b9248 commit 8424e4a

6 files changed

Lines changed: 110 additions & 12 deletions

File tree

‎cmd/backport-batch/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ A pick is known by the line git appends: the last `(cherry picked from commit
9090

9191
An original is on the open batch pull request when its commits picked there show it the same way. The same rules on the newer line decide rule 6.
9292

93-
Only the line's own commits count, the ones after the point where it branched from `main`, never a pull request against it: an open backport pull request, its title and its body are anyone's to write. The tool trusts the line's history because only maintainers merge into `release-*`; if that ever changes, a commit that names an original without carrying all of it would hide the rest. The batch pull request counts only when it comes from a `backport-batch/<line>-<yyyymmdd>` branch of this repository, not a fork, and was opened by the bot the scheduled batch runs as, the exact login `BACKPORT_BATCH_BOT` names (`cozystack-ci[bot]` in the workflow), or by someone whose permission on the repository GitHub reports as write or admin, and only at the head GitHub reports for it. No other bot counts, GitHub Actions' own included, since anything holding a workflow's token writes as it; a hand run that continues the scheduled batch sets `BACKPORT_BATCH_BOT` too, or it takes the bot's pull request for a look-alike. The notify comment is trusted the same way. The author association is not enough: an organization member or an invited collaborator can hold read access only. With more than one such pull request open the tool refuses to go on.
93+
Only the line's own commits count, the ones after the point where it branched from `main`, never a pull request against it: an open backport pull request, its title and its body are anyone's to write. The tool trusts the line's history because only maintainers merge into `release-*`; if that ever changes, a commit that names an original without carrying all of it would hide the rest. The batch pull request counts only when it comes from a `backport-batch/<line>-<yyyymmdd>` branch of this repository, not a fork, and was opened by the bot the scheduled batch runs as, the exact login `BACKPORT_BATCH_BOT` names, `cozystack-ci[bot]` when it is unset and no bot when it is set empty, or by someone whose permission on the repository GitHub reports as write or admin, and only at the head GitHub reports for it. No other bot counts, GitHub Actions' own included, since anything holding a workflow's token writes as it. So a maintainer's run by hand continues the scheduled batch with nothing set, rather than taking its pull request for a look-alike and opening a second one. The notify comment is trusted the same way. The author association is not enough: an organization member or an invited collaborator can hold read access only. With more than one such pull request open the tool refuses to go on.
9494

9595
Titles and bodies are read from the merge commits on `main`, which hold them as a maintainer merged them; the live pull request stays editable by its author.
9696

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2026 The Cozystack Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"slices"
21+
"strings"
22+
"testing"
23+
)
24+
25+
// A maintainer's run by hand continues the batch pull request the scheduled
26+
// run opened as its bot, with nothing set: it edits that pull request rather
27+
// than opening a second one for the line.
28+
func TestHandRunContinuesTheBotsBatch(t *testing.T) {
29+
for _, tc := range []struct {
30+
name, author string
31+
continued bool
32+
}{
33+
{"the scheduled run's bot", "cozystack-ci[bot]", true},
34+
{"another bot", "renovate[bot]", false},
35+
} {
36+
t.Run(tc.name, func(t *testing.T) {
37+
f := newFixture(t)
38+
f.lines()
39+
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
40+
gh := &fakeGH{bot: batchBotLogin(func(string) (string, bool) { return "", false })}
41+
openBatch(t, f, gh)
42+
gh.prs[0].User.Login, gh.prs[0].User.Type = tc.author, "Bot"
43+
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
44+
f.now = f.now.AddDate(0, 0, 3)
45+
46+
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push")
47+
if code != 0 {
48+
t.Fatalf("run --continue = %d\n%s", code, stderr)
49+
}
50+
continued := slices.Equal(gh.edited, []string{"#90 fix(backport): batch for release-1.1, 2026-10-26"}) && len(gh.created) == 1
51+
if continued != tc.continued {
52+
t.Errorf("edited %q, created %q; want the batch of %s continued=%v\n%s", gh.edited, gh.created, tc.author, tc.continued, stderr)
53+
}
54+
if !tc.continued && !strings.Contains(stderr, "ignoring #90") {
55+
t.Errorf("the run does not say it ignores #90:\n%s", stderr)
56+
}
57+
})
58+
}
59+
}
60+
61+
// The bot comes from BACKPORT_BATCH_BOT when it is set, empty included, and
62+
// is the scheduled run's own bot when it is not.
63+
func TestBatchBotLogin(t *testing.T) {
64+
env := func(v string, ok bool) func(string) (string, bool) {
65+
return func(key string) (string, bool) {
66+
if key != batchBotEnv {
67+
t.Errorf("looked up %s", key)
68+
}
69+
return v, ok
70+
}
71+
}
72+
for _, tc := range []struct {
73+
name, v string
74+
ok bool
75+
want string
76+
}{
77+
{"unset", "", false, "cozystack-ci[bot]"},
78+
{"set empty", "", true, ""},
79+
{"set", "other-app[bot]", true, "other-app[bot]"},
80+
} {
81+
if got := batchBotLogin(env(tc.v, tc.ok)); got != tc.want {
82+
t.Errorf("%s: batchBotLogin = %q, want %q", tc.name, got, tc.want)
83+
}
84+
}
85+
}

‎cmd/backport-batch/github.go‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,21 @@ var (
4848

4949
// batchBotEnv names the GitHub App bot the scheduled batch opens its pull
5050
// requests and comments as, <app slug>[bot]. The workflow sets it from the
51-
// token it mints; a hand run that continues the scheduled batch sets it too.
52-
const batchBotEnv = "BACKPORT_BATCH_BOT"
51+
// token it mints. Unset, it is defaultBatchBot, so that a maintainer's run by
52+
// hand continues the scheduled batch instead of opening a second one; set and
53+
// empty, no bot is trusted.
54+
const (
55+
batchBotEnv = "BACKPORT_BATCH_BOT"
56+
defaultBatchBot = "cozystack-ci[bot]"
57+
)
58+
59+
// batchBotLogin is the bot to trust given lookup, os.LookupEnv outside tests.
60+
func batchBotLogin(lookup func(string) (string, bool)) string {
61+
if v, ok := lookup(batchBotEnv); ok {
62+
return v
63+
}
64+
return defaultBatchBot
65+
}
5366

5467
// pullRequest is the REST view of a pull request: unlike gh pr list, it
5568
// carries the author's association with the repository.

‎cmd/backport-batch/main.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ policy block from that page on origin/main, or on --policy REF. Every command
7777
but notify, which reads only GitHub, fetches origin first.
7878
7979
BACKPORT_BATCH_BOT names the bot the scheduled batch writes as,
80-
<app>[bot]: a batch pull request or a notify comment it wrote is trusted like
81-
a maintainer's. Set it to continue the scheduled batch by hand.
80+
<app>[bot], cozystack-ci[bot] when unset: a batch pull request or a notify
81+
comment it wrote is trusted like a maintainer's. Set and empty, no bot is.
8282
`
8383

8484
const (
@@ -97,7 +97,7 @@ type app struct {
9797
}
9898

9999
func main() {
100-
a := &app{gh: ghCLI{bot: os.Getenv(batchBotEnv)}, stdout: os.Stdout, stderr: os.Stderr, now: time.Now}
100+
a := &app{gh: ghCLI{bot: batchBotLogin(os.LookupEnv)}, stdout: os.Stdout, stderr: os.Stderr, now: time.Now}
101101
os.Exit(a.main(os.Args[1:]))
102102
}
103103

‎docs/release/backports.md‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,11 @@ The batch runs for each of its lines twice a week, on Monday and Thursday: often
7373

7474
```bash
7575
go build ./cmd/backport-batch
76-
export BACKPORT_BATCH_BOT='cozystack-ci[bot]'
7776
./backport-batch run --line release-1.7 --continue --push
7877
./backport-batch run --line release-1.6 --continue --push
7978
```
8079

81-
`BACKPORT_BATCH_BOT` names the bot the scheduled run writes as, so that a run by hand continues its open batch pull request rather than taking it for a look-alike.
80+
A run by hand trusts the open batch pull request the scheduled run opened as `cozystack-ci[bot]` and continues it; `BACKPORT_BATCH_BOT` names another bot, and set empty trusts none.
8281

8382
`./backport-batch lines` prints the lines, newest first. `run`, `check`, `fold` and `lines` read the policy block from this page on `main`, so until the page with its block is on `main` they need `--policy <ref>` naming a ref that has it, and the scheduled run, which passes none, fails.
8483

@@ -95,7 +94,7 @@ The scheduled run's token may not push a commit that changes `.github/workflows/
9594
A maintainer brings it over with their own token, one that may push workflow changes (the Workflows permission, or the classic `workflow` scope), by continuing the open batch without the flag:
9695

9796
```bash
98-
BACKPORT_BATCH_BOT='cozystack-ci[bot]' ./backport-batch run --line <line> --continue --push
97+
./backport-batch run --line <line> --continue --push
9998
```
10099

101100
That picks it onto the batch in the batch's shape, after what the batch carries, and the body stops listing it; later scheduled runs continue the batch on top. To cut without it instead, take it out with `./backport-batch run --line <line> --defer <number> --reason <why> --push`.
@@ -104,7 +103,7 @@ That picks it onto the batch in the batch's shape, after what the batch carries,
104103

105104
[`backport-batch.yaml`](../../.github/workflows/backport-batch.yaml) runs [`hack/backport-batch-scheduled.sh`](../../hack/backport-batch-scheduled.sh) on Monday and Thursday. For each line the batch runs for, newest first, it continues the open batch pull request or opens one, runs `check`, and then `backport-batch notify`, which reports what waits on the batch. A batch open for three days or more, so one the previous run already saw, gets a comment on its pull request saying so and a line in the run's summary. The comment is one per pull request, updated in place on every run. Deferred conflicts go to whoever `BACKPORT_AGENT` names. `hack/backport-batch-scheduled.sh --dry-run` does the same in the current worktree and changes nothing on GitHub. Its `check` sees the line without the batch the dry run just built, so it can exit 1 where the real run's check passes.
106105

107-
The batch runs as the cozystack-ci GitHub App, which the release workflows already use, and needs no secret of its own. The job mints a token from `COZYSTACK_CI_APP_ID` and `COZYSTACK_CI_PRIVATE_KEY` for this repository alone, with Contents and Pull requests write and nothing more, and fails when either secret is missing. Its pushes start CI on the batch pull request, which a push with the job's own `GITHUB_TOKEN` would not. Its commits carry the App's bot, `cozystack-ci[bot]`, and `BACKPORT_BATCH_BOT`, which the job sets from the token, is how `backport-batch` knows to trust that bot's batch pull request and comment. The App's installation has to grant this repository Contents and Pull requests read and write, which an organization admin sees among the organization's installed GitHub Apps. It has no Workflows permission, so a change to `.github/workflows/` waits for a maintainer, as [changes to workflows](#changes-to-workflows) describes.
106+
The batch runs as the cozystack-ci GitHub App, which the release workflows already use, and needs no secret of its own. The job mints a token from `COZYSTACK_CI_APP_ID` and `COZYSTACK_CI_PRIVATE_KEY` for this repository alone, with Contents and Pull requests write and nothing more, and fails when either secret is missing. Its pushes start CI on the batch pull request, which a push with the job's own `GITHUB_TOKEN` would not. Its commits carry the App's bot, `cozystack-ci[bot]`, whose batch pull request and comment `backport-batch` trusts: `BACKPORT_BATCH_BOT`, which the job sets from the token, names it, and a run without the variable trusts `cozystack-ci[bot]` too. The App's installation has to grant this repository Contents and Pull requests read and write, which an organization admin sees among the organization's installed GitHub Apps. It has no Workflows permission, so a change to `.github/workflows/` waits for a maintainer, as [changes to workflows](#changes-to-workflows) describes.
108107

109108
It also reads these repository settings, which a repository admin sets up:
110109

@@ -114,4 +113,4 @@ It also reads these repository settings, which a repository admin sets up:
114113

115114
Optionally, once the `Backport batch shape` check runs from `main`, a ruleset makes it required on the release lines. It targets `release-*` and excludes `release-*.*.*`: `release-*` alone also matches the `release-X.Y.Z-rc.N` and `release-X.Y.Z` staging branches that [`tags.yaml`](../../.github/workflows/tags.yaml) and [`promote-rc.yaml`](../../.github/workflows/promote-rc.yaml) push, which have no batch on them, and a required check would refuse those pushes. "Do not require status checks on creation" stays on, since [`cut-prerelease.yaml`](../../.github/workflows/cut-prerelease.yaml) creates `release-X.Y` with a push at its first rc.
116115

117-
To run the scheduled script for real by hand, export your own token as `BACKPORT_BATCH_TOKEN`, since without `--dry-run` it uses that variable and no other credential, and `BACKPORT_BATCH_BOT='cozystack-ci[bot]'` so that it continues the scheduled batch. With a token that may push workflow changes, leave `--no-workflow-files` out and the run brings over what waits.
116+
To run the scheduled script for real by hand, export your own token as `BACKPORT_BATCH_TOKEN`: without `--dry-run` it uses that variable and no other credential. With a token that may push workflow changes, leave `--no-workflow-files` out and the run brings over what waits.

‎hack/backport-batch-scheduled.sh‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
# BACKPORT_BATCH the backport-batch binary (default: built from this tree)
2626
# BACKPORT_BATCH_TOKEN the token the batch pushes, opens and comments with
2727
# BACKPORT_BATCH_BOT the bot the scheduled batch writes as, <app>[bot],
28-
# whose batch pull request and comment are trusted
28+
# whose batch pull request and comment are trusted;
29+
# cozystack-ci[bot] when unset
2930
# BACKPORT_AGENT ci or manual (default manual): who takes a deferred
3031
# conflict, the CI agent or a maintainer
3132
# BACKPORT_AGENT_TOKEN with ci, the token the CI agent's tasks start with

0 commit comments

Comments
 (0)