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
14 changes: 11 additions & 3 deletions docs/guides/dev/cli-internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,17 @@ must treat `truncated: true` as an incomplete result. GitLab merge-request
discussions are mapped to the same thread model, including resolution state,
comments, positions, and resolver identity when GitLab provides it. Each comment
also includes `author_role` and `author_role_verified`; resolved threads include
`resolved_by_role` and `resolved_by_role_verified`. A role is `none` with
`verified: true` for an authoritative non-member result, and `verified: false`
when the actor is a bot/unknown or the permission lookup fails.
`resolved_by_role` and `resolved_by_role_verified`. `verified: true` means the
forge authoritatively established the role, not merely that the lookup request
succeeded. On GitHub, a built-in role, or a custom role that normalizes through
effective permission flags or the legacy `permission` field, is reported with
`verified: true`. A role is `none` with `verified: true` only for an
authoritative non-member result: a 404 from the collaborator permission
endpoint, or an explicit `role_name: none` with no conflicting `permission`. A
role is `none` with `verified: false` when the actor is a bot/unknown, the
permission lookup fails, or a successful response has missing, contradictory,
or unnormalizable permission data. Consumers must treat unverified roles as
untrusted (fail closed).

The `mint`, `inference`, and `github` subcommands decompose setup into role-specific operations for organizations that separate GCP and GitHub responsibilities:

