Skip to content
Open
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
10 changes: 6 additions & 4 deletions tools/rjc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file.cue> submit --title "feat(scope): subject"
Expand All @@ -13,18 +13,20 @@ RJC_PROFILES=<file.cue> rjc status

`--profiles` wins over `RJC_PROFILES`. When neither is set, rjc exits 2 with `no profile file: pass --profiles <file> 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 <owner>/<name> 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 <owner>/<name> 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. |
Expand Down
16 changes: 13 additions & 3 deletions tools/rjc/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
113 changes: 113 additions & 0 deletions tools/rjc/policy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
Expand Down Expand Up @@ -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 <org@example.com>"
}
`
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 <org@example.com>",
wantHooks: true,
},
{
name: "listed repo gets its own entry",
origin: "https://github.com/example/factory.git",
wantName: "example/factory",
wantPrefix: "EX",
wantAuthor: "Org Author <org@example.com>",
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 <org@example.com>",
wantHooks: true,
},
{
name: "second owner uses global coauthor",
origin: "https://github.com/second/unlisted.git",
wantName: "second/unlisted",
wantPrefix: "DF",
wantAuthor: "Example Author <author@example.com>",
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 <author@example.com>",
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")
Expand Down
2 changes: 2 additions & 0 deletions tools/rjc/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
}

Expand Down
12 changes: 9 additions & 3 deletions tools/rjc/schema.cue
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
Loading