Part of duplicate code analysis: #4458
Summary
The GitHub API request headers (Authorization, Accept: application/vnd.github+json, User-Agent) are set in two separate places: once in internal/server/unified.go for the guard's direct HTTP call, and once in internal/proxy/proxy.go's forwardToGitHub. The server-side path is missing User-Agent and sets auth differently.
Duplication Details
Pattern: GitHub API request header setup
- Severity: Low
- Occurrences: 2
- Locations:
internal/server/unified.go lines ~314–317 (callCollaboratorPermission)
internal/proxy/proxy.go lines ~355–364 (forwardToGitHub)
internal/server/unified.go:
req.Header.Set("Authorization", "token "+token)
req.Header.Set("Accept", "application/vnd.github+json")
// Note: no User-Agent set
internal/proxy/proxy.go (forwardToGitHub):
if clientAuth != "" {
req.Header.Set("Authorization", clientAuth)
} else if s.githubToken != "" {
req.Header.Set("Authorization", "token "+s.githubToken)
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("User-Agent", "awmg-proxy/1.0")
The server path is missing User-Agent and does not have the client-auth-first fallback logic.
Impact Analysis
- Maintainability: GitHub API version header or User-Agent changes require updates in multiple places
- Bug Risk: The server path lacks
User-Agent — GitHub may rate-limit or flag requests without it
- Code Bloat: Minor (~5 lines) but divergence is the real risk
Refactoring Recommendations
- Extract
applyGitHubHeaders(req *http.Request, auth string) to internal/httputil or internal/envutil:
func ApplyGitHubAPIHeaders(req *http.Request, authToken string) {
req.Header.Set("Authorization", "token "+authToken)
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("User-Agent", "awmg/1.0")
}
- Replace inline header setting in both locations with this helper
- Estimated effort: 30 minutes
Implementation Checklist
Parent Issue
See parent analysis report: #4458
Related to #4458
Generated by Duplicate Code Detector · ● 3M · ◷
Part of duplicate code analysis: #4458
Summary
The GitHub API request headers (
Authorization,Accept: application/vnd.github+json,User-Agent) are set in two separate places: once ininternal/server/unified.gofor the guard's direct HTTP call, and once ininternal/proxy/proxy.go'sforwardToGitHub. The server-side path is missingUser-Agentand sets auth differently.Duplication Details
Pattern: GitHub API request header setup
internal/server/unified.golines ~314–317 (callCollaboratorPermission)internal/proxy/proxy.golines ~355–364 (forwardToGitHub)internal/server/unified.go:internal/proxy/proxy.go(forwardToGitHub):The server path is missing
User-Agentand does not have the client-auth-first fallback logic.Impact Analysis
User-Agent— GitHub may rate-limit or flag requests without itRefactoring Recommendations
applyGitHubHeaders(req *http.Request, auth string)tointernal/httputilorinternal/envutil:Implementation Checklist
ApplyGitHubAPIHeadershelper tointernal/httputilorinternal/envutilinternal/server/unified.go:callCollaboratorPermissionto use the helper (and add User-Agent)internal/proxy/proxy.go:forwardToGitHubto use the helpermake test-allto verify no regressionsParent Issue
See parent analysis report: #4458
Related to #4458