Skip to content
Open
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
20 changes: 18 additions & 2 deletions src/providers/plugins/sso/proxy/plugins/header-injection.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ProxyPlugin, PluginContext, ProxyInterceptor } from './types.js';
import { ProxyContext } from '../proxy-types.js';
import { ProviderRegistry } from '../../../../core/registry.js';
import { logger } from '../../../../../utils/logger.js';
import { detectGitBranch } from '../../../../../utils/processes.js';

export class HeaderInjectionPlugin implements ProxyPlugin {
id = '@codemie/proxy-headers';
Expand All @@ -22,11 +23,25 @@ export class HeaderInjectionPlugin implements ProxyPlugin {
}
}

const BRANCH_CACHE_TTL_MS = 30_000;

class HeaderInjectionInterceptor implements ProxyInterceptor {
name = 'header-injection';

private cachedBranch: string | undefined;
private branchCachedAt = 0;

constructor(private context: PluginContext) {}

private async getCurrentBranch(): Promise<string | undefined> {
if (Date.now() - this.branchCachedAt < BRANCH_CACHE_TTL_MS) {
return this.cachedBranch;
}
this.cachedBranch = await detectGitBranch(process.cwd()) ?? this.context.config.branch;
this.branchCachedAt = Date.now();
return this.cachedBranch;
}

async onRequest(context: ProxyContext): Promise<void> {
// Request and session ID headers
context.headers['X-CodeMie-Request-ID'] = context.requestId;
Expand Down Expand Up @@ -66,8 +81,9 @@ class HeaderInjectionInterceptor implements ProxyInterceptor {
if (config.repository) {
context.headers['X-CodeMie-Repository'] = config.repository;
}
if (config.branch) {
context.headers['X-CodeMie-Branch'] = config.branch;
const currentBranch = await this.getCurrentBranch();
if (currentBranch) {
context.headers['X-CodeMie-Branch'] = currentBranch;
}
if (config.project) {
context.headers['X-CodeMie-Project'] = config.project;
Expand Down
Loading