JuliusBrussee/caveman · error

githubapp: exactly one repository is required

Error message

githubapp: exactly one repository is required

What it means

The library enforces least-agency token scoping: each minted token must target exactly one repository with a non-empty name. Broad multi-repo or empty-scope tokens are rejected before any network request. This is a deliberate design constraint so a leaked token can only affect a single repo.

Solutions

  1. Call MintInstallationToken once per repository with a single-element slice
  2. Validate the repo string (owner/name, non-blank) before calling
  3. If the repo is resolved dynamically, fail fast when it is empty instead of passing a blank name

Example fix

// before
repos := []string{"org/a", "org/b"}
tok, err := app.MintInstallationToken(ctx, id, repos, nil)
// after
for _, r := range repos {
	tok, err := app.MintInstallationToken(ctx, id, []string{r}, nil)
	// use token, then RevokeToken
}
Defensive patterns

Strategy: validation

Validate before calling

func okRepos(repos []string) bool {
	return len(repos) == 1 && strings.TrimSpace(repos[0]) != ""
}
if !okRepos(repos) { return fmt.Errorf("exactly one non-empty repo required") }

Try / catch

tok, err := app.MintInstallationToken(ctx, id, []string{repo}, nil)
if err != nil {
	if strings.Contains(err.Error(), "exactly one repository") {
		// fix caller to loop one repo at a time
	}
	return err
}

Prevention

When it happens

Trigger: App.MintInstallationToken called with repos nil, an empty slice, more than one entry, or a single entry that is empty/whitespace (e.g. [" "]).

Common situations: Passing an empty repos slice when the target repo is only known later; batch jobs looping over many repos but calling once with all of them; repo name built from an empty config value.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/4f226d5c1744202f. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/githubapp/githubapp.go:192

	if err := json.Unmarshal(raw, &out); err != nil {
		return out, fmt.Errorf("githubapp: decode installation: %w", err)
	}
	if out.ID != installationID || out.Account.ID <= 0 || strings.TrimSpace(out.Account.Login) == "" {
		return Installation{}, fmt.Errorf("githubapp: installation response identity mismatch")
	}
	return out, nil
}

// MintInstallationToken POSTs /app/installations/{id}/access_tokens narrowed to
// `repos` and `perms`, returning a ~1h token. Defaults (perms nil) are
// contents:write + pull_requests:write — enough to push a branch and open a draft
// PR, never to merge.
func (a *App) MintInstallationToken(ctx context.Context, installationID int64, repos []string, perms map[string]string) (InstallationToken, error) {
	if installationID <= 0 {
		return InstallationToken{}, fmt.Errorf("githubapp: installation id must be positive")
	}
	if len(repos) != 1 || strings.TrimSpace(repos[0]) == "" {
		return InstallationToken{}, fmt.Errorf("githubapp: exactly one repository is required")
	}
	repository := strings.TrimSpace(repos[0])
	if perms == nil {
		perms = map[string]string{"contents": "write", "pull_requests": "write"}
	}
	allowedPermissions := map[string]bool{"contents": true, "pull_requests": true}
	scopedPermissions := make(map[string]string, len(perms))
	for name, level := range perms {
		if !allowedPermissions[name] || (level != "read" && level != "write") {
			return InstallationToken{}, fmt.Errorf("githubapp: permission %q=%q exceeds the least-agency allowlist", name, level)
		}
		scopedPermissions[name] = level
	}
	jwt, err := a.AppJWT()
	if err != nil {
		return InstallationToken{}, err
	}
	body := map[string]any{"permissions": scopedPermissions, "repositories": []string{repository}}

View on GitHub (pinned to 3ee70a1026)