Skip to content

Git designed for AI agents.

graft: policy check exits 1 when the candidate would deny updates

Federico Elia8f0df17eba6a
diff --git a/cmd/graft/policy.go b/cmd/graft/policy.go
index ed1d051..6e1c1fa 100644
--- a/cmd/graft/policy.go
+++ b/cmd/graft/policy.go
@@ -41,7 +41,11 @@ FILE before a server reloads it (SIGUSR1). It changes nothing.
What it cannot show: an update that the policy in force refused is not in the
changeset log. The check lists what the candidate would newly deny among what
happened, not what it would newly allow. A batched land whose list of members
-the log cut short is checked for the members it lists.`
+the log cut short is checked for the members it lists.
+
+Exit status: 0 when the policy is valid and denies none of the updates
+checked, 1 when it would deny some (they are printed) or a repository cannot
+be read, 2 when the policy is invalid.`
// defaultPolicyCheckLimit is the number of changesets of each repository that
// graft policy check reads by default.
@@ -172,9 +176,22 @@ func policyCheck(ctx context.Context, out io.Writer, o *options, f *policyFlags,
if o.jsonOut {
enc := json.NewEncoder(out)
enc.SetIndent("", " ")
- return enc.Encode(res)
+ if err := enc.Encode(res); err != nil {
+ return err
+ }
+ } else {
+ printPolicyCheck(out, res)
+ }
+
+ // A candidate that would deny updates fails the check, so that a script can stop before the
+ // reload: the updates are printed above.
+ denied := 0
+ for _, repo := range res.Repositories {
+ denied += len(repo.Denied)
+ }
+ if denied > 0 {
+ return fmt.Errorf("the candidate policy would deny %d of the updates checked", denied)
}
- printPolicyCheck(out, res)
return nil
}
diff --git a/cmd/graft/policy_test.go b/cmd/graft/policy_test.go
index 6c4249a..1683073 100644
--- a/cmd/graft/policy_test.go
+++ b/cmd/graft/policy_test.go
@@ -144,7 +144,8 @@ func TestPolicyCheckAgainstChangesets(t *testing.T) {
t.Run("the updates the candidate would deny", func(t *testing.T) {
code, stdout, stderr := runPolicyCheck(append([]string{candidate, "-repo", env.relPath, "--json"}, server...)...)
- require.Equal(t, exitOK, code, stderr)
+ require.Equal(t, exitError, code, "a candidate that would deny updates fails the check")
+ require.Equal(t, "graft policy: the candidate policy would deny 1 of the updates checked\n", stderr)
var res policyCheckResult
require.NoError(t, json.Unmarshal([]byte(stdout), &res))
require.Len(t, res.Repositories, 1)
@@ -161,14 +162,20 @@ func TestPolicyCheckAgainstChangesets(t *testing.T) {
t.Run("the text output", func(t *testing.T) {
code, stdout, stderr := runPolicyCheck(append([]string{candidate, "-repo", env.relPath}, server...)...)
- require.Equal(t, exitOK, code, stderr)
+ require.Equal(t, exitError, code, stderr)
require.Contains(t, stdout, "1 updates the candidate would deny")
require.Contains(t, stdout, " alice refs/heads/main (2 changesets, last ")
})
t.Run("--limit reads the last changesets", func(t *testing.T) {
code, stdout, stderr := runPolicyCheck(append([]string{candidate, "-repo", env.relPath, "--limit", "1", "--json"}, server...)...)
- require.Equal(t, exitOK, code, stderr)
+ // The check fails exactly when the last changeset is one of alice's updates of main.
+ lastChangeset := all[len(all)-1]
+ wantCode := exitOK
+ if lastChangeset.Reference == "refs/heads/main" && lastChangeset.Actor == "alice" {
+ wantCode = exitError
+ }
+ require.Equal(t, wantCode, code, stderr)
var res policyCheckResult
require.NoError(t, json.Unmarshal([]byte(stdout), &res))
repo := res.Repositories[0]
diff --git a/doc/graft/CONFIGURATION.md b/doc/graft/CONFIGURATION.md
index d1ae521..efad5fc 100644
--- a/doc/graft/CONFIGURATION.md
+++ b/doc/graft/CONFIGURATION.md
@@ -140,7 +140,7 @@ DEBUG.
Before a reload, `graft policy check FILE` validates the file's policy as the
reload will (exit 2 when the policy is invalid) and prints its hash; with
`-repo REPO` it lists the recent updates of the repository that the new policy
-would deny. It checks the policy only: the reload also validates the rest of
+would deny, and exits 1 when there are any. It checks the policy only: the reload also validates the rest of
the file, against the server's own paths and settings, and rejects a file that
fails there.