Expand Down
59 changes: 48 additions & 11 deletions internal/forge/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3746,7 +3746,7 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re
if cached, ok := roleCache[login]; ok {
return cached.role, cached.verified
}
permission, err := c.GetCollaboratorPermission(ctx, owner, repo, login)
perm, err := c.fetchCollaboratorPermission(ctx, owner, repo, login)
if err != nil {
verified := forge.IsNotFound(err)
roleCache[login] = struct {
Expand All @@ -3755,12 +3755,18 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re
}{normevent.RoleNone, verified}
return normevent.RoleNone, verified
}
role := normevent.MapGitHubPermission(permission)
// A successful response is verified only when it authoritatively
// establishes the role; unnormalizable data stays RoleNone unverified.
permission, verified := perm.normalizedRole()
role := normevent.RoleNone
if verified {
role = normevent.MapGitHubPermission(permission)
}
roleCache[login] = struct {
role normevent.ActorRole
verified bool
}{role, true}
return role, true
}{role, verified}
return role, verified
}
for page := 1; page <= 20; page++ {
variables := map[string]any{
Expand Down Expand Up @@ -4356,20 +4362,30 @@ func (c *LiveClient) GetAppClientID(ctx context.Context, slug string) (string, e
}

func (c *LiveClient) GetCollaboratorPermission(ctx context.Context, owner, repo, username string) (string, error) {
perm, err := c.fetchCollaboratorPermission(ctx, owner, repo, username)
if err != nil {
return "", err
}
if perm.RoleName == "" {
return "", fmt.Errorf("%w: no permission for %s", forge.ErrNotFound, username)
}
return perm.baseRole(), nil
}

// fetchCollaboratorPermission returns the decoded collaborator permission
// response without interpreting it.
func (c *LiveClient) fetchCollaboratorPermission(ctx context.Context, owner, repo, username string) (collaboratorPermission, error) {
path := fmt.Sprintf("/repos/%s/%s/collaborators/%s/permission",
url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(username))
resp, err := c.get(ctx, path)
if err != nil {
return "", fmt.Errorf("get collaborator permission for %s: %w", username, err)
return collaboratorPermission{}, fmt.Errorf("get collaborator permission for %s: %w", username, err)
}
var perm collaboratorPermission
if err := decodeJSON(resp, &perm); err != nil {
return "", fmt.Errorf("decode collaborator permission for %s: %w", username, err)
return collaboratorPermission{}, fmt.Errorf("decode collaborator permission for %s: %w", username, err)
}
if perm.RoleName == "" {
return "", fmt.Errorf("%w: no permission for %s", forge.ErrNotFound, username)
}
return perm.baseRole(), nil
return perm, nil
}

// collaboratorPermission is the subset of GitHub's collaborator permission
Expand All @@ -4393,10 +4409,31 @@ type collaboratorPermission struct {
// legacy permission field, else "none". Keep in sync with has_repo_permission
// in reusable-dispatch.yml.
func (p collaboratorPermission) baseRole() string {
role, _ := p.normalizedRole()
return role
}

// normalizedRole returns baseRole plus whether the response authoritatively
// establishes that role. Built-in role names and custom role names that
// normalize to a real role through effective flags or the legacy permission
// are authoritative. "none" is authoritative only when GitHub explicitly
// reports role_name "none" with no conflicting legacy permission; a custom
// role whose data cannot be normalized, or a missing role_name, is not.
func (p collaboratorPermission) normalizedRole() (string, bool) {
switch p.RoleName {
case "admin", "maintain", "write", "triage", "read":
return p.RoleName
return p.RoleName, true
}
role := p.effectiveRole()
if role != "none" {
return role, p.RoleName != "" && p.RoleName != "none"
}
return role, p.RoleName == "none" && (p.Permission == "" || p.Permission == "none")
}

// effectiveRole resolves a non-built-in role name from GitHub's effective
// permission flags when present, otherwise from the legacy permission field.
func (p collaboratorPermission) effectiveRole() string {
if f := p.User.Permissions; f != nil {
switch {
case f.Admin:
Expand Down
11 changes: 11 additions & 0 deletions internal/forge/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4960,6 +4960,17 @@ func TestGetCollaboratorPermission(t *testing.T) {
require.Error(t, err)
assert.True(t, forge.IsNotFound(err))
})

t.Run("missing role name is not found", func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"permission":"write"}`))
}))
defer srv.Close()

_, err := newTestClient(t, srv).GetCollaboratorPermission(context.Background(), "o", "r", "alice")
require.Error(t, err)
assert.True(t, forge.IsNotFound(err))
})
}

func TestAddCollaborator(t *testing.T) {
Expand Down
60 changes: 60 additions & 0 deletions internal/forge/github/review_threads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,66 @@ func TestListPullRequestReviewThreads_RoleLookupFailures(t *testing.T) {
assert.False(t, got.Threads[0].Comments[1].AuthorRoleVerified, "bots do not use human permission roles")
}

func TestListPullRequestReviewThreads_RoleVerification(t *testing.T) {
cases := []struct {
name string
body string
status int
wantRole normevent.ActorRole
wantVerified bool
}{
{name: "built-in admin", body: `{"permission":"admin","role_name":"admin"}`, wantRole: normevent.RoleAdmin, wantVerified: true},
{name: "built-in maintain", body: `{"permission":"write","role_name":"maintain"}`, wantRole: normevent.RoleMaintain, wantVerified: true},
{name: "built-in write", body: `{"permission":"write","role_name":"write"}`, wantRole: normevent.RoleWrite, wantVerified: true},
{name: "built-in triage", body: `{"permission":"read","role_name":"triage"}`, wantRole: normevent.RoleTriage, wantVerified: true},
{name: "built-in read", body: `{"permission":"read","role_name":"read"}`, wantRole: normevent.RoleRead, wantVerified: true},
{name: "custom maintain flags", body: `{"permission":"write","role_name":"Repo Maintainer","user":{"permissions":{"maintain":true,"push":true,"triage":true,"pull":true}}}`, wantRole: normevent.RoleMaintain, wantVerified: true},
{name: "custom write flags", body: `{"permission":"write","role_name":"Dev","user":{"permissions":{"push":true,"pull":true}}}`, wantRole: normevent.RoleWrite, wantVerified: true},
{name: "custom legacy permission", body: `{"permission":"write","role_name":"Dev","user":{"permissions":null}}`, wantRole: normevent.RoleWrite, wantVerified: true},
{name: "explicit none", body: `{"permission":"none","role_name":"none"}`, wantRole: normevent.RoleNone, wantVerified: true},
{name: "authoritative non-member 404", status: http.StatusNotFound, wantRole: normevent.RoleNone, wantVerified: true},
{name: "custom role without usable data", body: `{"role_name":"Mystery"}`, wantRole: normevent.RoleNone, wantVerified: false},
{name: "custom role with unknown legacy permission", body: `{"permission":"none","role_name":"Mystery"}`, wantRole: normevent.RoleNone, wantVerified: false},
{name: "custom role with all flags false", body: `{"permission":"write","role_name":"Dev","user":{"permissions":{"admin":false,"maintain":false,"push":false,"triage":false,"pull":false}}}`, wantRole: normevent.RoleNone, wantVerified: false},
{name: "none role with conflicting legacy permission", body: `{"permission":"write","role_name":"none"}`, wantRole: normevent.RoleNone, wantVerified: false},
{name: "none role with conflicting flags", body: `{"role_name":"none","user":{"permissions":{"push":true}}}`, wantRole: normevent.RoleNone, wantVerified: false},
{name: "missing role name", body: `{"permission":"write"}`, wantRole: normevent.RoleNone, wantVerified: false},
{name: "malformed response", body: `{"role_name":"Dev","user":{"permissions":{"push":"true"}}}`, wantRole: normevent.RoleNone, wantVerified: false},
{name: "transient lookup failure", status: http.StatusInternalServerError, wantRole: normevent.RoleNone, wantVerified: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
lookups := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
require.Equal(t, "/repos/owner/repo/collaborators/alice/permission", r.URL.Path)
lookups++
if tc.status != 0 {
w.WriteHeader(tc.status)
return
}
_, _ = w.Write([]byte(tc.body))
return
}
_, _ = w.Write([]byte(`{"data":{"repository":{"pullRequest":{"reviewThreads":{"pageInfo":{"hasNextPage":false,"endCursor":""},"nodes":[{"id":"thread-1","isResolved":true,"resolvedBy":{"login":"alice","__typename":"User"},"comments":{"pageInfo":{"hasNextPage":false},"nodes":[{"author":{"login":"alice","__typename":"User"},"body":"comment","createdAt":"2026-10-06T10:00:00Z"}]}}]}}}}}`))
}))
defer srv.Close()

got, err := newTestClient(t, srv).ListPullRequestReviewThreads(context.Background(), "owner", "repo", 1)
require.NoError(t, err)
require.Len(t, got.Threads, 1)
require.Len(t, got.Threads[0].Comments, 1)
assert.Equal(t, tc.wantRole, got.Threads[0].ResolvedByRole)
assert.Equal(t, tc.wantVerified, got.Threads[0].ResolvedByRoleVerified)
assert.Equal(t, tc.wantRole, got.Threads[0].Comments[0].AuthorRole)
assert.Equal(t, tc.wantVerified, got.Threads[0].Comments[0].AuthorRoleVerified)
if tc.status != http.StatusInternalServerError {
assert.Equal(t, 1, lookups, "role lookups are cached per login")
}
})
}
}

func TestListPullRequestReviewThreads_PaginatesAndCaps(t *testing.T) {
var calls int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading