Skip to content

[duplicate-code] Duplicate Code Pattern: GitHub API Request Header Setup #4461

@github-actions

Description

@github-actions

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

  1. 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")
    }
  2. Replace inline header setting in both locations with this helper
  3. Estimated effort: 30 minutes

Implementation Checklist

  • Add ApplyGitHubAPIHeaders helper to internal/httputil or internal/envutil
  • Update internal/server/unified.go:callCollaboratorPermission to use the helper (and add User-Agent)
  • Update internal/proxy/proxy.go:forwardToGitHub to use the helper
  • Run make test-all to verify no regressions

Parent Issue

See parent analysis report: #4458
Related to #4458

Generated by Duplicate Code Detector · ● 3M ·

  • expires on May 1, 2026, 6:15 AM UTC

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions