Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ tasks:
- go run ./cmd/refs
- go run ./cmd/mutate -stale-exempts
- go run ./cmd/sfw check
- go run ./cmd/taskgate -allow .github/taskgate-allow.txt -task {{.TASK_EXE}}
# -common: the common tasks as every repository is held to them, the include being the
# tree's file here.
- go run ./cmd/taskgate -common -common-include internal/tasks/tasks.yml -allow .github/taskgate-allow.txt -task {{.TASK_EXE}}
# A commit since main whose author is a test's: what a test's git wrote into the checkout's
# config. On main, the range is empty.
- go run ./cmd/testidentity -base origin/main
Expand Down
35 changes: 32 additions & 3 deletions cmd/taskgate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
// CI does is written once, in the Taskfile, a developer runs the same thing, and what decides
// anything is Go with a test:
//
// go tool taskgate [-allow file] [-task path] [-command cmd]... [-max-lines n] [-max-script-lines n] [dir]
// go tool taskgate [-common] [-allow file] [-task path] [-command cmd]... [-max-lines n] [-max-script-lines n] [dir]
//
// -common also holds the repository to go-tools' common tasks (internal/tasks), as every
// repository has them: included once, none of their names the repository's own, how it is
// released stated and true of its workflows, and lint:workflows in what CI runs. Without the flag
// nothing of that is asked.
//
// -command is a command of the repository's own that a step may be instead of a task, as it is
// spelled at the start of the step - `go run ./cmd/spin-os`, or `spin-os` where a step put it on
Expand All @@ -12,7 +17,8 @@
// -allow names what is allowed on purpose, one `<key> # why` a line: a step as
// `<file>/<job>/<step>` (a composite action's without the job), a Taskfile's block as
// `<file>:<task>`, a script by its path, a task a step names that the Taskfile has not as
// `<file>/<job>/<step>/<task>`. A line that matches nothing fails too.
// `<file>/<job>/<step>/<task>`, and what -common finds as the `common:...` it prints after the
// finding. A line that matches nothing fails too.
//
// -task is the Task binary that resolves the names a step runs: the one the Taskfile runs under,
// {{.TASK_EXE}}, so the version that answers is the one a workflow runs.
Expand All @@ -32,6 +38,8 @@ func main() {
task := flag.String("task", "task", "the Task binary that lists the Taskfile's tasks")
maxLines := flag.Int("max-lines", 12, "lines of shell a task's own cmd or sh may hold; 0 is any")
maxScript := flag.Int("max-script-lines", 40, "lines of shell a script a task runs may hold; 0 is any")
common := flag.Bool("common", false, "hold the repository to go-tools' common tasks too")
included := flag.String("common-include", taskgate.CommonInclude, "the `taskfile` an include names to have the common tasks (go-tools' own is its tree's)")
var commands []string
flag.Func("command", "a command of the repository's own a step may be instead of a task (repeatable)", func(c string) error {
commands = append(commands, c)
Expand All @@ -47,8 +55,21 @@ func main() {
fmt.Fprintf(os.Stderr, "taskgate: %v\n", err)
os.Exit(2)
}
var uncommon []taskgate.Finding
said := ""
if *common {
// Before Task is asked anything: a second task of a common name is a Taskfile Task does
// not load, and what is wanted then is this gate's finding, which names the file.
if uncommon, said, err = taskgate.Common(root, *included); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
}
known, err := taskgate.Tasks(*task, root)
if err != nil {
for _, f := range uncommon {
fmt.Printf("%s (%s)\n", f, f.Key())
}
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
Expand All @@ -57,8 +78,13 @@ func main() {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
unexplained, stale := allow.Split(allowed, findings, taskgate.Finding.Key)
unexplained, stale := allow.Split(allowed, append(findings, uncommon...), taskgate.Finding.Key)
for _, f := range unexplained {
if _, ok := f.(taskgate.Uncommon); ok {
// Its key is not one a person can put together from the finding, as a step's is.
fmt.Printf("%s (%s)\n", f, f.Key())
continue
}
fmt.Println(f)
}
for _, k := range stale {
Expand All @@ -69,4 +95,7 @@ func main() {
os.Exit(1)
}
fmt.Printf("OK: every workflow step runs a task the Taskfile has or one of the repository's commands, and every task is commands (%d exempt).\n", len(allowed))
if said != "" {
fmt.Println("OK: " + said + ".")
}
}
58 changes: 39 additions & 19 deletions cmd/versions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ Usage:
versions bump <name> [version] pin an entry at a version, the newest by default, and
every entry tracked with it at the same; never at an
older version or commit than the pin's, unless -back,
and not an entry whose bump: names another command
and not an entry whose bump: names another command,
unless -by says this is that command running it
versions refresh <name>... bump each to its newest, a commit each, and say what moved
and what is a new major, left for a person;
-propose pushes them as refresh/<label>-<sum> and opens
the pull request (GH_TOKEN or GITHUB_TOKEN), once per set,
closing the open one it supersedes (-label, -base)
closing the open one it supersedes (-label, -base);
-by as for bump

Every command takes -file, versions.yaml by default. Nothing is built by a bump: each entry's
note says what it is checked with afterwards.
Expand All @@ -107,6 +109,7 @@ func run(ctx context.Context, argv []string) error {
base := fs.String("base", "main", "refresh: the branch the pull request is into")
byTask := fs.Bool("task", false, "check: name the common task that moves an entry, as a repository runs it")
back := fs.Bool("back", false, "bump: move the pin to an older version or commit too")
by := fs.String("by", "", "bump, refresh: the `command` that runs this one, as the entries it moves name it in bump:")
if err := fs.Parse(argv); err != nil {
if errors.Is(err, flag.ErrHelp) {
fmt.Print(usage)
Expand Down Expand Up @@ -146,9 +149,9 @@ func run(ctx context.Context, argv []string) error {
}
return check(os.Stdout, upstream.Check(ctx, v), *byTask)
case "bump":
return bump(ctx, v, *file, names, *back)
return bump(ctx, v, *file, names, *back, *by)
case "refresh":
r := refresher{file: *file, git: runGit, label: *label, base: *base, out: os.Stdout, now: time.Now()}
r := refresher{file: *file, by: *by, git: runGit, label: *label, base: *base, out: os.Stdout, now: time.Now()}
return refresh(ctx, r, *propose, names)
default:
fmt.Fprint(os.Stderr, usage)
Expand Down Expand Up @@ -218,8 +221,9 @@ func newer(w io.Writer, v *versions.Versions, names []string) error {
}

// bump pins an entry at a version, the newest by default, and writes the file. back is a person
// saying the version is older than the pin and meant: without it that is refused.
func bump(ctx context.Context, v *versions.Versions, file string, names []string, back bool) error {
// saying the version is older than the pin and meant: without it that is refused. by is the
// command that runs this one, when one does (anothers).
func bump(ctx context.Context, v *versions.Versions, file string, names []string, back bool, by string) error {
if len(names) < 1 || len(names) > 2 {
return errors.New("bump: <name> [version]")
}
Expand All @@ -231,7 +235,7 @@ func bump(ctx context.Context, v *versions.Versions, file string, names []string
if err != nil {
return err
}
if err := anothers(was); err != nil {
if err := anothers(was, by); err != nil {
return err
}
to := upstream.Bump
Expand Down Expand Up @@ -260,12 +264,23 @@ func bump(ctx context.Context, v *versions.Versions, file string, names []string

// anothers refuses an entry whose file says another command moves it, and names the command:
// what that command checks, waits for or writes besides the pin is not something this one does.
func anothers(e versions.Entry) error {
if e.Bump == "" {
//
// by is who runs this command, as -by says it. The command an entry names is often built on this
// one - spin-machine's `spin-machine-dev bump` runs `go tool versions bump`, and its scheduled
// refresh `versions refresh` - and would be refused by the field that names it. So it says who it
// is, in the words the entry uses, and an entry that names those words is moved. A person or the
// common task says nothing and is refused; one that types another command's name has read the
// refusal and overruled it, which the field is not there to stop.
func anothers(e versions.Entry, by string) error {
if e.Bump == "" || e.Bump == by {
return nil
}
return fmt.Errorf("versions: %s is moved by `%s`, as its entry says (bump:), and not by this command: "+
"that one knows what a bump of %s needs", e.Name, e.Bump, e.Name)
as := ""
if by != "" {
as = fmt.Sprintf(", which -by says is `%s`", by)
}
return fmt.Errorf("versions: %s is moved by `%s`, as its entry says (bump:), and not by this command%s: "+
"that one knows what a bump of %s needs", e.Name, e.Bump, as, e.Name)
}

// pins is each KEY=value the entries named are handed over as, after prefix, sorted: the same
Expand All @@ -289,20 +304,23 @@ func pins(v *versions.Versions, names []string, prefix string) ([]string, error)
// two are counted apart and fail apart: a row not read - a private upstream asked without a token
// answers 404 - may be behind, so a count of what is behind says "of those read" beside it.
// The last line names the command that moves an entry as the person who ran this would type it:
// the task every repository has when byTask says that is what ran it, and this command otherwise.
// the task every repository has when byTask says that is what ran it, and this command otherwise -
// and neither when every row behind names a command of its own, which is the only one that moves it.
func check(w io.Writer, rows []upstream.Status, byTask bool) error {
// mutate-exempt: of the three zeros, a minimum width of one is under every cell's, a tab's width is unused where the padding is spaces, and the flag that edit sets reads HTML entities, which no row has
tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintln(tw, "NAME\tPINNED\tNEWEST\t")
behind, unreadRows, anothers := 0, 0, false
behind, unreadRows, named := 0, 0, 0
for _, st := range rows {
mark := ""
switch {
case st.Unread:
mark, unreadRows = "not read:", unreadRows+1
case st.Behind:
mark, behind = "behind", behind+1
anothers = anothers || st.Entry.Bump != ""
if st.Entry.Bump != "" {
named++
}
}
note := st.Note
if st.Entry.Bump != "" {
Expand All @@ -318,15 +336,17 @@ func check(w io.Writer, rows []upstream.Status, byTask bool) error {
return unread{fmt.Errorf("%d not read, and %d behind of the %d that were: a row not read may be behind, and says why "+
"(GITHUB_TOKEN or GH_TOKEN reads a private repository, and more than sixty requests an hour)", unreadRows, behind, len(rows)-unreadRows)}
case behind > 0:
also := ""
if anothers {
also = ", and the command a row names moves that one"
}
bump := "versions bump <name>"
if byTask {
bump = "go tool task bump NAME=<name>"
}
return fmt.Errorf("%d behind; %s moves one%s", behind, bump, also)
switch named {
case 0:
return fmt.Errorf("%d behind; %s moves one", behind, bump)
case behind:
return fmt.Errorf("%d behind; the command its row names moves each, and %s moves none of them", behind, bump)
}
return fmt.Errorf("%d behind; %s moves one, and the command a row names moves that one", behind, bump)
}
return nil
}
38 changes: 37 additions & 1 deletion cmd/versions/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ func TestCheckCountsARowItCouldNotReadAndFailsApartForIt(t *testing.T) {
"one behind, through the task": {file: lead + behind, argv: []string{"-task"},
says: "1 behind; go tool task bump NAME=<name> moves one", status: 1},
"one behind that another command moves": {file: lead + behind + " bump: spin-os upgrade\n", rows: []string{"behind v1 v2 behind moved by `spin-os upgrade`"},
says: "1 behind; versions bump <name> moves one, and the command a row names moves that one", status: 1},
says: "1 behind; the command its row names moves each, and versions bump <name> moves none of them", status: 1},
"one behind that another command moves, through the task": {file: lead + behind + " bump: spin-os upgrade\n", argv: []string{"-task"},
says: "1 behind; the command its row names moves each, and go tool task bump NAME=<name> moves none of them", status: 1},
"two behind, one that another command moves": {file: lead + behind + " bump: spin-os upgrade\n" + strings.Replace(behind, "name: behind", "name: plain", 1), argv: []string{"-task"},
says: "2 behind; go tool task bump NAME=<name> moves one, and the command a row names moves that one", status: 1},
"one that another command moves, not behind": {file: lead + " bump: spin-os upgrade\n", rows: []string{"lead v2 v2 moved by `spin-os upgrade`"}}, //nolint:dupword // a row: its pin, then its newest
"one behind, and another that another command moves": {file: lead + " bump: spin-os upgrade\n" + behind,
says: "1 behind; versions bump <name> moves one", status: 1},
Expand Down Expand Up @@ -327,6 +331,38 @@ func TestABumpOfAnEntryAnotherCommandMovesIsRefused(t *testing.T) {
}
}

// The command an entry names is not refused by its own entry when it is built on this one: it says
// who it is with -by, in the entry's words, and the entry moves. Said of another command, or of an
// entry that names none, -by changes nothing: the one refuses and says both names, the other moves.
func TestTheCommandAnEntryNamesMovesItThroughThisOne(t *testing.T) {
const theirs = "- name: qemu\n kind: date\n version: 20260101T000000Z\n track: today\n bump: spin-machine-dev bump\n" +
"- name: plain\n kind: date\n version: 20260101T000000Z\n track: today\n"
file := filepath.Join(t.TempDir(), "versions.yaml")
require.NoError(t, os.WriteFile(file, []byte(theirs), 0o600))

err := run(t.Context(), []string{"bump", "-file", file, "-by", "spin-os upgrade", "qemu", "20270101T000000Z"})
require.ErrorContains(t, err, "qemu is moved by `spin-machine-dev bump`, as its entry says (bump:), and not by this command, which -by says is `spin-os upgrade`: that one knows")
raw, err := os.ReadFile(file) //nolint:gosec // the test's own file
require.NoError(t, err)
assert.Equal(t, theirs, string(raw))

out := stdout(t, func() {
err = run(t.Context(), []string{"bump", "-file", file, "-by", "spin-machine-dev bump", "qemu", "20270101T000000Z"})
})
require.NoError(t, err)
assert.Equal(t, "qemu: 20260101T000000Z -> 20270101T000000Z \n", out)
out = stdout(t, func() {
err = run(t.Context(), []string{"bump", "-file", file, "-by", "spin-machine-dev bump", "plain", "20270101T000000Z"})
})
require.NoError(t, err)
assert.Equal(t, "plain: 20260101T000000Z -> 20270101T000000Z \n", out)
v, err := versions.Load(file)
require.NoError(t, err)
qemu, err := v.Get("qemu")
require.NoError(t, err)
assert.Equal(t, "spin-machine-dev bump", qemu.Bump, "the bump kept the field")
}

// A failure is said once under the command's name, whoever worded it, and a help that was asked
// for is no failure. The table's columns are two spaces apart.
func TestAFailureIsSaidUnderTheCommandsName(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion cmd/versions/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
// wrote for itself, once.
type refresher struct {
file string
// by is the command that runs this refresh, as -by says it: an entry that names it is moved.
by string
// git runs git in the repository and answers its output, trimmed.
git func(args ...string) (string, error)
// gh is GitHub's API, for propose; nil otherwise.
Expand Down Expand Up @@ -106,7 +108,7 @@ func (r refresher) move(ctx context.Context, name string) (m []moved, major bool
}
if named, err := v.Get(name); err != nil {
return nil, false, err
} else if err := anothers(named); err != nil {
} else if err := anothers(named, r.by); err != nil {
return nil, false, err
}
e, out, err := upstream.Bump(ctx, v, name, "")
Expand Down
10 changes: 10 additions & 0 deletions cmd/versions/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,13 @@ func TestARefreshLeavesAnEntryAnotherCommandMoves(t *testing.T) {
assert.Equal(t, "2", git(t, "rev-list", "--count", "HEAD"), "one commit, for plain")
assert.NotContains(t, out.String(), "| snapshot |")
}

// Run by the command the entry names, which says so (-by), the refresh moves it: a scheduled
// refresh a repository's own command starts is not stopped by the field that names that command.
func TestARefreshByTheCommandAnEntryNamesMovesIt(t *testing.T) {
inRepository(t, strings.Replace(refreshFile(), " note: build everything again\n", " bump: spin-machine-dev refresh\n", 1))
var out bytes.Buffer
r := refresher{file: "versions.yaml", by: "spin-machine-dev refresh", git: runGit, label: "versions", base: "main", out: &out}
require.NoError(t, r.refresh(t.Context(), []string{"snapshot"}))
assert.Contains(t, out.String(), "| snapshot |")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/sigstore/sigstore-go v1.4.0
github.com/stretchr/testify v1.12.1
go.yaml.in/yaml/v3 v3.0.6
golang.org/x/mod v0.42.0
golang.org/x/tools v0.52.0
golang.org/x/vuln v1.9.0
google.golang.org/protobuf v1.36.12
Expand Down Expand Up @@ -84,7 +85,6 @@ require (
go.opentelemetry.io/otel/trace v1.47.0 // indirect
go.uber.org/mock v0.6.0 // indirect
golang.org/x/crypto v0.58.0 // indirect
golang.org/x/mod v0.42.0 // indirect
golang.org/x/net v0.61.0 // indirect
golang.org/x/sync v0.24.0 // indirect
golang.org/x/sys v0.49.0 // indirect
Expand Down
Loading