Use int64 for GitHub database IDs and add lint check

Change all struct fields representing GitHub database IDs from int to
int64 to match the API spec and prevent potential overflow on 32-bit
architectures.

Add a custom go/analysis linter (idtype-checker) that flags struct
fields with ID-like names or JSON tags using int instead of int64,
integrated into make lint.

Closes #9247

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
William Martin 2026-05-12 17:33:57 +02:00
parent 1e6bb88c6d
commit d4cd79e28c
24 changed files with 272 additions and 58 deletions

View file

@ -45,6 +45,7 @@ completions: bin/gh$(EXE)
.PHONY: lint
lint:
golangci-lint run ./...
go run ./cmd/idtype-checker ./...
# just convenience tasks around `go test`
.PHONY: test

View file

@ -0,0 +1,16 @@
// Command idtype-checker runs the idtype analyzer to flag struct fields
// representing GitHub database IDs that use int instead of int64.
//
// Usage:
//
// go run ./cmd/idtype-checker ./...
package main
import (
"github.com/cli/cli/v2/pkg/linter/idtype"
"golang.org/x/tools/go/analysis/singlechecker"
)
func main() {
singlechecker.Main(idtype.Analyzer)
}

View file

@ -150,7 +150,7 @@ type RepositoryOwner struct {
// Repository represents a GitHub repository.
type Repository struct {
ID int `json:"id"`
ID int64 `json:"id"`
FullName string `json:"full_name"`
DefaultBranch string `json:"default_branch"`
Owner RepositoryOwner `json:"owner"`
@ -602,7 +602,7 @@ type Machine struct {
}
// GetCodespacesMachines returns the codespaces machines for the given repo, branch and location.
func (a *API) GetCodespacesMachines(ctx context.Context, repoID int, branch, location string, devcontainerPath string) ([]*Machine, error) {
func (a *API) GetCodespacesMachines(ctx context.Context, repoID int64, branch, location string, devcontainerPath string) ([]*Machine, error) {
reqURL := fmt.Sprintf("%s/repositories/%d/codespaces/machines", a.githubAPI, repoID)
req, err := http.NewRequest(http.MethodGet, reqURL, nil)
if err != nil {
@ -642,7 +642,7 @@ func (a *API) GetCodespacesMachines(ctx context.Context, repoID int, branch, loc
}
// GetCodespacesPermissionsCheck returns a bool indicating whether the user has accepted permissions for the given repo and devcontainer path.
func (a *API) GetCodespacesPermissionsCheck(ctx context.Context, repoID int, branch string, devcontainerPath string) (bool, error) {
func (a *API) GetCodespacesPermissionsCheck(ctx context.Context, repoID int64, branch string, devcontainerPath string) (bool, error) {
reqURL := fmt.Sprintf("%s/repositories/%d/codespaces/permissions_check", a.githubAPI, repoID)
req, err := http.NewRequest(http.MethodGet, reqURL, nil)
if err != nil {
@ -804,7 +804,7 @@ func (a *API) GetCodespaceBillableOwner(ctx context.Context, nwo string) (*User,
// CreateCodespaceParams are the required parameters for provisioning a Codespace.
type CreateCodespaceParams struct {
RepositoryID int
RepositoryID int64
IdleTimeoutMinutes int
RetentionPeriodMinutes *int
Branch string
@ -855,7 +855,7 @@ func (a *API) CreateCodespace(ctx context.Context, params *CreateCodespaceParams
}
type startCreateRequest struct {
RepositoryID int `json:"repository_id"`
RepositoryID int64 `json:"repository_id"`
IdleTimeoutMinutes int `json:"idle_timeout_minutes,omitempty"`
RetentionPeriodMinutes *int `json:"retention_period_minutes,omitempty"`
Ref string `json:"ref"`
@ -1009,7 +1009,7 @@ type DevContainerEntry struct {
// ListDevContainers returns a list of valid devcontainer.json files for the repo. Pass a negative limit to request all pages from
// the API until all devcontainer.json files have been fetched.
func (a *API) ListDevContainers(ctx context.Context, repoID int, branch string, limit int) (devcontainers []DevContainerEntry, err error) {
func (a *API) ListDevContainers(ctx context.Context, repoID int64, branch string, limit int) (devcontainers []DevContainerEntry, err error) {
perPage := 100
if limit > 0 && limit < 100 {
perPage = limit

View file

@ -35,12 +35,12 @@ type Job struct {
}
type JobActor struct {
ID int `json:"id"`
ID int64 `json:"id"`
Login string `json:"login"`
}
type JobPullRequest struct {
ID int `json:"id"`
ID int64 `json:"id"`
Number int `json:"number"`
BaseRef string `json:"base_ref,omitempty"`
}

View file

@ -147,7 +147,7 @@ func deleteRun(opts *DeleteOptions) error {
}
}
for _, cache := range caches.ActionsCaches {
toDelete = append(toDelete, strconv.Itoa(cache.Id))
toDelete = append(toDelete, strconv.FormatInt(cache.Id, 10))
}
} else {
toDelete = append(toDelete, opts.Identifier)

View file

@ -22,7 +22,7 @@ var CacheFields = []string{
type Cache struct {
CreatedAt time.Time `json:"created_at"`
Id int `json:"id"`
Id int64 `json:"id"`
Key string `json:"key"`
LastAccessedAt time.Time `json:"last_accessed_at"`
Ref string `json:"ref"`

View file

@ -76,10 +76,10 @@ type apiClient interface {
CreateCodespace(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error)
EditCodespace(ctx context.Context, codespaceName string, params *api.EditCodespaceParams) (*api.Codespace, error)
GetRepository(ctx context.Context, nwo string) (*api.Repository, error)
GetCodespacesMachines(ctx context.Context, repoID int, branch string, location string, devcontainerPath string) ([]*api.Machine, error)
GetCodespacesPermissionsCheck(ctx context.Context, repoID int, branch string, devcontainerPath string) (bool, error)
GetCodespacesMachines(ctx context.Context, repoID int64, branch string, location string, devcontainerPath string) ([]*api.Machine, error)
GetCodespacesPermissionsCheck(ctx context.Context, repoID int64, branch string, devcontainerPath string) (bool, error)
GetCodespaceRepositoryContents(ctx context.Context, codespace *api.Codespace, path string) ([]byte, error)
ListDevContainers(ctx context.Context, repoID int, branch string, limit int) (devcontainers []api.DevContainerEntry, err error)
ListDevContainers(ctx context.Context, repoID int64, branch string, limit int) (devcontainers []api.DevContainerEntry, err error)
GetCodespaceRepoSuggestions(ctx context.Context, partialSearch string, params api.RepoSearchParameters) ([]string, error)
GetCodespaceBillableOwner(ctx context.Context, nwo string) (*api.User, error)
HTTPClient() (*http.Client, error)

View file

@ -511,7 +511,7 @@ func (a *App) showStatus(ctx context.Context, codespace *api.Codespace) error {
}
// getMachineName prompts the user to select the machine type, or validates the machine if non-empty.
func getMachineName(ctx context.Context, apiClient apiClient, prompter SurveyPrompter, repoID int, machine, branch, location string, devcontainerPath string) (string, error) {
func getMachineName(ctx context.Context, apiClient apiClient, prompter SurveyPrompter, repoID int64, machine, branch, location string, devcontainerPath string) (string, error) {
machines, err := apiClient.GetCodespacesMachines(ctx, repoID, branch, location, devcontainerPath)
if err != nil {
return "", fmt.Errorf("error requesting machine instance types: %w", err)

View file

@ -164,7 +164,7 @@ func TestApp_Create(t *testing.T) {
name: "create codespace with nonexistent machine results in error",
fields: fields{
apiClient: apiCreateDefaults(&apiClientMock{
GetCodespacesMachinesFunc: func(ctx context.Context, repoID int, branch, location string, devcontainerPath string) ([]*api.Machine, error) {
GetCodespacesMachinesFunc: func(ctx context.Context, repoID int64, branch, location string, devcontainerPath string) ([]*api.Machine, error) {
return []*api.Machine{
{
Name: "GIGA",
@ -208,7 +208,7 @@ func TestApp_Create(t *testing.T) {
name: "create codespace with devcontainer path results in selecting the correct machine type",
fields: fields{
apiClient: apiCreateDefaults(&apiClientMock{
GetCodespacesMachinesFunc: func(ctx context.Context, repoID int, branch, location string, devcontainerPath string) ([]*api.Machine, error) {
GetCodespacesMachinesFunc: func(ctx context.Context, repoID int64, branch, location string, devcontainerPath string) ([]*api.Machine, error) {
if devcontainerPath == "" {
return []*api.Machine{
{
@ -270,7 +270,7 @@ func TestApp_Create(t *testing.T) {
name: "create codespace with default branch with default devcontainer if no path provided and no devcontainer files exist in the repo",
fields: fields{
apiClient: apiCreateDefaults(&apiClientMock{
ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) {
ListDevContainersFunc: func(ctx context.Context, repoID int64, branch string, limit int) ([]api.DevContainerEntry, error) {
return []api.DevContainerEntry{}, nil
},
CreateCodespaceFunc: func(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error) {
@ -305,7 +305,7 @@ func TestApp_Create(t *testing.T) {
name: "returns error when getting devcontainer paths fails",
fields: fields{
apiClient: apiCreateDefaults(&apiClientMock{
ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) {
ListDevContainersFunc: func(ctx context.Context, repoID int64, branch string, limit int) ([]api.DevContainerEntry, error) {
return nil, fmt.Errorf("some error")
},
}),
@ -793,7 +793,7 @@ func TestHandleAdditionalPermissions(t *testing.T) {
CreateCodespaceFunc: func(ctx context.Context, params *api.CreateCodespaceParams) (*api.Codespace, error) {
return nil, tt.createCodespaceErr
},
GetCodespacesPermissionsCheckFunc: func(ctx context.Context, repoID int, branch string, devcontainerPath string) (bool, error) {
GetCodespacesPermissionsCheckFunc: func(ctx context.Context, repoID int64, branch string, devcontainerPath string) (bool, error) {
if tt.pollForPermissionsErr != nil {
return false, tt.pollForPermissionsErr
}
@ -844,12 +844,12 @@ func apiCreateDefaults(c *apiClientMock) *apiClientMock {
}
}
if c.ListDevContainersFunc == nil {
c.ListDevContainersFunc = func(ctx context.Context, repoID int, branch string, limit int) ([]api.DevContainerEntry, error) {
c.ListDevContainersFunc = func(ctx context.Context, repoID int64, branch string, limit int) ([]api.DevContainerEntry, error) {
return []api.DevContainerEntry{{Path: ".devcontainer/devcontainer.json"}}, nil
}
}
if c.GetCodespacesMachinesFunc == nil {
c.GetCodespacesMachinesFunc = func(ctx context.Context, repoID int, branch, location string, devcontainerPath string) ([]*api.Machine, error) {
c.GetCodespacesMachinesFunc = func(ctx context.Context, repoID int64, branch, location string, devcontainerPath string) ([]*api.Machine, error) {
return []*api.Machine{
{
Name: "GIGA",

View file

@ -38,10 +38,10 @@ import (
// GetCodespaceRepositoryContentsFunc: func(ctx context.Context, codespace *codespacesAPI.Codespace, path string) ([]byte, error) {
// panic("mock out the GetCodespaceRepositoryContents method")
// },
// GetCodespacesMachinesFunc: func(ctx context.Context, repoID int, branch string, location string, devcontainerPath string) ([]*codespacesAPI.Machine, error) {
// GetCodespacesMachinesFunc: func(ctx context.Context, repoID int64, branch string, location string, devcontainerPath string) ([]*codespacesAPI.Machine, error) {
// panic("mock out the GetCodespacesMachines method")
// },
// GetCodespacesPermissionsCheckFunc: func(ctx context.Context, repoID int, branch string, devcontainerPath string) (bool, error) {
// GetCodespacesPermissionsCheckFunc: func(ctx context.Context, repoID int64, branch string, devcontainerPath string) (bool, error) {
// panic("mock out the GetCodespacesPermissionsCheck method")
// },
// GetOrgMemberCodespaceFunc: func(ctx context.Context, orgName string, userName string, codespaceName string) (*codespacesAPI.Codespace, error) {
@ -59,7 +59,7 @@ import (
// ListCodespacesFunc: func(ctx context.Context, opts codespacesAPI.ListCodespacesOptions) ([]*codespacesAPI.Codespace, error) {
// panic("mock out the ListCodespaces method")
// },
// ListDevContainersFunc: func(ctx context.Context, repoID int, branch string, limit int) ([]codespacesAPI.DevContainerEntry, error) {
// ListDevContainersFunc: func(ctx context.Context, repoID int64, branch string, limit int) ([]codespacesAPI.DevContainerEntry, error) {
// panic("mock out the ListDevContainers method")
// },
// ServerURLFunc: func() string {
@ -100,10 +100,10 @@ type apiClientMock struct {
GetCodespaceRepositoryContentsFunc func(ctx context.Context, codespace *codespacesAPI.Codespace, path string) ([]byte, error)
// GetCodespacesMachinesFunc mocks the GetCodespacesMachines method.
GetCodespacesMachinesFunc func(ctx context.Context, repoID int, branch string, location string, devcontainerPath string) ([]*codespacesAPI.Machine, error)
GetCodespacesMachinesFunc func(ctx context.Context, repoID int64, branch string, location string, devcontainerPath string) ([]*codespacesAPI.Machine, error)
// GetCodespacesPermissionsCheckFunc mocks the GetCodespacesPermissionsCheck method.
GetCodespacesPermissionsCheckFunc func(ctx context.Context, repoID int, branch string, devcontainerPath string) (bool, error)
GetCodespacesPermissionsCheckFunc func(ctx context.Context, repoID int64, branch string, devcontainerPath string) (bool, error)
// GetOrgMemberCodespaceFunc mocks the GetOrgMemberCodespace method.
GetOrgMemberCodespaceFunc func(ctx context.Context, orgName string, userName string, codespaceName string) (*codespacesAPI.Codespace, error)
@ -121,7 +121,7 @@ type apiClientMock struct {
ListCodespacesFunc func(ctx context.Context, opts codespacesAPI.ListCodespacesOptions) ([]*codespacesAPI.Codespace, error)
// ListDevContainersFunc mocks the ListDevContainers method.
ListDevContainersFunc func(ctx context.Context, repoID int, branch string, limit int) ([]codespacesAPI.DevContainerEntry, error)
ListDevContainersFunc func(ctx context.Context, repoID int64, branch string, limit int) ([]codespacesAPI.DevContainerEntry, error)
// ServerURLFunc mocks the ServerURL method.
ServerURLFunc func() string
@ -200,7 +200,7 @@ type apiClientMock struct {
// Ctx is the ctx argument value.
Ctx context.Context
// RepoID is the repoID argument value.
RepoID int
RepoID int64
// Branch is the branch argument value.
Branch string
// Location is the location argument value.
@ -213,7 +213,7 @@ type apiClientMock struct {
// Ctx is the ctx argument value.
Ctx context.Context
// RepoID is the repoID argument value.
RepoID int
RepoID int64
// Branch is the branch argument value.
Branch string
// DevcontainerPath is the devcontainerPath argument value.
@ -257,7 +257,7 @@ type apiClientMock struct {
// Ctx is the ctx argument value.
Ctx context.Context
// RepoID is the repoID argument value.
RepoID int
RepoID int64
// Branch is the branch argument value.
Branch string
// Limit is the limit argument value.
@ -582,13 +582,13 @@ func (mock *apiClientMock) GetCodespaceRepositoryContentsCalls() []struct {
}
// GetCodespacesMachines calls GetCodespacesMachinesFunc.
func (mock *apiClientMock) GetCodespacesMachines(ctx context.Context, repoID int, branch string, location string, devcontainerPath string) ([]*codespacesAPI.Machine, error) {
func (mock *apiClientMock) GetCodespacesMachines(ctx context.Context, repoID int64, branch string, location string, devcontainerPath string) ([]*codespacesAPI.Machine, error) {
if mock.GetCodespacesMachinesFunc == nil {
panic("apiClientMock.GetCodespacesMachinesFunc: method is nil but apiClient.GetCodespacesMachines was just called")
}
callInfo := struct {
Ctx context.Context
RepoID int
RepoID int64
Branch string
Location string
DevcontainerPath string
@ -611,14 +611,14 @@ func (mock *apiClientMock) GetCodespacesMachines(ctx context.Context, repoID int
// len(mockedapiClient.GetCodespacesMachinesCalls())
func (mock *apiClientMock) GetCodespacesMachinesCalls() []struct {
Ctx context.Context
RepoID int
RepoID int64
Branch string
Location string
DevcontainerPath string
} {
var calls []struct {
Ctx context.Context
RepoID int
RepoID int64
Branch string
Location string
DevcontainerPath string
@ -630,13 +630,13 @@ func (mock *apiClientMock) GetCodespacesMachinesCalls() []struct {
}
// GetCodespacesPermissionsCheck calls GetCodespacesPermissionsCheckFunc.
func (mock *apiClientMock) GetCodespacesPermissionsCheck(ctx context.Context, repoID int, branch string, devcontainerPath string) (bool, error) {
func (mock *apiClientMock) GetCodespacesPermissionsCheck(ctx context.Context, repoID int64, branch string, devcontainerPath string) (bool, error) {
if mock.GetCodespacesPermissionsCheckFunc == nil {
panic("apiClientMock.GetCodespacesPermissionsCheckFunc: method is nil but apiClient.GetCodespacesPermissionsCheck was just called")
}
callInfo := struct {
Ctx context.Context
RepoID int
RepoID int64
Branch string
DevcontainerPath string
}{
@ -657,13 +657,13 @@ func (mock *apiClientMock) GetCodespacesPermissionsCheck(ctx context.Context, re
// len(mockedapiClient.GetCodespacesPermissionsCheckCalls())
func (mock *apiClientMock) GetCodespacesPermissionsCheckCalls() []struct {
Ctx context.Context
RepoID int
RepoID int64
Branch string
DevcontainerPath string
} {
var calls []struct {
Ctx context.Context
RepoID int
RepoID int64
Branch string
DevcontainerPath string
}
@ -849,13 +849,13 @@ func (mock *apiClientMock) ListCodespacesCalls() []struct {
}
// ListDevContainers calls ListDevContainersFunc.
func (mock *apiClientMock) ListDevContainers(ctx context.Context, repoID int, branch string, limit int) ([]codespacesAPI.DevContainerEntry, error) {
func (mock *apiClientMock) ListDevContainers(ctx context.Context, repoID int64, branch string, limit int) ([]codespacesAPI.DevContainerEntry, error) {
if mock.ListDevContainersFunc == nil {
panic("apiClientMock.ListDevContainersFunc: method is nil but apiClient.ListDevContainers was just called")
}
callInfo := struct {
Ctx context.Context
RepoID int
RepoID int64
Branch string
Limit int
}{
@ -876,13 +876,13 @@ func (mock *apiClientMock) ListDevContainers(ctx context.Context, repoID int, br
// len(mockedapiClient.ListDevContainersCalls())
func (mock *apiClientMock) ListDevContainersCalls() []struct {
Ctx context.Context
RepoID int
RepoID int64
Branch string
Limit int
} {
var calls []struct {
Ctx context.Context
RepoID int
RepoID int64
Branch string
Limit int
}

View file

@ -74,7 +74,7 @@ func deleteRun(opts *DeleteOptions) error {
id := ""
for _, gpgKey := range gpgKeys {
if gpgKey.KeyID == opts.KeyID {
id = strconv.Itoa(gpgKey.ID)
id = strconv.FormatInt(gpgKey.ID, 10)
break
}
}

View file

@ -11,7 +11,7 @@ import (
)
type gpgKey struct {
ID int
ID int64
KeyID string `json:"key_id"`
}

View file

@ -3,7 +3,7 @@ package shared
import "github.com/cli/cli/v2/pkg/cmdutil"
type Autolink struct {
ID int `json:"id"`
ID int64 `json:"id"`
IsAlphanumeric bool `json:"is_alphanumeric"`
KeyPrefix string `json:"key_prefix"`
URLTemplate string `json:"url_template"`

View file

@ -13,7 +13,7 @@ import (
)
type deployKey struct {
ID int `json:"id"`
ID int64 `json:"id"`
Key string `json:"key"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`

View file

@ -81,7 +81,7 @@ func listRun(opts *ListOptions) error {
now := time.Now()
for _, deployKey := range deployKeys {
sshID := strconv.Itoa(deployKey.ID)
sshID := strconv.FormatInt(deployKey.ID, 10)
t.AddField(sshID)
t.AddField(deployKey.Title)
sshType := "read-only"

View file

@ -164,7 +164,7 @@ func listRun(opts *ListOptions) error {
tp := tableprinter.New(opts.IO, tableprinter.WithHeader("ID", "NAME", "SOURCE", "STATUS", "RULES"))
for _, rs := range result.Rulesets {
tp.AddField(strconv.Itoa(rs.DatabaseId), tableprinter.WithColor(cs.Cyan))
tp.AddField(strconv.FormatInt(rs.DatabaseId, 10), tableprinter.WithColor(cs.Cyan))
tp.AddField(rs.Name, tableprinter.WithColor(cs.Bold))
tp.AddField(shared.RulesetSource(rs))
tp.AddField(strings.ToLower(rs.Enforcement))

View file

@ -10,7 +10,7 @@ import (
)
type RulesetGraphQL struct {
DatabaseId int
DatabaseId int64
Name string
Target string
Enforcement string
@ -24,13 +24,13 @@ type RulesetGraphQL struct {
}
type RulesetREST struct {
Id int
Id int64
Name string
Target string
Enforcement string
CurrentUserCanBypass string `json:"current_user_can_bypass"`
BypassActors []struct {
ActorId int `json:"actor_id"`
ActorId int64 `json:"actor_id"`
ActorType string `json:"actor_type"`
BypassMode string `json:"bypass_mode"`
} `json:"bypass_actors"`
@ -50,7 +50,7 @@ type RulesetRule struct {
Parameters map[string]interface{}
RulesetSourceType string `json:"ruleset_source_type"`
RulesetSource string `json:"ruleset_source"`
RulesetId int `json:"ruleset_id"`
RulesetId int64 `json:"ruleset_id"`
}
// Returns the source of the ruleset in the format "owner/name (repo)" or "owner (org)"

View file

@ -150,7 +150,7 @@ func viewRun(opts *ViewOptions) error {
}
if rs != nil {
opts.ID = strconv.Itoa(rs.DatabaseId)
opts.ID = strconv.FormatInt(rs.DatabaseId, 10)
// can't get a ruleset lower in the chain than what was queried, so no need to handle repos here
if rs.Source.TypeName == "Organization" {
@ -185,7 +185,7 @@ func viewRun(opts *ViewOptions) error {
}
fmt.Fprintf(w, "\n%s\n", cs.Bold(rs.Name))
fmt.Fprintf(w, "ID: %s\n", cs.Cyan(strconv.Itoa(rs.Id)))
fmt.Fprintf(w, "ID: %s\n", cs.Cyan(strconv.FormatInt(rs.Id, 10)))
fmt.Fprintf(w, "Source: %s (%s)\n", rs.Source, rs.SourceType)
fmt.Fprint(w, "Enforcement: ")
@ -262,7 +262,7 @@ func selectRulesetID(rsList *shared.RulesetList, p prompter.Prompter, cs *iostre
for i, rs := range rsList.Rulesets {
s := fmt.Sprintf(
"%s: %s | %s | contains %s | configured in %s",
cs.Cyan(strconv.Itoa(rs.DatabaseId)),
cs.Cyan(strconv.FormatInt(rs.DatabaseId, 10)),
rs.Name,
strings.ToLower(rs.Enforcement),
text.Pluralize(rs.Rules.TotalCount, "rule"),

View file

@ -64,7 +64,7 @@ type tagEntry struct {
// rulesetsResponse is a single ruleset from the rulesets API.
type rulesetsResponse struct {
ID int `json:"id"`
ID int64 `json:"id"`
Name string `json:"name"`
Target string `json:"target"`
Enforcement string `json:"enforcement"`

View file

@ -83,7 +83,7 @@ func listRun(opts *ListOptions) error {
now := time.Now()
for _, sshKey := range sshKeys {
id := strconv.Itoa(sshKey.ID)
id := strconv.FormatInt(sshKey.ID, 10)
if t.IsTTY() {
t.AddField(sshKey.Title)
t.AddField(id)

View file

@ -17,7 +17,7 @@ const (
)
type sshKey struct {
ID int
ID int64
Key string
Title string
Type string

View file

@ -0,0 +1,119 @@
// Package idtype provides a go/analysis analyzer that flags struct fields
// representing GitHub database IDs that use int instead of int64.
//
// GitHub database IDs are internally 64-bit, and the REST API OpenAPI spec
// declares many of them with format: int64. Using Go's int (which is
// platform-dependent) risks overflow on 32-bit architectures.
package idtype
import (
"go/ast"
"go/token"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
var Analyzer = &analysis.Analyzer{
Name: "idtype",
Doc: "checks that struct fields representing GitHub database IDs use int64 instead of int",
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
func run(pass *analysis.Pass) (interface{}, error) {
insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.StructType)(nil),
}
insp.Preorder(nodeFilter, func(n ast.Node) {
st := n.(*ast.StructType)
for _, field := range st.Fields.List {
if !isIntType(field.Type) {
continue
}
for _, name := range field.Names {
if isIDField(name.Name, field.Tag) {
pass.Reportf(field.Pos(), "struct field %s looks like a GitHub database ID but uses int; use int64 instead", name.Name)
}
}
}
})
return nil, nil
}
// isIntType checks if the type expression is the bare "int" type.
func isIntType(expr ast.Expr) bool {
ident, ok := expr.(*ast.Ident)
return ok && ident.Name == "int"
}
// isIDField returns true if the field name or JSON tag suggests this is a
// database ID. It checks:
// - Field name is exactly "ID" or "Id"
// - Field name ends with "ID" or "Id" (e.g. DatabaseId, RepositoryID, ActorId)
// - JSON tag is exactly "id" or ends with "_id"
func isIDField(fieldName string, tag *ast.BasicLit) bool {
if matchesIDName(fieldName) {
return true
}
if tag != nil && tag.Kind == token.STRING {
jsonTag := extractJSONTag(tag.Value)
if matchesIDTag(jsonTag) {
return true
}
}
return false
}
// matchesIDName checks if a Go field name looks like a database ID field.
func matchesIDName(name string) bool {
if name == "ID" || name == "Id" {
return true
}
if strings.HasSuffix(name, "ID") || strings.HasSuffix(name, "Id") {
return true
}
return false
}
// matchesIDTag checks if a JSON tag value looks like a database ID field.
func matchesIDTag(tag string) bool {
if tag == "" {
return false
}
if tag == "id" {
return true
}
if strings.HasSuffix(tag, "_id") {
return true
}
return false
}
// extractJSONTag extracts the JSON field name from a struct tag literal.
// For example, given `json:"repository_id,omitempty"`, it returns "repository_id".
func extractJSONTag(rawTag string) string {
// Remove surrounding backticks or quotes
tag := strings.Trim(rawTag, "`\"")
const prefix = `json:"`
idx := strings.Index(tag, prefix)
if idx < 0 {
return ""
}
tag = tag[idx+len(prefix):]
if end := strings.Index(tag, `"`); end >= 0 {
tag = tag[:end]
}
// Strip options like omitempty
if comma := strings.Index(tag, ","); comma >= 0 {
tag = tag[:comma]
}
return tag
}

View file

@ -0,0 +1,12 @@
package idtype
import (
"testing"
"golang.org/x/tools/go/analysis/analysistest"
)
func TestAnalyzer(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, Analyzer, "example")
}

View file

@ -0,0 +1,66 @@
package example
// Should be flagged - int fields with ID-like names or tags.
type Repository struct {
ID int `json:"id"` // want `struct field ID looks like a GitHub database ID but uses int; use int64 instead`
Name string `json:"name"`
}
type Cache struct {
Id int `json:"id"` // want `struct field Id looks like a GitHub database ID but uses int; use int64 instead`
Key string `json:"key"`
}
type RulesetRule struct {
Type string
RulesetId int `json:"ruleset_id"` // want `struct field RulesetId looks like a GitHub database ID but uses int; use int64 instead`
}
type Autolink struct {
ID int `json:"id"` // want `struct field ID looks like a GitHub database ID but uses int; use int64 instead`
}
type DeployKey struct {
ID int // want `struct field ID looks like a GitHub database ID but uses int; use int64 instead`
}
type Actor struct {
ActorId int `json:"actor_id"` // want `struct field ActorId looks like a GitHub database ID but uses int; use int64 instead`
}
type WithDatabaseID struct {
DatabaseId int // want `struct field DatabaseId looks like a GitHub database ID but uses int; use int64 instead`
}
type RepositoryIDField struct {
RepositoryID int `json:"repository_id"` // want `struct field RepositoryID looks like a GitHub database ID but uses int; use int64 instead`
}
// Should NOT be flagged - correct types or non-ID fields.
type CorrectRepository struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
type CorrectRule struct {
RulesetId int64 `json:"ruleset_id"`
}
type NotAnID struct {
Count int `json:"count"`
Timeout int `json:"idle_timeout_minutes"`
Name string `json:"name"`
Description string
}
type StringID struct {
ID string `json:"id"`
}
type NumberField struct {
TotalCount int
Number int `json:"number"`
Port int `json:"port"`
}