diff --git a/tools/rjc/README.md b/tools/rjc/README.md index b14a2467d..38804396e 100644 --- a/tools/rjc/README.md +++ b/tools/rjc/README.md @@ -4,7 +4,7 @@ Opens, updates, and lands GitHub pull requests from a jj repository. It creates ## Profiles are required -rjc has no built-in repository list and no default values. Each run reads a profile file that the caller supplies: +rjc has no built-in repository list. Each run reads a profile file that the caller supplies: ```bash rjc --profiles submit --title "feat(scope): subject" @@ -13,18 +13,20 @@ RJC_PROFILES= rjc status `--profiles` wins over `RJC_PROFILES`. When neither is set, rjc exits 2 with `no profile file: pass --profiles or set RJC_PROFILES`. -rjc reads the `origin` remote, finds the repository in the profile, and uses that entry. A repository whose owner is not in `owners`, or that has no entry under `repos`, is refused before any write: `repository / is not listed in the profile file`. rjc never guesses an issue prefix or a spec directory. +rjc uses the `repos` entry matching the `origin` remote, or the optional `default` profile when no repository entry matches. The repository owner must appear in `owners`. Without a default, an unlisted repository is refused before any write: `repository / is not listed in the profile file`. The selected profile provides its own issue prefix and spec directories. The global `coAuthor` trailer can be overridden per owner with `coAuthorByOwner`. The optional `default` uses the same required `issuePrefix` and `specDirs` as a repository entry. ## Schema The profile is CUE. rjc unifies it with the embedded schema in `schema.cue` (`#Config`) and refuses a file that does not validate. -Top-level fields, all required: +Top-level fields: | Field | Meaning | | --- | --- | | `owners` | GitHub owners rjc accepts repositories from (at least one). | -| `coAuthor` | `Co-authored-by` trailer added to commits and PR bodies. | +| `coAuthor` | Required global fallback `Co-authored-by` trailer for commits and PR bodies. | +| `coAuthorByOwner` | Optional owner-to-trailer map that overrides `coAuthor`. | +| `default` | Optional repository profile used when no `repos` entry matches. | | `reviewer` | Login requested as reviewer on tracking-fork PRs. | | `agentEmail` | jj `user.email` required in agent workspaces. | | `mergeBotEmail` | jj `user.email` required in the merge-bot workspace. | diff --git a/tools/rjc/policy.go b/tools/rjc/policy.go index 0706074a4..2ed3054f2 100644 --- a/tools/rjc/policy.go +++ b/tools/rjc/policy.go @@ -54,8 +54,8 @@ type Profile struct { func (p Profile) TrunkRemote() string { return p.Trunk + "@origin" } -// ProfileFor returns the profile for originURL. A repository the caller's -// Config does not list is refused: its issue prefix and spec dirs are unknown. +// ProfileFor returns the profile for originURL. An unlisted repository uses +// the caller's default profile when present; otherwise it is refused. func ProfileFor(cfg Config, originURL string) (Profile, error) { origin := strings.TrimSpace(originURL) var repo string @@ -91,10 +91,20 @@ func ProfileFor(cfg Config, originURL string) (Profile, error) { } } if !found { - return Profile{}, oops.Errorf("repository %s is not listed in the profile file", repo) + if cfg.Default == nil { + return Profile{}, oops.Errorf("repository %s is not listed in the profile file", repo) + } + p = *cfg.Default + p.Name = repo } p.LocalGates = []string{"bookmark-name", "issue-id"} p.CoAuthor = cfg.CoAuthor + for configuredOwner, coAuthor := range cfg.CoAuthorByOwner { + if strings.EqualFold(configuredOwner, owner) { + p.CoAuthor = coAuthor + break + } + } p.Reviewer = cfg.Reviewer p.GateRunnerDir = cfg.GateRunnerDir p.RootChecksDir = cfg.RootChecksDir diff --git a/tools/rjc/policy_test.go b/tools/rjc/policy_test.go index 8a9d4e314..d98a69891 100644 --- a/tools/rjc/policy_test.go +++ b/tools/rjc/policy_test.go @@ -1,6 +1,9 @@ package main import ( + "os" + "path/filepath" + "runtime" "slices" "strings" "testing" @@ -43,6 +46,116 @@ func TestProfileFor(t *testing.T) { } } +func TestProfileForDefaultAndOwnerCoAuthor(t *testing.T) { + _, source, _, ok := runtime.Caller(0) + if !ok { + t.Fatal("cannot locate the test source directory") + } + data, err := os.ReadFile(filepath.Join(filepath.Dir(source), "testdata", "profile.cue")) + if err != nil { + t.Fatal(err) + } + profileCUE := strings.Replace(string(data), `owners: ["example"]`, `owners: ["example", "second"]`, 1) + ` + +default: { + issuePrefix: "DF" + specDirs: ["docs/"] +} +coAuthorByOwner: { + EXAMPLE: "Org Author " +} +` + path := filepath.Join(t.TempDir(), "profile.cue") + writeTestFile(t, path, profileCUE) + withDefaults, err := loadConfig(path) + if err != nil { + t.Fatal(err) + } + tests := []struct { + name string + origin string + noDefault bool + oldFile bool + wantName string + wantPrefix string + wantAuthor string + wantHooks bool + wantError string + }{ + { + name: "unlisted repo gets default", + origin: "https://github.com/example/unlisted.git", + wantName: "example/unlisted", + wantPrefix: "DF", + wantAuthor: "Org Author ", + wantHooks: true, + }, + { + name: "listed repo gets its own entry", + origin: "https://github.com/example/factory.git", + wantName: "example/factory", + wantPrefix: "EX", + wantAuthor: "Org Author ", + wantHooks: false, + }, + { + name: "unlisted repo with no default is refused", + origin: "https://github.com/example/unlisted.git", + noDefault: true, + wantError: "is not listed in the profile file", + }, + { + name: "owner override wins over global coauthor", + origin: "https://github.com/example/app.git", + wantName: "example/app", + wantPrefix: "EX", + wantAuthor: "Org Author ", + wantHooks: true, + }, + { + name: "second owner uses global coauthor", + origin: "https://github.com/second/unlisted.git", + wantName: "second/unlisted", + wantPrefix: "DF", + wantAuthor: "Example Author ", + wantHooks: true, + }, + { + name: "old profile file still loads", + origin: "https://github.com/example/app.git", + oldFile: true, + wantName: "example/app", + wantPrefix: "EX", + wantAuthor: "Example Author ", + wantHooks: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg := withDefaults + if tt.noDefault { + cfg.Default = nil + } + if tt.oldFile { + cfg = loadExampleConfig(t) + } + profile, err := ProfileFor(cfg, tt.origin) + if tt.wantError != "" { + if err == nil || !strings.Contains(err.Error(), tt.wantError) { + t.Fatalf("ProfileFor error = %v, want %q", err, tt.wantError) + } + return + } + if err != nil { + t.Fatal(err) + } + if profile.Name != tt.wantName || profile.IssuePrefix != tt.wantPrefix || profile.CoAuthor != tt.wantAuthor || profile.Hooks != tt.wantHooks { + t.Fatalf("profile = %+v, want name %q, issue prefix %q, coauthor %q, hooks %t", profile, tt.wantName, tt.wantPrefix, tt.wantAuthor, tt.wantHooks) + } + }) + } +} + func TestJJVineProfileSkipsHooks(t *testing.T) { cfg := loadExampleConfig(t) profile, err := ProfileFor(cfg, "https://github.com/example/jj-vine.git") diff --git a/tools/rjc/profile.go b/tools/rjc/profile.go index ac2fea738..aef413eb5 100644 --- a/tools/rjc/profile.go +++ b/tools/rjc/profile.go @@ -25,6 +25,7 @@ var schemaCUE []byte type Config struct { Owners []string `json:"owners"` CoAuthor string `json:"coAuthor"` + CoAuthorByOwner map[string]string `json:"coAuthorByOwner,omitempty"` Reviewer string `json:"reviewer"` AgentEmail string `json:"agentEmail"` MergeBotEmail string `json:"mergeBotEmail"` @@ -33,6 +34,7 @@ type Config struct { GateRunnerDir string `json:"gateRunnerDir"` RootChecksDir string `json:"rootChecksDir"` DevenvNixpkgs string `json:"devenvNixpkgs"` + Default *Profile `json:"default"` Repos map[string]Profile `json:"repos"` } diff --git a/tools/rjc/schema.cue b/tools/rjc/schema.cue index be284ad92..481c9bd8c 100644 --- a/tools/rjc/schema.cue +++ b/tools/rjc/schema.cue @@ -1,12 +1,16 @@ package main // #Config is the caller's whole profile file. Every deployment value is a -// required field here: nebula supplies no default for any of them. +// required field here except default and coAuthorByOwner, which are optional. #Config: { // owners lists the GitHub owners rjc accepts repositories from. owners: [_, ...string] - // coAuthor is the trailer added to every commit and PR body. + // coAuthor is the fallback trailer added to every commit and PR body. coAuthor: string & !="" + // coAuthorByOwner optionally overrides coAuthor for a GitHub owner. + coAuthorByOwner?: { + [Owner=string]: string & !="" + } // reviewer is the login requested as reviewer on tracking-fork PRs. reviewer: string & !="" // agentEmail is the jj user.email required in agent workspaces. @@ -23,11 +27,13 @@ package main rootChecksDir: string & !="" // devenvNixpkgs pins the nixpkgs revision used to start a devenv shell. devenvNixpkgs: string & !="" + // default supplies repository policy when repos has no matching entry. + default?: #Repo repos: [Name=string]: #Repo & {name: Name} } #Repo: { - name: string + name?: string pr: *true | bool posture: *"none" | "tracking" | "owned" trunk: *"main" | string