Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 44 additions & 7 deletions src/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7092,6 +7092,34 @@ export async function syncGitHubTokenPropagationForAgents(params: {
}
}

function normalizeGitHubTokenSecretRefCandidate(value: unknown): string | undefined {
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
}
Comment thread
alvarosanchez marked this conversation as resolved.
Outdated

export function resolveGitHubTokenSecretRefForPropagation(params: {
explicitSecretRef?: unknown;
settingsSecretRef?: unknown;
companyId?: string | null;
pluginConfig?: GitHubSyncPluginConfig | null;
}): string | undefined {
const explicitSecretRef = normalizeGitHubTokenSecretRefCandidate(params.explicitSecretRef);
if (explicitSecretRef) {
return explicitSecretRef;
}

const settingsSecretRef = normalizeGitHubTokenSecretRefCandidate(params.settingsSecretRef);
if (settingsSecretRef) {
return settingsSecretRef;
}

const companyId = normalizeGitHubTokenSecretRefCandidate(params.companyId);
if (!companyId || !params.pluginConfig) {
return undefined;
}

return normalizePluginConfig(params.pluginConfig).githubTokenRefs?.[companyId];
}

function normalizeCliAuthPollIntervalMs(value: unknown): number {
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
return CLI_AUTH_POLL_INTERVAL_FALLBACK_MS;
Expand Down Expand Up @@ -12170,11 +12198,12 @@ export function GitHubSyncSettingsPage(): React.JSX.Element {
return;
}

let githubTokenSecretRef =
typeof options.githubTokenSecretRef === 'string' && options.githubTokenSecretRef.trim()
? options.githubTokenSecretRef.trim()
: undefined;
const companyId = hostContext.companyId;
let githubTokenSecretRef = resolveGitHubTokenSecretRefForPropagation({
explicitSecretRef: options.githubTokenSecretRef,
settingsSecretRef: currentSettings?.githubTokenConfigSyncRef ?? settings.data?.githubTokenConfigSyncRef,
companyId
});

if (!githubTokenSecretRef) {
if (!companyId) {
Expand All @@ -12187,8 +12216,10 @@ export function GitHubSyncSettingsPage(): React.JSX.Element {
}

const currentConfigResponse = await fetchJson<PluginConfigResponse | null>(`/api/plugins/${pluginId}/config`);
const normalizedConfig = normalizePluginConfig(currentConfigResponse?.configJson);
githubTokenSecretRef = normalizedConfig.githubTokenRefs?.[companyId];
githubTokenSecretRef = resolveGitHubTokenSecretRefForPropagation({
companyId,
pluginConfig: normalizePluginConfig(currentConfigResponse?.configJson)
});
}

if (!githubTokenSecretRef) {
Expand Down Expand Up @@ -12505,9 +12536,15 @@ export function GitHubSyncSettingsPage(): React.JSX.Element {

let propagationError: unknown = null;
try {
const githubTokenSecretRef = resolveGitHubTokenSecretRefForPropagation({
explicitSecretRef: result.githubTokenConfigSyncRef,
settingsSecretRef: currentSettings?.githubTokenConfigSyncRef ?? settings.data?.githubTokenConfigSyncRef,
companyId
});
await propagateGitHubTokenToSelectedAgents({
selectedAgentIds: normalizeAgentIds(normalizeAdvancedSettings(result.advancedSettings).githubTokenPropagationAgentIds),
previousAgentIds: normalizeAgentIds(currentSettings?.advancedSettings?.githubTokenPropagationAgentIds)
previousAgentIds: normalizeAgentIds(currentSettings?.advancedSettings?.githubTokenPropagationAgentIds),
githubTokenSecretRef
});
} catch (error) {
propagationError = error;
Expand Down
50 changes: 50 additions & 0 deletions tests/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,56 @@ test('syncGitHubTokenPropagationForAgents batches propagation updates with a sma
}
});

test('resolveGitHubTokenSecretRefForPropagation uses saved settings ref when plugin config refs are stripped', async () => {
const uiModule = await importFreshUiModule() as {
resolveGitHubTokenSecretRefForPropagation?: unknown;
};

assert.equal(typeof uiModule.resolveGitHubTokenSecretRefForPropagation, 'function');

const resolveGitHubTokenSecretRefForPropagation = uiModule.resolveGitHubTokenSecretRefForPropagation as (params: {
explicitSecretRef?: unknown;
settingsSecretRef?: unknown;
companyId?: string | null;
pluginConfig?: GitHubSyncPluginConfig | null;
}) => string | undefined;

assert.equal(
resolveGitHubTokenSecretRefForPropagation({
explicitSecretRef: ' explicit-secret-ref ',
settingsSecretRef: 'settings-secret-ref',
companyId: 'company-1',
pluginConfig: {
githubTokenRefs: {
'company-1': 'config-secret-ref'
}
}
}),
'explicit-secret-ref'
);

assert.equal(
resolveGitHubTokenSecretRefForPropagation({
settingsSecretRef: 'settings-secret-ref',
companyId: 'company-1',
pluginConfig: {}
}),
'settings-secret-ref'
);

assert.equal(
resolveGitHubTokenSecretRefForPropagation({
companyId: 'company-1',
pluginConfig: {
githubTokenRefs: {
'company-1': 'config-secret-ref'
}
}
}),
'config-secret-ref'
);
});

test('resolveInstalledGitHubSyncPluginId finds the GitHub Sync installation id from plugin listings', () => {
const records = [
{
Expand Down